Load stuff from Content Browser

One of the thing you are doing lot with Unreal Engine C++ programming is to get references to your Content Browser objects. Getting basic reference is super easy thing to do. In this tutorial, we are going through basic methods to get the references.


ConstructorHelpers

If you want to load data from content browser when you are compiling your game, you want to use ConstructorHelpers class.  You can load any kind of data using it. Lets try some of them. Just for the sake of it, create a C++ class and make some class pointers in header file.

UTexture* TestTexture; // Texture2d
UMaterial* MyTestMaterial; // Material
UStaticMesh* MyTestMesh; // Static mesh
UParticleSystem* MyTestParticle; // Particle System
UObjectLibrary* LibraryOfObjects; // Object Library. Remember to add ObjectLibrary.h!
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TSubclassOf<AActor> ToBeSpawnActor; //Class

In constructor we can find the Content Browser objects using the ConstructorHelpers class. Be sure to set the class you want to find properly and be precise with the spelling errors. If you want, you can right-click assets in Content Browser and choose ”Copy Reference” and then paste it in code.

ConstructorHelpers::FObjectFinder<UTexture> MyTexture (TEXT("/Game/Textures/Natural.Natural"));
if (MyTexture.Succeeded())
TestTexture = MyTexture.Object;
ConstructorHelpers::FObjectFinder<UMaterial> MyMaterial(TEXT("/Game/Materials/M_MyMaterial.M_MyMaterial"));
if (MyMaterial.Succeeded())
MyTestMaterial = MyMaterial.Object;
ConstructorHelpers::FObjectFinder<UStaticMesh> MyMesh(TEXT("/Game/StarterContent/Props/SM_Chair.SM_Chair"));
if (MyMesh.Succeeded())
MyTestMesh = MyMesh.Object;
ConstructorHelpers::FObjectFinder<UParticleSystem> MyParticle(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
if (MyParticle.Succeeded())
MyTestParticle = MyParticle.Object;
ConstructorHelpers::FObjectFinder<UObjectLibrary> MyObjectLibrary(TEXT("/Game/ObjectLibrary/ObjectLibrary.ObjectLibrary"));
if (MyObjectLibrary.Succeeded())
LibraryOfObjects = MyObjectLibrary.Object;

That is how we get reference to basic stuff! If we want to load an actual blueprint class, we need to do little differently. Instead of FObjectFinder we use FClassFinder. If we are not sure what the class is, or we can’t get any reference to it inside our code (It is purely made in Blueprints) we can just use the base class (AActor) and cast it later.

static ConstructorHelpers::FClassFinder<AActor> SomeClass(TEXT("/Game/StarterContent/Blueprints/Blueprint_Effect_Fire"));
if (SomeClass.Succeeded())
ToBeSpawnActor = SomeClass.Class;

We can confirm these in BeginPlay(), if we want.

Super::BeginPlay();
if (TestTexture)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *TestTexture->GetPathName());
if (MyTestMaterial)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *MyTestMaterial->GetPathName());
if (MyTestMesh)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *MyTestMesh->GetPathName());
if (MyTestParticle)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *MyTestParticle->GetPathName());
if (ToBeSpawnActor)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *ToBeSpawnActor->GetPathName());
if (LibraryOfObjects)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully"), *LibraryOfObjects->GetPathName());

And that is the formula you can use to find any other object types you can think of! Very easy and powerful!

LoadObject / StaticLoadObject

If we are not sure what assets need to load or we just don’t want to load something in constructor, then we can use LoadObject or StaticLoadObject.  They do basically the same thing, but StaticLoadObject gives you more flags to use.

Heres how to use LoadObject. We tell what is the type we are loading and then give a path to the object. That’s it.

UTexture2D* TextureSample = LoadObject<UTexture2D>(this, TEXT("/Game/Textures/Forge.Forge"));
if (TextureSample)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully on Runtime"), *TextureSample->GetPathName());

StaticLoadObject works similary, but we need to give additional details. Remember, StaticLoadObject can take more additional flags if you need to add them.

UTexture2D* TextureSample2 = Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), this, TEXT("/Game/Textures/Frosty.Frosty")));
if (TextureSample2)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully on Runtime"), *TextureSample2->GetPathName());

If you want to load a class, you can use LoadClass.

TSubclassOf<AC_SecondActor> SecondActor; 
SecondActorToSpawn = LoadClass<AActor>(this, TEXT("/Game/Blueprints/BP_SecondActor.BP_SecondActor_C"));
if (SecondActorToSpawn)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully on Runtime"), *SecondActorToSpawn->GetPathName());

or you can use FStringreference and to load class

 TSubclassOf<AActor> TmpActor = FStringClassReference("/Game/Blueprints/BP_SecondActor.BP_SecondActor_C").TryLoadClass<AActor>();
if (TmpActor)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully!"), *TmpActor->GetPathName());

Finally. If you know the object is already loaded in memory. You can just use FindObject. If the object is not in memory, search will fail. If you want to be sure it succeeds, use LoadObject or StaticLoadObject.

UTexture2D* TextureSample3 = FindObject<UTexture2D>(this, TEXT("/Game/Textures/Frosty.Frosty"));
if (TextureSample3)
UE_LOG(LogTemp, Warning, TEXT("%s loaded successfully on Runtime"), *TextureSample3->GetPathName());

You can now succesfully load any UObject at runtime! Happy coding.

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *