So I’m doing a basic PRINT node on my client side just to get an understanding of what to expect within networking, I’m using PIE mode with 4 players and my Net Mode is ‘Play As Listen Server’.
Ah thank you, for some reason I was thinking the game instance could carry variables from clients. I guess I’m just trying to carry some vars from a client to the server to distribute to other clients.
More specifically I’m just trying to display other clients names onto their widget. I’m thinking I can use playerstates to hold some information and possibly distribute through gamestate for this.
So hypothetically say I have a textbox input on a clients widget for their player name… What would be the best practice for storing this information for distrubuting?
Storing it in the GameInstance is good (the GameInstance will persist throughout level changes, so if you want to keep that information throughout level changes then GameInstance is the place to store it in).
If you want to show that information on widgets on other clients, then you need three steps:
(-) client tells server
(-) server tells other clients
(-) other clients each tell the widget
For example you could do:
(-) client tells server
That only works through a RunOnServer RPC (a custom event where you select run on server under replicates). And the name would be an input into the RPC. Something to note, is that a client can only call RunOnServer RPCs on an actor that it owns (e.g. the player controller, but you can also set ownership on the server).
So you could create a RunOnServer event in your player contoller which you then on the client to tell the server. EventBeginPlayer → IsLocalPlayerController (should be true) → RunOnServer event
(-) server tells other clients
(-) other clients each tell the widget
Those two steps will depend on how you choose to handle this and I do not know of a very good way to do this. For example, from the RunOnServer event you could set a repnotify variable in the player pawn (get controlled pawn in the player controller to get the right player pawn). Then from the repnotify function you tell the widget the new username.
Hmm… so I guess I’m still confused about whether I can stored local variables for the clients on their instance of the game. It seems I was able to get their name (var string) from fetching their instance of the game.
Notice the delay, it was needed to allow the playerstate to finish loading the variable from the clients game instance. I just need to learn more about RPCs and where/when to use them as right now I understand what they are used for but not entirely sure how to use them.
Notice the delay, it was needed to allow the playerstate to finish loading the variable from the clients game instance.
Instead of the delay, you could create use an event dispatcher in the playerstate that broadcasts when it has received the data from the client (in the RunOnServer event). Then bind to that event dispatcher in your game mode.
Or you can also call an event in the game mode from the RunOnServer event in the player state.
The difference between the two approaches is just whether you want the game mode to know about the playerstate (which is what you currently have), or the player state about the game mode.
Thank you for the reply, I’m still learning and dispatchers are still a little trivial for me at this state. I’ll have to read up a bit more on how to use them.