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).
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.
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.
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.
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:
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.
Player Controller? Same thing. The server has to know what’s logging on, so it gets destroyed and rebuilt.
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 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.
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.