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

Handling Team-Based Scoring Logic

You will group PlayerState instances into teams and aggregate scores at the GameState level to support team-based victory conditions.

In this lesson, you will learn how to transition from tracking individual scores to managing team-based victory conditions by aggregating data within the GameState.

Moving Beyond Individual Stats

Previously, you learned to track individual scores within each PlayerState. While this is great for solo play, team games require a higher-level "manager." We use the GameState for this because it acts as the single source of truth for all clients, ensuring everyone sees the same score regardless of which player earned the points.

Identifying Teams

To group players, we first need a way to categorize them. You can add a TeamID (an integer or enum) to your custom PlayerState class. When a player performs an action that grants points (like capturing an objective), the server-authoritative logic identifies the player, looks up their TeamID, and tells the GameState to update the total for that specific group.

Aggregating at the GameState

Inside your GameState class, you should maintain an array or a map that stores the total score for each team. Because the GameState is replicated, these values are automatically sent to all clients.

Here is a simplified look at how you might structure the aggregation logic on the server:

// Inside your custom GameState class
void AMyGameState::AddScoreToTeam(int32 TeamID, int32 Points)
{
    // Ensure we are only calculating this on the server
    if (HasAuthority())
    {
        TeamScores[TeamID] += Points;
        
        // Trigger a UI update event for all players
        OnTeamScoreChanged(TeamID, TeamScores[TeamID]);
    }
}

Why this works

By keeping the master list in the GameState, you create a clean separation of concerns:

  1. PlayerState keeps track of "who" did the action.
  2. GameState keeps track of "how many" points the team has earned.

This prevents desync issues where different clients might think the score is different. When the GameState updates, the UI widgets linked to it will automatically reflect the correct team totals, providing a consistent experience for every player in the match.

Key Takeaway: Use the GameState as the centralized authority to aggregate individual PlayerState contributions into team totals, ensuring consistent scores across all network clients.