What should I do in GameMode, GameState, and PlayerState?


I’m creating my first game in UE4, and I’m just sorting in my head what the above-mentioned blueprint classes should do. I know that a lot of people say there’s no set way to do things, but I’m trying to understand what these classes are intended for. My guess is that GameState and PlayerState should really be used to hold values and not implement any behaviour as such, whereas the GameMode should contain most of the game logic and be used to tie the player, level and HUD .etc. together. GameState would hold variables relating to the level itself (i.e. not to any particular player), whereas the PlayerState would hold things relating to a particular player in the world (such as health, ammo .etc.).

Am I thinking along the right path here, or have I got it completely wrong?

In a Single Player Game it doesn’t really matter, since the Local Maschine is the Server.

It however makes a difference in Multiplayer.
The GameMode sets the Rules (Score needed to something to happen). It’s the basic set of rules (For Example: Football) only the Server can access it.
The GameState handles the actual Game (Check if Scored). (For Example: The Referee, or Playfield). Everyone can try to score and the Server decides whether you scored.
The PlayerState is replicated to everyone but only the owning client or the server can change something in it and contains Mode-specific data (For example : The goals you were responsible for or your name or shirt number).
I recommend reading @eXi’s Compendium:

GameInstance (an object spawned when you launch the application and that remains the same until you close it)
GameMode (spawned when a level is loaded)
GameState (spawned by the gameMode)
PlayerState (spawned when a PlayerController is spawned = when a player arrives in the game)

You have to pay close attention to what you do and where you do it when coding a multiplayer game. When it comes to singleplayer, you can’t really “have it wrong” except for GameInstance stuff.

But here are the general guidelines I follow:
GameInstance - Holds any non-pointer persistent variables (persistent means that you need to store in between two levels and that you don’t need to store in a SaveGame)

GameMode - The overall game manager - starts and stops the current game space you’re in, handles the GameStates and how they rotate - an example might be “King of the Hill”

GameState - Keeps track of every data relative to the current state of the game (timers, scores, winning team) that all players in the game need to know about, handles scripted events related to the state
For instance:
PregameState: Prevents player from performing any action, starts a timer and display it to everyone. When timer expires, ask KotH (KingOfTheHill game mode) to rotate to IngameState
IngameState: Enable player input, spawn a big loud noise and open players’ spawn gates. Open the Capture Point and store the amount of capture time both teams have. When one of the team reaches max score, asks KotH to switch to EndgameState.
EndgameState: Destroy every player’s characters and starts a cinematic showing the PlayOfTheGame then asks KotH to rotate to ScoreGameState etc.

PlayerController - HUD, Camera, Mouse, Keyboard, Gamepad, Inputs calling actions on the Character.

PlayerCharacter - Actions in response of Controller’s input + Holds personal infos and stats (Health, Ammo - but Ammo might on your Weapon Class if you can switch Weapons).

PlayerState - Holds every variable non related to the PlayerCharacter that needs to be known by everyone (best scores, current killing streak, player name…).

Thanks for clearing this up for me guys! :slight_smile:

Yun-Kun - Let’s just have your answer in the documentation :slight_smile:

Honestly this is the best explanation on the UE4 multiplayer framework. So short and so precise


Thanks alot man, now it’s incredible clear

Where would you put functionality like in-game time and date, upgrades as in a tech tree and in-game player cash amount?

If you are going for a single player game, I would suggest putting these variables in a SaveGame file utilizing your Game Instance. However, for multiplayer, you would need to store those in a Database and retrieve them using the Game Instance. From there, I would utilize in-game time within the Game State and the other variables within the Player State.

You said - “Keeps track of every data relative to the current state of the game”
Do you mean that I need to spawn every current GameState (replace current GameState) or write all states in one GameState?

I meant having a parent class “GameState” containing all the logic and utility functions which are going to be shared by all your game states.

Then create child classes of your parent “GameState” which have their **specific **features.

Then spawn the proper state when required.