How can a client send a variable from his Game Instance to the listen server? So that everyone else can see it too?
I have a string variable in the game instance called “Playername”. The client sets it before he joins a session. Then he joins a session. Now I want to get his playername. I tried to work with Event PostLogin in the Game Mode but I can’t get it to work.
(I work with blueprints)
Create a custom PlayerState Blueprint. Set this in your GameMode Defaults Settings. Create a PlayerName Variable in the PlayerState (there is already one from Epic, but you can’t SET it, so just ignore it in Blueprints). Make it REPLICATED!
Now, in your PlayerState Blueprint, create the BeginPlay Event Node. Use “Switch has Authority” and choose the “Remote” exec, which will be called client side (Authority is Server version of the PlayerState).
Create a new CustomEvent (still in PlayerState). Set it to “Replicated, Run on Server”. Give it an Input Parameter. Likewise of the same type as your PlayerName variable (String, Text, or what ever you’ve chosen). Take the Input and set the PlayerName with it (so the custom event is just setting the PlayerName by what ever a client passes over).
Now back to the BeginPlay with the Switch has Authority node. Behind the “Remote Exec”, get your GameInstance, cast it to your custom GameInstance class and get the PlayerName you saved there. Now call the Custom Event you created and pass the PlayerName over.
And, additional info:
This PlayerName variable can be accessed from every other client, since the PlayerState is replicated. The GameState class (which every client can get) has an array with all PlayerStates.
If you have a Listen host, so a server who also plays as a client, you will want to add the part with the GameInstance to the “Authority” exec. But instead of calling the RunOnServer event, you just directly set the Variable.
Why? Because only the server is allowed to set a replicated variable.
Very nice, thank you so much!
But How do you know that this Player state belongs to the player on listen server and so you should switch on authority instead of remote , how do you calculate that?