In this lesson, you will learn how to configure Actor variables to automatically synchronize across the server and clients using the Replicated keyword.
The Mechanism of Property Replication
While RPCs are excellent for executing logic across the network, they are unsuitable for maintaining a persistent "source of truth" for game state. To ensure that variables like player health, ammo counts, or actor transforms remain consistent across all connected machines, we use Property Replication.
By marking a variable as UPROPERTY(Replicated), you tell Unreal Engine's replication system to monitor that variable. Whenever the server changes its value, the engine automatically packages the new data and sends it to the clients during the next network update.
Implementation Requirements
To implement this, you must perform two essential steps. First, you declare the variable with the Replicated specifier:
// In your Header (.h)
UPROPERTY(Replicated)
float CurrentHealth;
Second, you must override GetLifetimeReplicatedProps in your C++ implementation file to register the variable with the engine’s replication system:
// In your Source (.cpp)
#include "Net/UnrealNetwork.h"
void AMyActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyActor, CurrentHealth);
}
Why Use This Over RPCs?
If a client joins the game late, they will miss any RPCs that were fired before they connected. However, if a variable is marked as Replicated, the server will send the current state of that variable to the new client automatically as part of the initial actor synchronization. This makes property replication the standard approach for data that defines the current "snapshot" of an object's state in the world.
Always remember that only the server can modify a replicated variable; if a client attempts to change a Replicated variable locally, the server will not receive that change, and the client's value will likely be overwritten the next time the server pushes an update.
Key Takeaway: Use the Replicated keyword combined with DOREPLIFETIME to create a robust, persistent synchronization layer that ensures all clients automatically stay updated with the server's authoritative state.