Creating a Custom Game Mode
You need a place to store your game’s logic and behaviors. In UE4, each level has its own Blueprint, which is one place to store game logic, but putting too much scripting in the Level Blueprint means a lot of copying and pasting down the road to transfer that logic to new levels and maps. Instead, UE4 has the concept of a Game Mode. Like Level Blueprints, Game Modes can store complex behaviors related to a game, but unlike with Level Blueprints, that behavior can be shared between multiple levels.
The Game Mode is responsible for defining the behavior of the game being played and enforcing rules. The Game Mode holds information about items a player begins the game with, what happens when the player dies or the game ends, game time limits, and scores.
Game modes are the glue between many of the different systems in a game. Game mode Blueprints hold the characters or Pawns you are using and also reference which HUD class to use, which spectator class to used, and the game state and player state classes that control the information necessary for multiplayer experiences.
At the most basic level, the Game Mode sets the rules of the current game—for example, how many players can join, how level transitions are handled, information about when the game is paused or active, and game-specific behaviors like win and loss conditions.
Creating a new Game Mode is easy. In the Content Browser, right-click and select Blueprint Class to open the Pick Parent Class window, which is where you can select Game Mode, as shown in Figure 20.2.
FIGURE 20.2 The Pick Parent Class window. This commonly used window offers several class options, including the Game Mode option you need now.
Now that you have a new Game Mode, you need to tell UE4 to load it instead of the default Game Mode. You do this in the Project Settings panel.
There is only ever one Game Mode present per level—either the default Game Mode set in the Project Settings panel or the Game Mode set on a per-level basis. In a multiplayer game, the Game Mode only ever runs on the server, and the results of the rules and state are sent (replicated) to each client.