How to use the Game Instance in Unreal Engine 4

The Game Instance is one the most important blueprint classes in an Unreal Engine 4 game.

In this guide we will go through what the Game Instance does and how to use the game instance in Unreal Engine 4.

game instance project settings value

What is the Game Instance

The game instance is a manager class that is not destroyed when changing levels in your game.

This means data such as player health, score, ammo and more can be stored between levels.

The data is kept as long as the game is running.
Once you close your game the data is lost!

Casting to the game instance created later in the guide

Creating your Game Instance

Creating the Class

To create the Game Instance class, right click anywhere in your content browser at the bottom and click ‘Blueprint Class’.

Creating a new blueprint class

A new dialog box will show and ask which parent class you wish to make.

List of blueprint classes to potentially create

To create the Game Instance class we need to click the ‘All Classes’ drop down at the bottom.

Selecting all classes to spawn

Clicking this will now show all the classes we can create.

Listing all blueprint classes to potentially create

In the search box type Game Instance, click the GameInstance text and then click select to create our new Game Instance.

Selecting the game instance class to create

Now after naming your new Game Instance, it will be shown in the content browser.

Renaming your new game instance class

Adding Variables

Firstly, to add variables to your new Game Instance, simply double click to open the blueprint editor.

Opening the game instance class

On the left hand side click the + Variable button to add a new variable.

Creating a new variable

For this example I have created a Float variable called Health that has a default value of 100.

Creating the health variable

When you are done creating your variables make sure to compile and save!

Compiling and saving the new changes

Accessing your Game Instance

Configuring the Project Settings

Firstly, before you can access your game instance, you will need to configure the game to use the new Game Instance that we just created.

This is a very simple step and only takes a few clicks.

Click edit in the top right, then click on Project Settings.

Opening the project settings

Once the Project Settings window opens, search for the Maps & Modes option and click on it.

Opening the maps and modes section

At the bottom of the Maps & Modes section look for the Game Instance Class drop down box.

Click this box and select your new Game Instance class. In my case, I named mine GameInstanceDemo in the previous step.

Configuring your game instance into the project

Accessing and Setting the Variables

To start using your Game Instance right click in any of your blueprints (characters, actors etc) and type get game instance.

This will retrieve the game instance with your set variables.

Get the game instance through a blueprint node

From the pin of the Get Game Instance node create a cast node to your newly created Game Instance Class.

This cast node is named the same as your Game Instance set earlier.

Casting to the game instance

From this new cast node you can set and get the values of your Game Instance variables.

Get the health and setting the health variable values

In this example I will set the health value to 50 and load a new level which will print out the value. This value will stay the same as the Game Instance values are kept over different levels.

Setting the health variable to 50 and opening the next test level
Blueprint scripting for the first level
The second level blueprint code containing a print string to show the value of health on the screen
Blueprint scripting for the second level

Loading the second level will print out the value of Health from our Game Instance. As this value was set to 50 in the first level the print string node will print the value 50 on the screen.

The value 50 printed on the screen
Result of testing the Game Instance

Conclusion

Now that you have the knowledge to use the Game Instance in your Unreal Engine 4 game, you can now adapt this to your user interface through the widget system or onto your player character.

Doing so will give you the ability to switch levels without losing player values such as health, ammo and quest progress.

Those are only a few examples of what the Game Instance can do but there are many more ways to use saved values.

More Complex Uses

Using Discord Rich Presence in your game can be easily accessed by storing the Discord object in the Game Instance.

Click here to learn how to add Discord Rich Presence into your Unreal Engine 4 game.

Be the first to comment

Leave a Reply

Your email address will not be published.


*