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

Implementing Win and Loss Conditions

You will create a robust checking mechanism that evaluates GameState variables to trigger game-over sequences based on defined match rules.

In this lesson, you will learn how to create a centralized checking mechanism within your GameState to determine when a match should conclude.

The Role of GameState in Match Flow

Up to this point, you have learned how to track statistics and synchronize global state. However, data alone doesn’t end a game. To implement win and loss conditions, you need a "referee" that constantly evaluates the current match rules against the data stored in your GameState.

Because the GameState is replicated and accessible to all clients, it is the ideal place to host your match-rule logic. You don’t want your character or controller to decide if the game is over; instead, you want the GameState to broadcast an "EndGame" event once specific criteria are met.

Implementing the Checker

The most robust approach is to create a function (e.g., CheckMatchRules) that runs whenever a relevant variable—like a team score or player health—is updated.

Here is how you might structure the logic in your GameState:

void AMyGameState::CheckMatchRules()
{
    // Define your win condition: First to 10 points
    if (TeamAScore >= 10)
    {
        SetMatchState(EMatchState::WinnerTeamA);
    }
    else if (TeamBScore >= 10)
    {
        SetMatchState(EMatchState::WinnerTeamB);
    }
}

Triggering the Sequence

Once the condition is met, SetMatchState triggers a lifecycle transition. By utilizing the logic you learned in "Broadcasting Match Events," this state change tells your UI widgets to stop accepting input, displays the "Game Over" screen, and prevents further scoring.

By keeping this logic centralized in the GameState rather than scattering it across different Actors, you ensure that the game rules are applied consistently across the network. If a player reaches the goal, the GameState updates, the state machine shifts, and all connected clients receive the command to show the results screen simultaneously. This prevents desyncs where one player thinks the game is still running while another sees the victory screen.

Key Takeaway: Centralize your win and loss logic within the GameState so that all clients evaluate the same authoritative conditions, ensuring a synchronized and bug-free end-of-match experience.