GameInstance in multiplayer

From my understanding the Game Instance is an object that is created at the start of each ue4 game executable regardless of whether it is a client or server. The Game Instance exists for the life of the executable. (https://docs.unrealengine.com/en-us/Gameplay/Framework/GameFlow) (Blueprint Game Framework Basics | Live Training | Unreal Engine - YouTube)

The Game Instance on each executable is not replicated but one will exist on clients and servers.(https://answers.unrealengine.com/questions/206758/variables-on-the-gameinstance-are-not-replicated.html)

If you join a multiplayer game, each client will have its own game instance since a separate game instance exists for each executable and it is not replicated.

With that if you call Get Game Instance on a client then you will get that client’s game instance.

1 Like

I just want to make sure if I understand it correctly.

So when I start the game a game instance is created.
If I join in a multiplayer game, each client will have its own game instance?
And the getGameInstance node will get the client’s game Instance when its called ?

You are mostly correct, OP.

Game instance created when game starts and lasts until that game executable closes, persisting across level changes and joining sessions etc.

Game instance is only aware of its own executable. Does not have any connection between server and clients.

When I need to persist something from one level to another for example when a client joins a server’s level then I put it in game instance to save it for after the level switch. Then after Game state starts on the newly joined or opened level I have it grab anything it needs to inform all clients about from the Game Instance and can tell the server via playercontroller RPC to set those vars or multicast a function to everyone about it.

1 Like

So does that mean when a server launches a dedicated server , that the dedicated server itself would have a game instance?.. that only it has access to?

that you could essentially call on the game mode of your server map using authority to pull say server player saves from and load them from player input of say a name for player save slot

I searched for a proper answer to this eaxct question too - but couldn’t find anything, so after 9000 browser tabs I’ll asume that’s the case, until proven otherwise.

The GameInstance is alive as long as the application is alive, pretty much as its name suggests, it’s an instance of the game. With that said, when a dedicated server is launched, a GameInstance will be created in response (on server). The GameInstance on server has no clue about the GameInstance that exists on clients, which gets instantiated the moment you launch the game, and is destroyed once you exit and close the game instance.

Quick recap: Every server instance has his own GameInstance, and every client instance has his own too.

There’s a high possibility that searchers for this thread might want to check how they can persist data between travels.

4 Likes

Great resource, thank you for creating this!

I think people get confused when they read “store data on game instance when chancing level for multiplayer”, while they are also learning about what is shared/replicated and what is not.

To make it simpler, Game Instace is the executable that is running on your own computer.
So if you open “MyOwnGame.exe” that will create a game instance, if you go and open it again myowngame.exe that will go and create another game instance, that is completelly isolated from the first. Same thing happens between “server and client”. The server .exe has its own game instance, while the client .exe has its own instance.

For new unreal joiners, when reading about stuff that exists in both server and client, I think for most, the game isntance seems to behave like server and client, similar to the player character or player controller, that exists in both server and client and are somewhat linked.

But that is not the case for game instance, yes it exists in both server and client, but there is no link between them, server has its own game instance while each client has its own game instance.

The best way to visualize it, create a brand new third person control game, create a new game instance, and set the project to use this game instance, and set multiplayer net mode to “play as client”, and number of players to 1, on init method of your game instance, set a guid to a variable string, like:

now on your third person character, on begin play, you can print your guid from the game instance.
Since this will fire on server and on client, it will output different values since each client has its own game instance.

this will play like:

in fact you don’t even need the branch, since each side will run its own version, it is exactly the same code, but two different values

Why one would use game instance? Mostly for configuration, things that will change from one computer to another. Like your own controller mapping, server has no idea what your control mapping looks like.

Or you are switching a map regardless if it is single player or multiplayer, and you have data on the game mode you don’t want to lose, you can store it in the game session.

Like, from the server perspective, switching maps, the info on game mode is lost, on the server game instance, the server can read the info from game mode, store in its game instance, load a new game mode, and set the values back on this new game mode. During all this, each client game instance will remain unchanged, they have nothing to do with the server game instance.

The only exception to this would be shared split screen multiplayer. Is not an exception, but I bet some will get confused, so when launching in a split screen, there is only 1 game running, so it will only have 1 game instance, regardless of the fact that there are two players characters.

So repeating the experiment above with a shared splitscreen, will in fact output the same uuid for all players, since they are all sharing the same game instance.

A fun experiment to do, if you really want to sink this down.
Configure your game to add 3 local players, while having the net mode to play as client, and number of players to 2.

This will run 2 clients + server, so it will have 3 game session, while also having 8 players