Course contentsShow
← Unreal Engine Game Development
Lesson 116 of 8802. The Gameplay FrameworkFree lesson

Defining Match Lifecycle States

You will implement a state machine within the GameMode to transition between waiting, playing, and finished match phases.

In this lesson, you will learn how to orchestrate the flow of your game by using a state machine to track whether the match is waiting for players, currently in progress, or finished.

Why Use a State Machine?

In Unreal Engine, the GameMode class acts as the referee of your match. You don't want players running around before the countdown finishes, nor do you want them to continue fighting after a winner is declared. A state machine allows you to categorize the game into distinct "buckets" and only execute specific logic when the game is in the corresponding state.

The Lifecycle States

We typically define three fundamental states for a match:

  1. WaitingToStart: The lobby phase where players join.
  2. InProgress: The active gameplay phase where input is enabled.
  3. WaitingPostMatch: The cooldown phase where results are displayed.

Implementing the Logic

Inside your GameMode, you can define these states using an enum and a variable to track the current phase. By using a simple switch statement or if checks, you can gate your gameplay functions.

// A simple enumeration for our match states
enum class EMatchState { WaitingToStart, InProgress, WaitingPostMatch };

// Inside your GameMode
void AMyGameMode::SetMatchState(EMatchState NewState) 
{
    CurrentState = NewState;
    
    switch(CurrentState) 
    {
        case EMatchState::InProgress:
            RestartAllPlayers(); // Enable movement/input
            break;
        case EMatchState::WaitingPostMatch:
            DisableAllInput();   // Stop gameplay
            break;
    }
}

By checking the CurrentState variable before performing actions—like checking if a character is allowed to fire their weapon—you ensure that your gameplay mechanics respect the rules of the match. If the state is not InProgress, you simply return early from your logic, effectively "locking" the game until the state changes again. This keeps your code modular and prevents bugs where players might interact with the world during a transition phase.

Key Takeaway: A GameMode state machine acts as a gatekeeper that ensures gameplay mechanics only trigger when the current match phase allows them to.