Pass PlayerState to the server when the client join a session

Hi!

I’m working with Unreal 5.0.3.

I want to pass the Player’s Name to the server. First I set it when the player is standalone, and after that the player will join a session on the server.

I have tried to retrieve the PlayerState after the player has joined the Session and it has reset all its values.

I think I can store it on client’s GameInstance, but I don’t know how to restore the saved PlayerState in the Server.

How can I do it?

By the way, I will do it first with blueprint and after that with C++ (I’m learning).

Thanks.

If you already have your player name variable inside of the player state class setup for replication, add to your Player Controller’s Begin Play event to call a custom event RPC (must be set to ‘runs on server’), that will set the value of the player state playername variable.

This will then trigger replication as normal, and the player’s name will be replicated to all clients. Grab the value from your local client’s game instance and pass that along as the event param if that’s where your player is keeping the name before joining.

FYI you can hook into other events to trigger this, you might prefer to use GameMode->PreLogin and PostLogin if your player passes along your custom playername as a URL join variable.

FString playerName;
FString Address = FString::Printf(TEXT("serverIpAddress:7777?playerName=%s"), *playerName );

I’d suggest having the GM “request” the data at the Post Login point.

Sorry, I don’t understand your answer because I’m joining a Session, not using an IP address.

1 Like

I don’t why it is so difficult to find a solution of this if everyone has found this issue dealing about how to pass data to a server when the player travels there.

There’s many different ways to accomplish tasks like this. Rev0verDrive’s suggestion to ‘request’ the player’s custom name after PostLogin is likely the easiest to get the job done for your scenario since you’re not using a URL.

Is the PostLogin event on the GameMode?

I’ve tried PostLogin event on the game mode and the GameInstance for the player that acts as the server is lost. So, I can’t get the data stored.

And, if I parse the options in the GameMode OnPostLogin, the other client (the one that join the session on the listen server) parses it too, so both player has the same PlayerName.

How are you handling spawning in the game mode? Have you overwritten the game modes spawning functionality? If not you should.

@Rev0verDrive

Spawning? I don’t understand that.

This is the only thing I do in GameMode:

But this code runs twice: once from the player that runs as a Listen Server, and once for the player who joins the session in the listen server.

I don’t think I’m spawning anything or handle it.

Hi there! I’m new to gamedev and new to UE, but I’m running across a similar issue.
The core of what I’m finding is that when your player joins a server, a lot of your classes are going to basically suffer amnesia:

  1. Your game instance is going to be reset. It will be the same game instance, but all the variables you store there will be set back to what they were hard-coded at. The reason for this is that the GI needs to be in a state that the server expects in order for the server to predict it’s behavior.
  2. Player Controller? Same thing. The server has to know what’s logging on, so it gets destroyed and rebuilt.
  3. Player State? Same thing. Destroyed when they client leaves their own session and built and assigned by the server, then attached to the new player controller that was built and assigned by the server.

The only way that I can find to pass a variable from a client-only game into a dedicated server is by using a save file.

I’m in the middle of trying to make this work, and I’m having a lot of trouble with it, so I’d love somebody who’s involved in the industry to chime in and clarify how this is supposed to be done.

But, please, if you’re just going to tell me to have the server ask the player controller for the variable like the first couple of comments, you’re not really listening. The variable is no longer there after joining, by design.

When you boot your game you typically load a Lobby map which has its own GM, GS, PC, PS etc. When you join a server you get a new controller, map etc.

Any settings changes done while in the lobby must be saved to file… e.g. Save Game.

When you join a server the GM creates a controller and replicates it to you. Once you get it and have it fully loaded you can fire off an event that passes “Save Game” data to the GM/GS/GI etc.

1 Like

I appreciate this, but my problem is that I can’t figure out how to fire off the event and have the server receive the variable. I know it sounds simple, but at this point I’ve tried BP_Interfaces, Event Dispatchers, Casting from either direction to either direction, and each of these methods going through the Player State. I get the same thing every time: null variables (assuming I get it to respond at all).

Even when I don’t save a class reference, but instead convert it to a string, and use a map and a return map do code and decode it, I can’t get the variable to the server.

I’ve done this going through the Game Instance as well.

I think that the Server’s copy of the player controller is overwriting the player controller, and that’s true for the other classes as well (ie, the server’s copy of the Player State is “correcting” the client’s copy of the Player State).

I’m not sure why it’s happening in the Game Instance as well. Maybe I’ll revisit that one too.

I just wish I could find some documentation on how I’m “supposed” to load a saved game into a dedicated server.

When the server replicates the “New” controller to you… It’s actually loaded on the client. Open the Save Game in your clients controller, get the data you need. Then RPC to controller on Server. Have it pass the data on to the GM, or PlayerState.

Player Controller Class

Game Mode Class

Execution needs to happen only on the Autonomous Controller (Client-side).

1 Like

Thank you so much for your help. Between this and the compendium, I really should be ready. You’ve been a huge help.

*edit: IT’S ALIIIIIIIIVE!!!
I seriously can not thank you enough, Rev. Call me out and I’ll get you tix to Horton Heat next chance. tytytytytyty