Blueprint Class as Variable

I have a Blueprint actor that wants to spawn another type of actor. In Unity, I could just say public SpawnedClass prefab, and then drag and drop that prefab from the project browser into the variable. However, in Unreal, I can’t seem to get a reference to SpawnedClass, only to an instance of it.

In the spawner’s C++ class that it inherits from, I have:

(.h file)

UPROPERTY(EditAnywhere, Category = Spawning)
		SpawnedClass* spawnBP;

(.cpp file)

FActorSpawnParameters spawnParams;
	spawnParams.Template = spawnBP;
SpawnedClass* t = (SpawnedClass*)()->SpawnActor<SpawnedClass>(spawnParams);

I also tried making FActorSpawnParameters a UPROPERTY instead but got errors saying it had to be declared as a UCLASS USTRUCT or UENUM. Anyone know how to get a blueprint type reference and not an instance reference?

Instead of SpawnedClass* spawnBP;

Try TSubclassOf<SpawnedClass> SpawnBPClass;

Note that UE4 (unlike Unity) use unique classes for each actor type, so for each C++ actor and for each blueprint (yes blueprints ARE ALSO classes) there a class, so to spawn something you need to refrence class of object. Unity need prefabs because each actor you place on level in Unity is unique composition of GameObject class and all actors are GameObject objects, so to save what you make you need to save actors to special file call prefab which is serialization of GameObject state. It’s a main fundamental difference between UE4 and Unity that you will need to get use to it, i recommend you forget about prefabs is in UE4, because this is not how things work in UE4 and it will only confuse you.

But to topic at hand, C++ does not support natively class refrencing variables, insted in UE4 you can reference class via UE4’s reflection system. Each class inheriting from UObject when engine starts have a UClass object made. working as class identifier, it also contains information about the class you can access. So to reference class you make UClass* variable there also special template TSubclassOf<UBaseClass> that limit class selection to specific base class (This is also reflected in editor, bluepritns, property editor etc.), this time is what usally will see but it works the same as UClass*

To get UClass* of specific class you use StaticClass function which is static function that every UObject have, so for example ACharacter::StaticClass() , you can also use GetClass() from any UObject to get class of object independently of type of variable you calling

Some functions in UE4 has class template (like you doing SpawnActor<SpawnedClass>), it used for auto casting return type so you don’t need to cast… which you trying do, it’s pointless its already casted. You can ONLY place class name in there not variable name. In case of SpawnClass template also sets class of actor that gonna be spawn, or else you give different class in argument then template is ignored and only used for auto casting.

So let say you blueprints are based out of ABaseClass (in general it’s best practice have base class in C++ for perticilar type of blueprints you making with all properties and functions declared in there that you gonna use, this makes it easier to reference things in C++, because you can not alse reference Blueprint content directly in C++, if you work in C++ in UE4 then it better to declare all things in C++ it saves lot of hassle, specially structs and enums)

you declere class property like this:

UPROPERTY(EditAnywhere, Category = Spawning)
TSubclassOf<ABaseClass> spawnBP;

And then to spawn:

ABaseClass* t = ()->SpawnActor<ABaseClass>(spawnBP);

Now about template you generally don’t need that, class already have state of you blueprint (as defaults are stored in UClass*), you only need this if you want to clone state of a already spawned actor that is on the level.

Btw UClass is just tip of a iceberg of UE4 reflection system, each structural thing in code have is own UField class which let you read how class or struct is build it properties and functions and so on, if you master it it allow to make auto generative UI, you can see this in UE4 editor, entire property editor just reads UClass and gets all UProperty of class to generate property editor UField | Unreal Engine Documentation

This worked to get rid of compile errors, though now I’m facing a different hurdle. I’m trying to spawn actors while in editor (not playing the game), and it keeps crashing unreal when I try.

FActorSpawnParameters spawnParams;
	spawnParams.Template = (AActor*)spawnBP.Get();
ABaseClass* t = (ABaseClass*)()->SpawnActor<ABaseClass>(spawnParams);
spawnArray.Add(t);

If I try to call a function from t, it crashes. When I try to add it to a TArray it crashes. I don’t know what is going wrong, theres so little examples of how to do this, only unhelpful documentation.

Nevermind I figured it out

ABaseClass* t = ()->SpawnActor<ABaseClass>(spawnBP);
tiles[y].Add(t);

I was overcomplicating things, who knew.