How to properly use PlayerArray (add/remove players)?

Hello people!

So, i´m developing my first multiplayer game, i´m on the stage where I would like to create a menu where you can see blue team and red team , if you connect to my server, you can get the list of connected players. While doing some research, I found this thread:

…which basically says that GameState and PlayerState is already prepared to work on this feature.

Of course it didn´t work. Everytime I try to loop this array, it´s empty. So question is…

  1. When my player connects to my server, how does the gameState knows that a player is connected?

  2. How should I use the PlayerArray? Shoudl I add playerStates manually?

  3. I can see this Player State has an ID, but this ID changes all the time, is not unique for the player, so what is the idea of this ID if it will not be reliable?

  4. I see you have a “GetPlayerName” but there is no way to “SetPlayerName”… (I think this is a very old question nobody knows)

So in conclusion, if somebody could point me best way to achieve this, will be very welcome.
Thanks!

  1. The gamestate knows that a player has connected because the game mode tells it that a player has join I believe. You can hook into this event using the OnPostLogin event in the game mode.

  2. As far as I remember, the playerstates are automatically added to the playerarray when a player joins a server. If it doesn’t, you can do that yourself in the event I mentioned in 1. The way to interact with these would be to get a playerstate and then cast it to your playerstate class.

  3. So this player ID does represent a unique number for a player. It can change based on which online subsystem you’re using, but it will always be unique. From the documentation: Unique net id number. Actual value varies based on current online subsystem, use it only as a guaranteed unique number per player.

  4. Playerstate does have a SetPlayerName(const FString &S) function, so I’m not sure where you saw that.






    Does this help? I think I answered your questions, but if not let me know! Good luck!

-Testy

I will retry in a few moments about 1 and 2, to discard that I´m doing something wrong and not seeing the array properly or something.

But regarding SetPlayerName, inside my PlayerState class, I don´t see any method like that, please see my screenshot. (perhaps is available in c++ only?)

262905-8.png

Ah, yeah. it looks like that’s not exposed to blueprints. I guess the next course of action would be to make your own PlayerName variable in your PlayerState class and use that one instead. I wonder why you can’t set it in blueprints, that seems like a strange decision.

Would you say I need to mark this variable as “Replicate”? or as is part of player state which is replicated, everything inside it will be replicated by inheritance?

I would definitely set the variable to Replicate otherwise it won’t be replicated