Pick PlayerStart depending on Team

So I’m making a multiplayer game, which involves two teams, and i want to have a set of PlayerStarts for each team. Pretty standard.

The way i’m currently doing it is:

  • The PlayerState has the TeamIndex. This TeamIndex is set from the GameMode class on PostLogin event.
  • When i pick a PlayerStart to use for spawning in the ChoosePlayerStart method, the PlayerState’s TeamIndex variable isn’t yet set, so it picks the same PlayerStarts for people of both teams.

How do i solve this?

Thanks in advance :slight_smile:

why isnt the teamindex set on the playerstate yet? i make my game wait to spawn player pawns until after their player state is initialized

How do you make your game mode wait until the player state is initialized?

I just dont tell it to spawn any pawns until everything else is done. Is yours spawning pawns without you telling it to with a Spawn Actor node? If you arent using a Spawn Actor blueprint node then how are the pawns being created in the game? Are they already placed in the level so their BeginPlay runs before yor setup stuff is ready?

OnPosogin indicates playercontroller is created but doesnt guarantee playerState is created. try adding a small delay or check if playerstate exists for that playercontroller before assigning teamindex

From what i understand, the ChoosePlayerStart function is the one that spawns the Pawns. That’s done automatically. It’ll pick a PlayerStart and spawn a player there. But when that happens, the PostLogin function (which sets the TeamIndex in the PlayerState) wasn’t yet called.

That’s not my problem though :stuck_out_tongue: so far I’ve had no problems with that, the problem is: How do i setup the PlayerState’s TeamIndex before i spawn the player?

oh ok. i see the problem now. I assumed postlogin would already be called and a player already created before any code runs to attempt to spawn. sounds like if there is no player yet it creates on for you as well.

I believe you can create a Player (without spawning a pawn for them) with the Create Player node in BP so there is a C++ equivalent. If I remember right then this will trigger the OnPostLogin event because the new player was created.

if the player in question already exists then the onpostlogin will not fire so you will have to rely on a different event to initialize the player state’s team index.

arethe teams assigned by choice or automatically?

Ok good to know :slight_smile:

The team is assigned automatically (every time a player joins on the PostLogin event, the assigned team index swaps and they will be put on that team.)

In thag case would it be okay to assign the team on Possess, right after the pawn spawns? Would that cause any problem with the scoring or anything?

I don’t think so, but i just checked, and the OnPossess event in the player controller still gets called after the ChoosePlayerStart :confused:

I ended up fixing this by:

  • Overriding the GameMode’s ChoosePlayerStart method and simply returning nullptr, so that the GameMode doesn’t spawn a new player unless explicitly told to.
  • Have the PlayerController call a GameMode function (that you’ll have to create) that notifies it that a new player should be spawned. This should only happen if IsLocalPlayerController() is true in the Player Controller.
  • When said function is called in the GameMode, the team index can be assigned, and you can then spawn the new pawn in one of the team’s corresponding PlayerStarts.