Saving And Loading Game Data With Blueprints And C++ In Unreal Engine

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

To demonstrate how saving and loading game data works let us create a basic example that will save and load an Integer variable.
 
The first thing we need to do is create a SaveGame Blueprint. I have created a Blueprints folder in my project and this is where I am going to store my Blueprints, you will of course store yours in the appropriate folder for your game.
 
To create the SaveGame Blueprint, Right Click -> Blueprint Class, and in the search bar type savegame:
 
Img 1

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:

Img 2

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:

Img 3

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:

Img 4

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:

Img 5
You can copy the nodes from here:
 

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:

Img 6

The SaveGameData function will have a parameter of type of Integer called New Score:

Img 7

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:

Img 8
You can copy the nodes from here:
 
The LoadGameData function will have a return value of type Integer:
 
Img 9

And this is the implementation of the LoadGameData function:

Img 10
You can copy the nodes from here:
 

Saving And Loading Game Data

To make this work, first we need to set our BP_MyGameInstance Blueprint to be the default game instance. To do this, we need to go in Edit -> Project Settings -> Maps & Modes and at the very bottom you will see Game Instance option, for the Game Instance Class select our BP_MyGameInstance Blueprint:
 
Img 11

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:

Img 12

In the My Blueprint tab under variables I have created a reference to the game instance type of BP_MyGameInstance:

Img 13

And from the Event Construct node I will get a reference to the game instance:

Img 14

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:

Img 15

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:

Img 16

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:

Img 17

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:

Img 18

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:

Img 19

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:

Img 20
Img 21

You can copy the nodes from here:

To save and load player’s location we need to get a reference to BP_MyGameInstance, and we are going to do that inside the ThirdPersonCharacter Blueprint(I am using Third Person Project Template). First we are going to declare a variable type of BP_MyGameInstace:
 
Img 22

Then, from BeginPlay we are going to get a reference to BP_MyGameInstance:

Img 23

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:

Img 24

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

Leave a Reply

Your email address will not be published. Required fields are marked *