In this lesson, you will learn how to use the GameState class to keep all connected players perfectly synchronized with the shared data of your match.
The Role of GameState
While the GameMode handles the "rules" of your match (like score thresholds or win conditions) and exists only on the server, the GameState is the specialized container for information that every player needs to know. Whether it’s the current time remaining in a round, the team scores, or the status of a global objective, the GameState acts as the single source of truth that the server automatically pushes out to every connected client.
Synchronizing Data
To make data visible to everyone, you store your variables inside a custom GameState class and use the UPROPERTY(Replicated) macro. When the server changes a variable in the GameState, Unreal’s networking system automatically broadcasts that change to every client in the game.
For example, if you want to track a global score, your header file would look like this:
UCLASS()
class MYGAME_API AMyGameState : public AGameStateBase
{
GENERATED_BODY()
public:
// This variable will automatically sync from Server to all Clients
UPROPERTY(Replicated)
int32 GlobalScore;
// You must override this to register the variables you want to sync
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
};
In your implementation file, you define the registration:
void AMyGameState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Register the variable so it is included in network synchronization
DOREPLIFETIME(AMyGameState, GlobalScore);
}
Why use GameState?
If you stored the GlobalScore on an individual PlayerController or Character, other players might not have access to that object, leading to "ghost" data that only the local player can see. By putting it in GameState, you ensure that every client—regardless of which character they are controlling—has access to the exact same, up-to-date information.
Key Takeaway: The GameState is your central, server-authoritative hub for shared match data that must be replicated to every client in the game.