Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.21
more ▼

asked Dec 19 '18 at 07:12 PM in Using UE4

avatar image

169 9 18 24

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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!


more ▼

answered Dec 19 '18 at 07:49 PM

avatar image

351 7 6 13

avatar image leofucci Dec 19 '18 at 11:30 PM

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?)

alt text

8.png (20.6 kB)
avatar image TestyRabbit Dec 20 '18 at 12:00 AM

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.

avatar image leofucci Dec 20 '18 at 01:48 AM

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?

avatar image TestyRabbit Dec 20 '18 at 05:40 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question