Replication
Unreal multiplayer uses an authoritative server. The server runs the real game; clients run a copy that the server keeps in sync through replication: actors marked to replicate are created on clients, and their replicated properties are sent whenever they change. Clients talk back with RPCs (next lesson). Everything in this section also applies to a listen server, where one player's machine is the server.
Making an actor replicate
// ArenaPickup.cpp
AArenaPickup::AArenaPickup()
{
bReplicates = true; // spawn on clients, replicate properties
SetReplicateMovement(true); // send the root component's movement too (for moving actors)
}Actors placed in the level exist on every machine already. With bReplicates they are matched up by name; without it, each machine has its own unrelated copy. Spawn replicated actors on the server only. A replicated actor spawned on a client exists only on that client.
Replicated properties
// ArenaPickup.h
UPROPERTY(ReplicatedUsing = OnRep_IsActive)
bool bIsActive = true;
UPROPERTY(Replicated)
int32 Charges = 3;
UFUNCTION()
void OnRep_IsActive();
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;// ArenaPickup.cpp
#include "Net/UnrealNetwork.h"
void AArenaPickup::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AArenaPickup, bIsActive);
DOREPLIFETIME_CONDITION(AArenaPickup, Charges, COND_OwnerOnly);
}
void AArenaPickup::OnRep_IsActive()
{
// Runs on clients when the new value arrives. Update visuals here.
Mesh->SetVisibility(bIsActive);
if (!bIsActive) { /* play the pickup sound/VFX */ }
}
void AArenaPickup::Consume() // called on the server
{
check(HasAuthority());
bIsActive = false;
OnRep_IsActive(); // RepNotifies don't run on the server: call it yourself for a listen-server host
}The rules:
- A property needs
ReplicatedorReplicatedUsingand an entry inGetLifetimeReplicatedProps. Missing either one means no replication. - Replication is server → client only. A client changing a replicated property changes its local copy until the server overwrites it.
- RepNotifies run on clients, when a changed value arrives. In C++ they don't run on the server (Blueprint RepNotifies do), so call the function yourself after changing the value if the host needs the same reaction.
- Only the latest value is sent, and not necessarily every change. Set a value to 5 and back to 3 in one frame and clients see nothing. For one-shot events use an RPC or a counter.
Conditions
| Condition | Sent to |
|---|---|
COND_None |
everyone (default) |
COND_OwnerOnly |
only the owning client (ammo, inventory details) |
COND_SkipOwner |
everyone except the owner (the owner predicts it locally) |
COND_InitialOnly |
only when the actor is first replicated |
COND_SimulatedOnly / COND_AutonomousOnly |
by net role |
DOREPLIFETIME_CONDITION_NOTIFY(…, REPNOTIFY_Always) |
call the RepNotify even when the value didn't change |
Bandwidth: relevancy, frequency, dormancy
The server can't send everything to everyone every frame, so it decides per connection:
- Relevancy: actors beyond
NetCullDistanceSquared(or otherwise not relevant) aren't replicated to that client at all, and are destroyed there.bAlwaysRelevantfor game-wide actors. - NetUpdateFrequency / MinNetUpdateFrequency: how often the actor is considered (per second). A pickup doesn't need 100 Hz. (Newer 5.x versions expose these through
SetNetUpdateFrequencyand friends.) - Priority: when bandwidth is short, higher
NetPriorityactors go first. - Dormancy: an actor that rarely changes can go dormant (
SetNetDormancy(DORM_DormantAll)) and costs nothing untilFlushNetDormancy(). - Push model (
bWithPushModel,MARK_PROPERTY_DIRTY_FROM_NAME) avoids comparing properties every update; large projects use it. - Iris, the newer replication system, is available as an opt-in in recent 5.x releases. The concepts here carry over.
A runnable model: property replication
The model keeps a server copy and two client copies of an actor. On each net update it compares the server's properties with what each connection last received (the "shadow state"), sends only changes allowed by the property's condition, and runs RepNotifies on the receiving side.
#include <iostream>
#include <map>
#include <string>
#include <vector>
enum Cond { None, OwnerOnly, SkipOwner };
struct Prop { std::string Name; Cond Condition; bool HasNotify; };
struct Connection {
int Id;
std::map<std::string, int> Shadow; // last values sent to this client
std::map<std::string, int> ClientCopy; // what the client's actor holds
};
int main() {
std::vector<Prop> props = {{"bIsActive", None, true}, {"Charges", OwnerOnly, false}, {"AimPitch", SkipOwner, false}};
std::map<std::string, int> server = {{"bIsActive", 1}, {"Charges", 3}, {"AimPitch", 0}};
const int owner = 1;
std::vector<Connection> conns = {{1, {}, {}}, {2, {}, {}}};
auto netUpdate = [&](const char* label) {
std::cout << label << "\n";
for (auto& c : conns) {
std::cout << " -> client " << c.Id << ":";
int sent = 0;
for (const auto& p : props) {
if (p.Condition == OwnerOnly && c.Id != owner) continue;
if (p.Condition == SkipOwner && c.Id == owner) continue;
if (c.Shadow.count(p.Name) && c.Shadow[p.Name] == server[p.Name]) continue; // unchanged
c.Shadow[p.Name] = c.ClientCopy[p.Name] = server[p.Name];
std::cout << " " << p.Name << "=" << server[p.Name];
if (p.HasNotify) std::cout << " (OnRep_" << p.Name.substr(1) << ")";
sent++;
}
if (!sent) std::cout << " nothing";
std::cout << "\n";
}
};
netUpdate("initial replication");
server["Charges"] = 2; server["AimPitch"] = 15;
netUpdate("server: Charges 3->2, AimPitch 0->15");
server["bIsActive"] = 0; server["bIsActive"] = 1;
netUpdate("server: bIsActive 1->0->1 within one frame");
conns[1].ClientCopy["bIsActive"] = 0; // client 2 cheats locally
server["Charges"] = 1;
netUpdate("client 2 sets bIsActive=0 locally; server: Charges 2->1");
std::cout << "client 2 still believes bIsActive=" << conns[1].ClientCopy["bIsActive"]
<< " (the server's value didn't change, so it isn't resent)\n";
}initial replication
-> client 1: bIsActive=1 (OnRep_IsActive) Charges=3
-> client 2: bIsActive=1 (OnRep_IsActive) AimPitch=0
server: Charges 3->2, AimPitch 0->15
-> client 1: Charges=2
-> client 2: AimPitch=15
server: bIsActive 1->0->1 within one frame
-> client 1: nothing
-> client 2: nothing
client 2 sets bIsActive=0 locally; server: Charges 2->1
-> client 1: Charges=1
-> client 2: nothing
client 2 still believes bIsActive=0 (the server's value didn't change, so it isn't resent)
The third update shows why replicated properties are state, not events: the flicker never reached anyone. The last line shows that a client-side change survives until the server's value changes. It is never "corrected" automatically, which is why clients must never write replicated gameplay state.
Testing
In the editor, Play → Number of Players = 2 and Net Mode = Play As Listen Server (or Play As Client, which starts a dedicated server in the background). The console command stat net and Networking Insights (Unreal Insights with the net trace channel, -trace=net) show what is sent.
Test yourself
You added UPROPERTY(Replicated) int32 Score; but clients never see changes. What is missing?
- A RepNotify
- DOREPLIFETIME(AMyActor, Score) in GetLifetimeReplicatedProps (and bReplicates on the actor)
- A Server RPC
- SetReplicateMovement(true)
In C++, where does a RepNotify function run automatically?
- On the server only
- On clients, when a changed value arrives
- Everywhere
- Only in Blueprints
Which condition sends a property only to the actor's owning client?
- COND_SkipOwner
- COND_OwnerOnly
- COND_InitialOnly
- COND_None