How do you spawn a class chosen in editor with C++?

I have a C++ Pawn class, ‘MyPawn’. I have a subclassed blueprint version where I can change the mesh, etc in the editor.

Similarly I also have a Projectile class and a subclassed blueprint version.

I’m trying to set which type of projectile a type of pawn shoots, like so:

UPROPERTY(EditDefaultsOnly)
TSubclassOf<MyProjectile> ProjectileType;

This appears fine in the editor and I can select the base projectile class or my blueprinted subclass which has a mesh assigned, etc.

Now, I’m trying to spawn one of those projectiles and I have no idea how to do it. I’m looking at the docs and every version I try doesn’t seem to work. I have:

GetWorld()->SpawnActor<ProjectileType>(ProjLoc, GetActorRotation());

I get the error 'UWorld::SpawnActor': no matching overloaded function found.

For that matter: GetWorld() is underlined with an error that says pointer to incomplete class type is not allowed

In fact, many parts of my code have been getting underlined saying either the ‘incomplete class’ thing or “undeclared identifier”, even when it compiles and runs just fine. I have no idea why and it’s completely infuriating. I always seem to get this issue when working with custom enums or structs… it makes intellisense totally break down even if the code actually compiles just fine. Help?

Thank you, I got it to work.

Hey!

So c++ teach here:

Template functions which is marked with T (in short everything is template function/class etc if you need give something between <> ) means you need your class as Template.
Not your variable, your c++ class!

SpawnActor will give back your spawned projectile as class which you give to template as input :slight_smile:

Your Code must look in this way:

SpawnActor<MyProjectile>(ProjectileType,Location,Rotation)

Or something like that because i dont know your class names, but in short

Between <> goes your C++ class name, not your file name your class name like APawn, AActor, AMyProjectile, MyProjectile depends on how you named that class :slight_smile: (check yourclass.h and youl will se after class word :wink: )

Next, spawn what you want spawn goes first after (WhichClassWantyouSpawn, location, rotation)

Whichclasswantyouspawn can be a c++ class or a TSubClassOf variable. SpawnActor decide about this which spawn need to place in world :slight_smile: