Spawn Actor from Class?

Hello,

I have an object with a class variable (UClass* MyClass;), and I would like to spawn an actor from this class. How can I go about this? It would be optimal to have some sort of equivalent to this blueprint function.

254199-unbenannt.jpg

I’d want it to specifically use UClass* because this will be a variable exposed as a spawn parameter, where as soon as this actor is spawned, it will spawn another actor from MyClass.

Basically:

 FVector Location(0.0f, 0.0f, 0.0f);
 FRotator Rotation(0.0f, 0.0f, 0.0f);
 FActorSpawnParameters SpawnInfo;
 GetWorld()->SpawnActor<MyClass::StaticClass>(Location, Rotation, SpawnInfo);

well, to correct you based on the question without making a new answer it would be :

UClass* MyClassToSpawn;
GetWorld()->SpawnActor(MyClassToSpawn,Location, Rotation, SpawnInfo);

I now get this error:

254213-unbenannt.jpg

I have an ‘#include “Engine/World.h”’ in the .cpp file, and a ‘UClass* MyClass;’ as a public variable in the .h file. What could I have done wrong?

Whoops, just tried it’s asking for pointer to rotation and location when using in this way. this should work:
UClass* MyClassToSpawn;
FVector Location(0.0f, 0.0f, 0.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f);
FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor(MyClassToSpawn, &Location, &Rotation, SpawnInfo);

Yep, all working! Thank you so much! :slight_smile: I’ll post the full resolution as an answer with your credit.

Firefly 74 provided this answer, reposting it as an answer so I can mark it solved:

#include "Engine/World.h"

UClass* MyClass;
FVector Location(0.0f, 0.0f, 0.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f);
FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor(MyClass, &Location, &Rotation, SpawnInfo);