Trouble spawning an actor

This is more of a “I don’t understands templates or function templates” post, and I’m hoping someone can help me out with understand how they work.

I am trying to spawn my actor, and I have successfully done so by doing

APawn* ResultPawn = (APawn*)GetWorld()->SpawnActor(APawn::StaticClass());

The issue is I want to spawn it at a certain location with a certain rotation, and this Spawning Actors in Unreal Engine | Unreal Engine 5.1 Documentation says I need to use templates for that.

I gave it a shot by literally copying the last example: (.h)

template< class T >
T* SpawnActor
(
    UClass* Class,
    FVector const& Location,
    FRotator const& Rotation,
    AActor* Owner=NULL,
    APawn* Instigator=NULL,
    bool bNoCollisionFail=false
)
{
    return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
}

(.cpp):

APawn* ResultPawn = SpawnActor(DefaultPawnClass, StartLocation, StartRotation, NULL, Instigator);

so this is my code:

(.h)

	template< class T >
	T* SpawnActor
	(
		UClass* Class,
		FVector const& Location,
		FRotator const& Rotation,
		AActor* Owner = NULL,
		APawn* Instigator = NULL,
		bool bNoCollisionFail = false
	)
	{
		return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
	}

(.cpp)

	const FVector startLocation = FVector(-8908.868164f, -12233.826172f, 3496.578613f);
	const FRotator startRotation = FRotator(0.f, 0.f, 0.f);
	
	ATower_Archer* ResultPawn = SpawnActor<ATower_Archer>(ATower_Archer::StaticClass(), startLocation, startRotation, NULL, Instigator);

I get these errors:

[2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error e:\users\*\documents\unreal projects\towerdefenseproject\source\towerdefenseproject\MainPawn.h(124) : error C2661: 'UWorld::SpawnActor': no overloaded function takes 9 arguments
[2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error E:\Users\*\Documents\Unreal Projects\TowerDefenseProject\Source\TowerDefenseProject\MainPawn.cpp(48) : note: see reference to function template instantiation 'T *AMainPawn::SpawnActor<ATower_Archer>(UClass *,const FVector &,const FRotator &,AActor *,APawn *,bool)' being compiled

I followed the example exactly like it says and I can’t quite figure it out.

You don’t need to write the code for the template function - Unreal Engine has already provided that. You just need to call it. To call a template function, the class you want to use needs to go in the angle brackets. Like so:

ATower_Archer* ResultPawn = GetWorld()->SpawnActor<ATower_Archer>(startLocation, startRotation);

This should spawn an ATower_Archer at the desired location and rotation. If you wanted to spawn another class, you’d just replace ATower_Archer with AAnother_Class.

AAnother_Class* ResultPawn = GetWorld()->SpawnActor<AAnother_Class>(startLocation, startRotation);

You only need that line of code - delete the declaration and definition of SpawnActor in your own header and cpp files.

Oh. Thanks. Just tried that and it worked.

Great! Could you mark the answer as accepted? There’s a checkmark under the upvote/downvote button.