Saving And Loading Game Data With Blueprints And C++ In Unreal Engine
November 22, 2021
No matter what type of game you are creating you will need a system that will save and load the data about the progress in your game.
In this post we are going to take a look at how can we create save/load system with Blueprints and C++.
Important Information Before We Start
This tutorial is not for complete beginners. To follow this tutorial you will have to know your way around Unreal Engine, Blueprints and C++.
It is not required that you are an expert in Blueprints and C++, but it is required that you know the basics, and you are comfortable creating simple games using Blueprints and C++.
For this tutorial I am using the ThirdPerson template project, so if you want to follow along with the tutorial create a new project and select the ThirdPerson template so that you have the same set up as I.
Modeling Game Data Blueprint
Give the new Blueprint name GameData. Now open GameData Blueprint in the editor, and under My Blueprint tab create a new variable type of Integer and name it score:
Create A Custom Game Instance Blueprint
The one who is going to be responsible for saving and loading game data is our game instance. The reason why I am using game instance for this task is because can carry our game instance across our whole game, so no matter in which map we are in, we can use the game instance to save and load game data.
To create a game instance Right Click -> Blueprint Class, and in the search bar type game instance:
Name the new Blueprint BP_MyGameInstance. Open BP_MyGameInstance Blueprint in the editor and in My Blueprints tab create two variables, one called Save Slot Name type of String, and the other Game Data type of GameData Blueprint that we created.
Select the Save Slot Name variable, and in the Details tab under Default Value type GameData:
Initializing Game Data
In the Event Graph tab of BP_MyGameInstance Blueprint we are going to initialize our game data. This means that when the game starts, we are going to check if the game data exists, if that is true we will load the game data, if the game data doesn’t exist, that means we are running the game for the first time so we need to create a new game data.
This is how we are going to do that:
Creating Functions That Will Save And Load Game Data
Even though this example is simple, it is always a good practice to create functions that will save and load game data, because if you don’t, then you will be creating the same code in every Blueprint where you want to save game data which will lead to duplicate code and unnecessary work.
In My Blueprint tab under functions create two functions. Name one SaveGameData and the other LoadGameData:
The SaveGameData function will have a parameter of type of Integer called New Score:
This will allow us to pass a new integer value to the function and it will save it without the need for us to get a reference to Game Data variable. Here is the Save Game Data function:
And this is the implementation of the LoadGameData function:
Saving And Loading Game Data
I have also created a simple UI widget that has a text that will display the game data and a button that will save the game data:
In the My Blueprint tab under variables I have created a reference to the game instance type of BP_MyGameInstance:
And from the Event Construct node I will get a reference to the game instance:
You can copy the nodes from here:
I have also created a function called Set Score that is bound to the text widget which will display the score:
You can copy the nodes from here:
The Set Score function will call the Load Game Data function from our game instance and it will take the returned value and pass it to our text widget.
Lastly we have the On Clicked event for our button:
You can copy the nodes from here:
Essentially when we press the button we are going to generate a random Integer from 0 to 100 and save it with the help of our game instance.
It is not mandatory that you create the same set up, but I am using this to demonstrate how saving and loading game data works.
Since we need to create the widget and display it in our game, I am going to create a new GameMode Blueprint, so Right Click -> Blueprint Class and in the search bar type game mode:
Name the new Blueprint BP_SaveLoadGameData_GameMode and inside Edit -> Project Settings -> Maps & Modes for the default game mode set our newly created game mode Blueprint:
Now we can test the game:
Every time we pressed the button we generated a new random Integer that was saved, and when we stopped and run the game again that same data was loaded and displayed with the help of the text widget.
Saving Player’s Location
The first example was a simple one that we used to demonstrate how saving and loading game data works, now we are going to save player’s location inside the game and next time when we play the game we can continue from the player’s last location.
To do that, first we need to create a Vector variable inside the GameData Blueprint and name it Player Location:
Inside the BP_MyGameInstance Blueprint I am going to create two functions, one for saving the player’s location called SavePlayerLocation and one for loading player’s location called LoadPlayerLocation:
You can copy the nodes from here:
Then, from BeginPlay we are going to get a reference to BP_MyGameInstance:
You can copy the nodes from here:
When we press the E button on the keyboard we are going to save the player’s location, and when we press the R button we are going to load the player’s location:
You can copy the nodes from here:
Let’s run the game and test it out:
Every time we press the E key we save the player’s position in the game, then no matter where we are in the level, when we press the R key, the player will be moved to the saved position. This mechanic can be a nice idea for a game