Ramblings of an aging IT geek
← Ramblings of an aging IT geek
gamedev

what nobody tells you about replicated variables

First contact with Unreal's network replication, and the realisation that the server is the only thing that is actually true.

A gamedev editor on a screen

I spent an evening this week trying to make a door open for two players at once, and came away with a much better understanding of why multiplayer games are hard. The short version: in Unreal, the server is the only thing that is real. Everything the client thinks it knows is a hopeful copy.

You mark a variable Replicated, add it to GetLifetimeReplicatedProps, and Unreal quietly syncs it from server to clients. That part is tidy. What caught me out is that setting the variable on a client does nothing useful. The server changes it, the change flows down, and a RepNotify fires on the receiving end so you can react. My door worked on the host and did absolutely nothing for the joining client, because I was flipping the bool client-side and expecting magic.

UPROPERTY(ReplicatedUsing = OnRep_IsOpen)
bool bIsOpen = false;

void ADoor::OnRep_IsOpen()
{
    PlayOpenAnimation(bIsOpen);
}

The mental model that finally landed: clients ask, the server decides, the result trickles back down. Server RPCs to request, replicated state for the truth, multicast or RepNotify for the visible effect. Obvious in hindsight, slightly maddening at the time. Two hours, one door. I'll take it.