Storing values between game sessions can be done with the Save Game object in Unreal Engine. Much like with other objects, we create a custom subclass of it, give it variables that we want it to remember, then populate those before the game ends (and retrieve from it when our game begins). Let me show you how this works in a quick example.
Creating a Save Game
We create the Save Game by creating a regular Blueprint Class, then search for Save and the object will come up. I’ll call mine SG_HighScore.
Open it up and add variables like on any other blueprint. That’s all we need to do here.
Retrieving Data from the Save Game
Save Game data is stored in “slots”, and we can have as many per save game as we like. When our game begins, we can check if a slot exists, and if it does, retrieve values from it. Begin play is a good place for this. You can type in the Slot Name or promote the value to a variable to avoid spelling mistakes.
If we have a Save Game does exist, we can cast to our custom Save Game class and retrieve values like this. It’s very similar to accessing properties on other classes we cast to.
Storing Data in the Save Game
The stashing process is a little more intricate, but only marginally so as we need to cater for the “first run” scenario. Here we as, does a save game with our slot exist, and if so, we retrieve data. If not, we go ahead and create it.
On the creation (false) branch, we can first populate any variables on the save game we want to stash, and then call the Save Game to Slot function. This will store the data in the target platform’s persistent storage, ready for retrieval in the previous step.