I am trying to spawn a Blueprint defined without a C++ base class (that’s a requirement of what I’m working on), and trying to spawn the Blueprint from C++ using only its name. I have created a test actor called SimpleActor in Blueprints (no C++ base class). I have two problems getting this to work, but the code is below:
AActor * SpawnSimpleActorActor(float X, float Y, float Z, float Roll, float Pitch, float Yaw)
{
UObject * ClassPackage = ANY_PACKAGE;
UObject* ObjectToSpawn;
ObjectToSpawn = FindObject<UObject>(ClassPackage, TEXT("/Game/Blueprints/SimpleActor.SimpleActor"));
if (ObjectToSpawn)
{
UClass * ClassToSpawn = ObjectToSpawn->StaticClass();
if (ClassToSpawn)
{
const FVector Location = FVector(X, Y, Z);
const FRotator Rotation = FRotator(Pitch, Yaw, Roll);
AActor * NewActor = GWorld->SpawnActor(ClassToSpawn, &Location, &Rotation);
if (NewActor)
{
return NewActor;
}
}
}
return (AActor*)NULL;
}
The first issue that I’m facing, is that FindObject returns null unless the class is loaded first. I can get FindObject to return a valid pointer if I call this function below first:
ConstructorHelpers::FObjectFinder SimpleActorFinder(TEXT(“Blueprint’/Game/Blueprints/SimpleActor.SimpleActor’”));
However this function can only be called in a constructor. I need a way to load the class first outside of a constructor. I found this link on the forums:
Which explains using the LoadObject function, however I can’t find that function either in the engine code or searching Google, and the link posted by the answerer is broken. That’s the first problem: How do I load the class outside of a constructor?
The second question is, even if I call that function in a random constructor so that the class is loaded, the above GWorld->SpawnActor function call is still returning null and the actor isn’t in the scene. How do I get this part to work so I can at long last spawn a non-C+±based Blueprint from C++ by only knowing its name? It has taken a lot of work and troubleshooting just to wrap my brain around what my question is.
Thanks!