Synchronizing Global Game State
In this lesson, you will learn how to use the GameState class to ensure all players see the same match-wide data, such as scores and timers, in real-time.
The Role of GameState
While a PlayerController manages the input and logic for an individual player, the GameState class acts as the "source of truth" for the entire match. Because the GameState exists on both the server and all connected clients, it is the perfect place to store information that everyone needs to see, such as the total team score, the remaining round time, or the current stage of the game (e.g., "Waiting for Players" vs. "Match in Progress").
How Replication Works
When the server updates a variable inside the GameState, that change is automatically pushed to all clients. This ensures that when a player scores, their UI updates to reflect the new total instantly.
To implement this, you simply mark your variables as Replicated in your GameState class:
// Inside your GameState header
UPROPERTY(Replicated)
int32 TeamAScore;
UPROPERTY(Replicated)
float RemainingMatchTime;
When the server calculates that a goal has been scored, it updates TeamAScore on its local copy of the GameState. Because of the Replicated keyword, the engine handles the networking magic to broadcast that new integer to every client connected to the session.
Why not use the PlayerController?
If you stored the "Total Team Score" inside a single PlayerController, other players wouldn't be able to "see" it easily, because a client’s PlayerController only exists for their own local instance. By moving global data into the GameState, you ensure that the data is centralized, synchronized, and accessible by every UI widget and actor in the game, regardless of who is playing.
By utilizing the GameState to bridge the gap between server logic and client visuals, you keep your game world consistent and your players informed of the current match status.
Key Takeaway: The GameState class is your centralized hub for global match data, ensuring that variables like scores and timers are automatically replicated and kept in sync across all connected clients.