How can I Create a PlayerStart from code?

Hi,

I’ve deleted the PlayerStart in the UE Editor, and I would like to add one in the scene, from C++.

Here is what I’ve done actually (in a custom GameMode class) :

PlayerStart = GetWorld()->SpawnActor<APlayerStart>();
PlayerStart->SetActorLocation(Location);

AddPlayerStart(PlayerStart);

The code is compiling, but when I’m starting the game, it’s not using my PlayerStart but a “default” one (automatically created by the editor)…

So, how can I use mine ?

Thanks !

#try more of a full spawn code like this

FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail 		= true;
SpawnInfo.Owner 				= this;
SpawnInfo.Instigator				= NULL;
SpawnInfo.bDeferConstruction 	= false;
 
PlayerStart = GetWorld()->SpawnActor<APlayerStart>(APlayerStart::StaticClass(), Loc ,FRotator::ZeroRotator, SpawnInfo );

Thanks for your answer (again!) Rama ! Btw there is a problem… The PlayerStart is at the right place if I’m looking to its properties in the editor BUT the view is not at that coordinates… In my case, I’m generating a level through code, and after, I’m creating/adding the PlayerStart, but so, not right placed… Do you have an idea ?

I encountered this same issue in unreal engine 4.9, but Rama’s solution didn’t make any difference for me.

I used the approach described here. Specifically, overriding one of AGameMode::FindPlayerStart_Implementation or AGameMode::ChoosePlayerStart_Implementation to use your APlayerStart.

In my case, I simply overid ChoosePlayerStart_Implementation() and had it return the member variable of my AGameMode-class that points to my APlayerStart instance, which is spawned as Rama describes. Making the function more robust may be advisable, but this at least appears to work.