I want to make an array of players currently in a session in multiplayer. the array itself will contain a wee bit of data, such as username of the players.
The problem is that the players currently only have the username locally on their clients, and it’s stored in the GameInstance, because that persist through level changes and joining of sessions.
GameInstance would seem like the most logical place to put the array of players, but each player seem to create his own array, containing only themselves… Is there anywhere else to put the array, or alternatively make it work in GameInstance?
Use the replicated “PlayerState” for this. The PlayerState class already contains PlayerName and the UniqueID Variable. These variables are replicated as well as the PlayerState itself.
Also the GameState has an Array called “PlayerArray” in which all PlayerStates can be found. This is also replicated.
You can also create your own PlayerState and GameState class/BP. If you have done this there is one little tricky thing to do. The GameState can be set under “Maps and Nodes” where you also set your Playerclass, HUD, Controller, GameMode etc. You can find it under Project Settings:
To set your own PlayerState, you need your own GameMode (you can set this up in the Maps and Nodes too, like i said above). After you created your GameMode and PlayerState BP, you need to OPEN the GameMode BP and click on “Class Defaults” at the top. After that, you will have the options on the right side showing:
As you can see, there you can set your PlayerState (:
I hope that helps!
More information about the PlayerState:
More information about the GameState:
Thanks, that seems to have solved things. Just again tricky with having to save the variables to the PlayerState.
Can you please explain, who is in charge to add player to this PlayerArray? For me is always empty, should I add myself and others?..also which place should be the best place to do this?