Variables inside Player Controller in Multiplayer games.

Can someone please help me? I’m creating an RTS game in which I store the team of each player inside the PlayerController class. The first player is assigned to team 0, the next player to team 1, and so on. When a player joins the game, I set their team inside the GameMode Blueprint as shown below:

It works fine for the host (perhaps because they are always on team 0 as the first to join?), but when I try to retrieve the team to create a HUD on the client, it can’t get the team correctly. It always returns 0 (the default).

So, can I use variables inside the PlayerController to control some aspects of the game in multiplayer or not? Remember that I set this team variable inside the Game Mode (which is server-side), and the team variable is being replicated!

Thanks for the help.

you can but other players wont have access to it, so say if the teamname is story on the player controller other players wont see it, use the player state instead

Okay, I figured out what the problem was. The logic to create the HUD was being invoked before the server set the teams within the Player Controller. It was a timing synchronization issue. The problem was resolved by adding a 0.2-second delay node before the ‘Begin Play’ event in the Player Controller.

just be careful with delays like that as things may change in the future and then your ref wont work again, especially when packaging things like that can break.

you could loop it until valid to be sure which is wasteful but only done once on load so not horrible or create a event system, like a load screen that loads everything you need in the order you need.

Your solution seems to be better and more appropriate. Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.