Unreal Engine Game State and Game Mode

GameIn any game, there are game rules that need to be tracked such as how many players can join a game, where they spawn in the game, and what actions they game perform.  There are also things the game needs to keep track of, such as the completion of certain objectives, or changes to the game like placing items, building or destroying things, that need to be shared with other players.

In this post, I am going to explain the purpose and use of two objects in Unreal Engine 4: Game State and Game Mode.

Game Mode

There are now two built-in classes in Unreal Engine 4 that handle the Game Mode.  They are GameModeBase and GameMode.  GameModeBase is the ultimate base class for all GameMode objects.  The internal names of the classes are AGameModeBase and AGameMode.  AGameModeBase contains basic game mode functionality and AGameMode contains additional capabilities that are applicable to standard shooter type games and adventure games.  If you need more specialized behavior you can create your own game mode from either of these base classes and add the additional capabilities to the game mode.

A GameMode object created for each level of a game and each level can have a different game mode.  Game mode objects can also be switched out depending on what needs to change in the game.  To switch game modes you can select the Override Game Mode in the world settings for a particular level, this will cause the game mode to be changed when the player enters the new level.    Also, the GameMode, in a multi-player game, is not accessible by clients, it is handled entirely by the server.  However., it is possible to share and synchronize the game mode information with clients using the GameState object (see below).

The AGameMode class contains a variety of functions and events:

  • InitGame
  • PreLogin
  • PostLogin
  • HandleStartingNewPlayer
  • RestartPlayer
  • SpawnDefaultPawnAtTransform
  • LogOut

Game State

The game state (AGameState) is not specific to any player but allows all players to monitor the state of the game.  This is the place where all important states the effects gameplay should be stored except for that state that is player specific.  I will cover the PlayerState in another post.

The base class AGameStateBase contains functions for obtaining the server time, an array of players, and whether or not gameplay has begun.


  • GetServerWorldTimeSeconds
  • PlayerArray
  • HasBegunPlay
Additionally, you can keep track of any other state values your game requires.  In upcoming videos, we will explore the use of GameState in detail.





Comments

Popular posts from this blog

Introduction to Unreal Engine 4