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.