Best way to transfer data between multiplayer maps

Hey I’m trying to figure out how I would create a character selection screen, where each player picks a character while in the lobby before the game starts and then the game spawns their pick when the game starts on a different map.

Im trying to save the picked character class in the player state, but when its time to read it in the player controller and actually spawn the character it turns out empty.

Inside Widget when the character icon is clicked.

In the custom player state BP
image

In the player controller

The variable is populated in the custom player state, but goes empty in the player controller.

The cast is successful so I’ve no idea whats going on.

Any help appreciated :slight_smile: Also If there are better ways of doing this I’m open to suggestions.

EDIT: Using the game instance to store the variable was better. See the posts under :slight_smile:

1 Like

The GameInstance class is where you will handle loading maps and any persistent data. Everything else gets destroyed when a new level is loaded:

1 Like

I tried using the game instance to store the character class, but if I understand correctly it is not replicated, so when I try to spawn the character on the server only the character the server picked is spawned for all of the players.

I’m not sure if there’s a different way to handle it in modern Unreal, but in UE2 and UE3, the server would be sitting in the map, and the player controllers wouldn’t be ready to start until after they had selected their characters. So, you don’t tell it what you want to spawn in a separate level, you just have it selected locally in the level you’re going to play, and then you start with that one.

In the lobby, store the selected character in each player’s local game instance. Then, when the new map is opened, have each player controller get their selected character from their game instance, then send it to the server before spawning.

Here I try doing it using the game instance to store the Selected Character variable

1 (In the Lobby map) Character is selected in the widget and stored in the game instance

2 (In-game map) Each Player Controller gets the variable from its game instance

3 (In-game map) Characters are then spawned on the server I run the variable into the SpawnPlayer_OnServer event and use it to spawn actor

4 In game, while in the lobby, I pick Blue Character on the client and Red Character on the server, but both characters spawn as red on the In-game map. Log comes out like this:
image

Where does it go wrong? The server reads the PC Selected Character variable as Red character when I call it on the OnServer event, yet it shows as the correct character when i print the string after in the player controller. I cant understand why or what to do about it. :face_with_thermometer:

Looks like the Server is spawning a character for the client. Since server’s game instance has red character, both will be red. Try a branch with a “Is Local Player Controller” node before the cast on BeginPlay, and only set/spawn the selected character if true.

Sweet! This worked. Such a simple solution, thank you!!

1 Like