Hello,
I am working on a multiplayer card game and for that needed to be sure that the players (and AI) can only spawn at empty spawning positions, so four players at four defined positions. In case anyone else is looking for a solution for that, I wanted to share my current solution.
I am overwriting the ChoosePlayerStart function in the GameMode (so look for it under functions->override in the gamemode blueprint). This is how the function looks like:
So in short:
- get all actors of the class Player Start
- loop through this returned array
- check the tag of the current Player Start item
- if it is not equal to “Taken” (or anything else you want): choose this spawn point
- set the tag to “Taken” (or any other identifier) and return the Player Start item
- if it is already taken: continue the loop
If I want to add AI opponents after all human players joined, I use the same method for the AI spawning at the open seats.
Update:
C++ Implementation:
The function to override is called ChoosePlayerStart_Implementation, because the parent function is declared as a Blueprint Native Event.
AActor* AClassName::ChoosePlayerStart_Implementation(AController* Player) {
// create array with all PlayerStart Actors
TArray<AActor*> PlayerStarts;
TSubclassOf< APlayerStart > PlayerStart = APlayerStart::StaticClass();
UGameplayStatics::GetAllActorsOfClass(GetWorld(), PlayerStart, PlayerStarts);
//Loop through Array, spawn only if the spawn is not already taken
for (auto Spawn : PlayerStarts) {
if (Cast< APlayerStart>(Spawn)->PlayerStartTag != "Taken") {
Cast< APlayerStart>(Spawn)->PlayerStartTag = "Taken";
return Spawn;
}
}
return Super::ChoosePlayerStart_Implementation(Player);
}