How To Spawn A Named Actor From C++?

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!

You can create C++ GameMode (forexample) with public UE4 class property like:

class MY_API MYGameMode : public AGameMode
{
	GENERATED_BODY()
	public:
		UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ClassTemplates")
			TArray<UClass*> classTemplates;
		UFUNCTION(BlueprintCallable, Category = "ClassTemplates")
		void SpawnFromTemplate(int32 templateindex);
}

then you can derive your ingame BP game mode from this class and set this property inside BP. You can set any “only BP” classes for this. And then you can call your Spawn with BP defined classes any place after GameState constructed and initialized.

P.S. You cant use that property inside constructor because BP defined property initialize much later, but you can use it, for example, inside BeginPlay…

Thanks, Detech! Like the name, by the way! I ended up not being able to require a class to be derived from one of my classes, but I was able to get what I needed by requiring the developer to register the class immediately after BeginPlay in the Level Blueprint. After that I can spawn their classes by name as needed, now that I know about them after registration.

Thanks!