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

Synchronizing Controller State with Network Replication

Learners will implement controller-side variables that correctly replicate across a network to maintain consistent state between server and clients.

In this lesson, you will learn how to synchronize specific player-related variables directly within the PlayerController so that the server and client stay in sync during gameplay.

When you create variables in your PlayerController—such as a player’s current score, inventory count, or menu state—they do not automatically update across the network. Because the PlayerController exists on the owning client and the server, but not on other players' machines, you must explicitly tell Unreal Engine to replicate these values.

The Concept of Replication

In Unreal, replication is the process of sending data from the server to connected clients. To synchronize state, we use the Replicated property specifier. When a variable is marked as replicated, the server keeps track of its value and automatically pushes updates to the client that owns that specific PlayerController.

Implementation

To sync a variable, you define it in your C++ class header and use the UPROPERTY macro with the Replicated keyword. You must also implement the GetLifetimeReplicatedProps function to register the variable.

// Inside your PlayerController.h
UPROPERTY(Replicated)
int32 PlayerScore;

// Inside your PlayerController.cpp
void AMyPlayerController::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
    Super::GetLifetimeReplicatedProps(OutLifetimeProps);
    
    // Tell the engine to replicate the PlayerScore variable
    DOREPLIFETIME(AMyPlayerController, PlayerScore);
}

Why This Matters

If you update PlayerScore on the server, the server will detect the change and send the new value to the owning client. This ensures that the UI or gameplay logic on the client correctly reflects the truth established by the server. Without this, the client might show a default value of zero while the server has already progressed to a higher score.

By keeping state in the PlayerController, you ensure that the data is tied specifically to the player's session, making it the perfect place for persistent information that needs to travel across the network.

Key Takeaway: To keep data consistent across the network in your PlayerController, you must mark variables as Replicated and register them in GetLifetimeReplicatedProps so the server can push updates to the owning client.