Spawn derived class at runtime (blueprint derived from C++)

Let’s say I have a property in my actor class:

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Magic)
TSubclassOf<ASpellProjectile> testProjectileClass;

I want to set this property in editor (by dragging Blueprint derived from ASpellProjectile into property), and then spawn the class at runtime.

Problems:

First, for some reason this doesn’t work:

ASpellProjectile *tmp = Cast<ASpellProjectile>(testProjectileClass);

in this example tmp will always be null.

Second, attempting to spawn actor being referenced:

ASpellProjectile *projectile = 
	world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

Results in message:

 SpawnActor failed because BlueprintGeneratedClass is not an actor class

So, how do I actually spawn an actor from “template”/“preset”?

Hello NegInfinity,

You should be able to fix this by making one change to the lines of code you provided. In this line:

ASpellProjectile *projectile = world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

Remove the “->GetClass()” part and just leave the variable name “testProjectileClass”. This should let the function know that you intend to spawn that class. You can find more information about this kind of setup from the following video.

Hope this helps!

Thanks for the response. I was having trouble because I thought that testProjectileClass points at object instance to clone (like unity prefabs), not at object class. Someone mentiond on the forums that it points at class, not instance, and that cleared things up.