Learning Unreal 5: How to Use Game Modes, States and Instances

In the course of working on my first game in Unreal, I found myself running into an issue – where to store variables and logic that should exist within specific maps, or levels? The level blueprint seemed like the obvious place, but later discovered that it’s not easy to access from other blueprints, which made updating the UI with level specific data challenging. Most tutorials I saw online put this logic, a timer and some variables in my case, in the player controller. But my programming experience screamed at me – that’s a violation of the Single Responsibility Principle! Surely Epic must’ve put something built in to help with that.

Enter Game Mode, Game State and Game Instance.

In my search for A Better Way for my single player game, I ran across Game Modes, Game States, and Game Instances – additional blueprint class types beyond the common ones readily displayed when creating a new one in the engine. Modes and Instances perfectly solve my issue.

Game Modes

Game modes are a class that you can store mode specific logic and data in. By default, all new levels use the BP_FirstPersonGameMode, BP_ThirdPersonGameMode or similar, depending on which template was selected on creating the project. This can be changed on the project level impacting all new levels, or set strictly level-by-level.

Any data stored in the game mode is reset each time the level loads – so it’s not a good place to keep anything persistent, but for anything that needs tracked just for the duration of the level, it’s perfect.

In the game I’m currently creating, I have two levels that each have different game play loops. So utilizing Game Modes, one for each level, makes perfect sense in separating the level specific logic that I’ll need access to for UI purposes. For one level, I’ll be using game mode to track a quota the player needs to hit, as well as some logic needed in order to achieve that quota.

How to create a new game mode

  1. Right click on the content drawer and create a new Blueprint class.
  2. In the Pick Parent Class window, expand the All Classes section and search for game mode.
  3. Select either GameModeBase or GameMode. The primary differences as I understand is that GameModeBase is simpler and focused on single player games, whereas the GameMode option is designed for multiplayer and has some additional features for multiplayer.
Example of how to search for the game mode blueprint class in the Pick Parent Class window.

How to change the default game mode

This default can be changed under Edit -> Project Settings -> Maps & Modes -> Default Game Mode.

How to change the game mode of a specific level

  1. Load the level in question.
  2. Go to World Settings panel (if not visible, got to Window -> World Settings).
  3. Set the GameModeOverride to the new Game Mode blueprint.
Example of the Game Mode options in World Settings.

Important! Make sure to set the DefaultPawnClass and PlayerControllerClass to the correct BP classes. In my case it defaulted to a generic class and I gained the ability to fly!

How to access the data in the game mode

Accessing data from the game mode is pretty easy, especially if you use Interfaces.

  1. Create an interface. Right click on the content browser, go to Blueprints -> Blueprint Interface. Name it BPI_TestInterface.
  2. Create a function in the interface, testFunction and give it an output of an int variable, number.
  3. Save, then go to the Game Mode file and under Class Settings add the BPI_TestInterface.
  4. Compile, then implement the testFunction. For the sake of this, you can have it just return a number.
  5. Go to another file, such as a UI Widget and get the value of testFunction by using a Get Game Mode node and calling the testFunction.

For example, in my game I have an interface called BPI_MiningLevel. In my GameMode, I have a quota value. In order to display this in the UI, I have a UI Widget where I call the Get Quota Count function I implemented from the BPI_MiningLevel interface and set the value to the UI Text string, as seen below:

Visual example of calling the Game Mode interface implementation in order to set a value in the UI.

For more examples in how to set up a game mode, check out Unreal’s guide to setting up a Game Mode.

Game Instance

A game instance is a great companion to game modes. While you can have multiple game modes, you can only use one instance. It can be used to track data you need to persist across levels. Game instances spin up when a game is launched, and is accessible until the game is closed.

In my game, I have some data I want to track across multiple levels. These can be modified from both locations as well – I may decide to put them into the game instance.

How to create a new game instance

Like the Game Mode, the game instance class is found in the All Classes section of the new blueprint.

  1. Right click on the content drawer and create a new Blueprint class
  2. In the Pick Parent Class window, expand the All Classes section and search for game instance.
  3. Select GameInstance.

Game State

Game state came up a few times as I was looking into game modes and instances. The various resources I was using, annoyingly, didn’t cover it clearly. So I turned to the official docs, which are actually pretty decent. It seems like the intent of game states is primarily for multiplayer games. There, it’s considered a server side function that informs the local clients of the overall state of a game and keeps all players in sync.

The Game State is responsible for enabling the clients to monitor the state of the game. Conceptually, the Game State should manage information that is meant to be known to all connected clients and is specific to the Game Mode but is not specific to any individual player. It can keep track of game-wide properties such as the list of connected players, team score in Capture The Flag, missions that have been completed in an open world game, and so on.

Unreal Docs on Gamestate

I may be able to include the variables I want to track in Game State rather than Game Instance. Following nomenclature that may make more sense, although for the sake of a single player game, I doubt that it matters much.

How to create a new Game State

  1. Right click on the content drawer and create a new Blueprint class
  2. In the Pick Parent Class window, expand the All Classes section and search for game state.
  3. Select GameState.

Additional Resources

For further learning, here are some of the resources I used when investigating this topic:

Beyond Blueprints

Game modes and instances are also available in C++, with some additional features not made available to blueprints. When I get into using C++ in Unreal, I’ll be sure to dive into those differences as well.