When a Client connect to a Server it can pass information to the server so the server will have the value already available during the Login call.
This is done when the Client calls “OpenLevel” in the “options” parameter.
Nameis a key that GameModeBase is already looking for when logging in a new player.
The client when calling OpenLevel could then add Name=MyName to the “options” field.
The PlayerState Class already replicate “PlayerName” and have a function in c++ for OnRep_PlayerName. If you want a callback in Blueprint you could declare a multicast Delegate and call it when OnRep_PlayerName is called.
The Pawn class also has a OnRep_PlayerState function that you should also also declare a multicast Delegate for.
Now the final steps is to bind an event to the OnRep_PlayerState Delegate on the Pawn class followed by binding an event to the OnRep_PlayerName Delegate on the replicated PlayerState. Whenever this PlayerName Delegate is called is when you update the Widget name field.
Quick Recap.
- Add the option
Name=MyNameto the OpenLevel call on the client - Add and call a Delegate for OnRep_PlayerState in C++ on the Pawn/Character class.
- Add and call a Delegate for OnRep_PlayerName in C++ on the PlayerState class.
- Bind an Event to the PlayerState Delegate
- Bind an Event to the PlayerName Delegate by the Event in step 4.
- Use the step 5 Event to update the widget name field.
If you want to avoid c++ you would need to make a duplicate of the PlayerState variable on the Pawn/Character and make it a RepNotify variable and set it by the Server when “OnPossessed” is called.
You also need to duplicate PlayerName on the PlayerState and make it RepNotify and set it by the Server when “OnChangeName” is called on “GameModeBase”.
To be able to bind to the callbacks for both variables also create an Event Dispatcher for both OnRep_PlayerState and OnRep_PlayerName.