The problem often arises when you want to bring your players into a new map in a networked game. The problem being, you lose all your data from the previous map. Let’s say you had earned some gold from slaying monsters in level 1 and now you want keep that gold in level 2, which is a separate map. In Unreal, you can choose from 2 options when loading maps:
- Hard – blocking operation, disconnects players and connects them back when everything is loaded
- Seamless – async operation, puts players in a transition map without disconnecting them until everything is loaded
Generally, in single player games you don’t care about disconnects so you can just hard load every time. But in multiplayer games, the disconnect can be pretty jarring. The obvious solution then is to use seamless travel. That’s where most tutorials end. Just use seamless travel for multiplayer. But what about all the information that gets destroyed? Most objects such as GameMode and GameState get destroyed when loading a new map, so you can’t store your precious gold in GameMode.
Luckily, there are ways to save information when you seamless travel. The easiest way is just knowing that anything attached to the player controller will not be destroyed and recreated. That means any actor components on the controller will remains just as they are. And for most people, this might be enough. But let’s say you have something like your GameplayAbilitySystem on your PlayerState. What can we do then? Specifically for player state, there is a function you can override called CopyProperties(). I looked in other places like GameState and GameMode, but this didn’t exist. With CopyProperties(), it takes in the new PlayerState as a parameter which you can then load anything you want to keep from your old (current) state into.
void ASPlayerState::CopyProperties(APlayerState* PlayerState)
{
Super::CopyProperties(PlayerState);
ASPlayerState* SPS = Cast<ASPlayerState>(PlayerState);
if (SPS == nullptr)
{
return;
}
SPS->Gold= Gold; // yay now I keep my gold when I transition levels!
}
If all else fails you can always tuck things into your GameInstance since those won’t ever get destroyed while you’re playing the game.