Network model¶
Co-op is hosted by one of the players, and the host's game decides almost everything. A change that ignores this works when you play alone and breaks in co-op. Multiplayer covers hosting and joining.
A listen server, not a dedicated server¶
One player's game is the server, a listen server, and the others join it. A game holds four players
at most, a number you change with MaxPlayers in
DataAssets/Session/Childs/DA_Session_Default.
The Gameplay Ability System is not used, because it needs C++ and the template stays Blueprint only.
What the host decides¶
The host holds the true state of the world. It populates the map, rolls the loot, runs the zombies and the world systems, and keeps the one clock. It resolves looting, damage, crafting and building, saves the world and is the only one to grant experience.
Every gesture, from searching to reloading, is a timed action that a player's machine asks for and
the host accepts or refuses with a reason. The host refuses any action that is not in the
action registry, DA_ActionRegistry_Default. A change of your own to the world
takes the same path, usually as a PDA_ActionDef listed there, and is never made on a client.
Architecture follows a request step by step.
Every random draw of gameplay is made on the host, such as the outcome of a climb or whether a house is locked, and every machine shows the result. Make your own draws there too.
A shot is resolved by the host alone, with the shooter and the zombies where it has them when the
client's Server_Fire(AimYaw, bAiming) arrives. There is no lag compensation. A client with L ms of
latency aims at zombies shown about L ms earlier, and on their screen a moving zombie can seem hit
without being hit.
The host simulates every vehicle, whatever the movement setting below. A client at the wheel sends its inputs and gets no prediction of the car, so on a slow connection the car answers a little late.
The host also decides who gets in. It checks the version, the password and the identity of the joining player, and applies the same character rule as the menu to whatever a client sends.
Movement¶
Each machine predicts the moves of its own survivor, so that it stays responsive, and by default the host checks them.
Host-authoritative movement¶
bHostAuthoritativeMovement is ticked in DataAssets/Session/Childs/DA_Session_Default. The host
simulates the same moves as the client, and corrects a client that ends one more than 20 cm from
where the host computes it. A modified client can no longer walk through walls, teleport or run
faster than its speed.
- The tolerance is
MAXPOSITIONERRORSQUARED(400, in cm squared) inConfig/DefaultGame.ini. The engine's own value, 3, corrected every change of walking speed. - While a survivor climbs over an obstacle, the host plays the climb too and takes the client's word
until
VaultTrustGraceSeconds(0.6 s, a Setting onBP_Survivor) after it lands. - The URL option
?HostAuthMovement=0turns the check off for one game, and?HostAuthMovement=1forces it on. - The host replicates the setting in
BP_ZSGameState.bHostAuthMovement, andBP_Survivor.ApplyMovementTrustsets the movement flags of every survivor from it.
With two games on one PC under Unreal's Average network emulation, walking, sprinting, aiming and climbing produced no correction. Two separate PCs have not been tested yet.
Trusting each player's machine instead¶
Untick bHostAuthoritativeMovement, and the host accepts the position each client reports for its
own survivor and never corrects it. Movement stays smooth whatever the latency, but a modified client
can move its survivor anywhere. Keep it for games between friends.
The password is not security¶
The password keeps a game between friends closed to strangers who find it on the network. The host
checks it against what the joining game sends, and nothing is encrypted. The host can /kick and
/ban a player by name, but nothing detects a cheating client, so keep the movement check on when
you play with people you do not know.
Replication in plain words¶
When a replicated value changes on the host, each machine receives the new value and updates what it shows.
A door, a barricade, a built piece, an item on the floor, a corpse or the blood on the ground is a replicated value, so a player who joins a game in progress sees the world as it is. Put anything other players must see in a replicated variable. A one-off message alone is never seen by a player who joins later.
The host sends facts, such as the hour, the weather and what a survivor wears, and each machine draws the sky and dresses the survivors from them. A sound that comes with a change of state, such as a door opening, plays when that state arrives, so a player who joins later does not hear it.
A player who joins is sent the neighbourhood around their survivor at once. The project raises Unreal's ceiling per player to 1,000,000 bytes per second for that reason, with the settings listed under Bandwidth in Multiplayer.
A zombie sends its state 12 times a second (NetUpdateFrequency), down to 2 when nothing changes,
and nothing while it sleeps far from every player.
Public and private¶
| Public: every player receives it | Private: only the owning player receives it |
|---|---|
| what a survivor wears, with each piece's holes, patches and stains | statuses, endurance, body temperature, wetness, shelter |
| the blood and dirt on the skin, and whether the survivor sleeps | the health of each body zone, and the wounds |
| the body and skin tone | the contents of the bag and of the worn bags |
| the weapons stowed on the body, with their attachments | the world map and its marks |
To treat another player, you open their health window in patient mode.
What stays on each screen¶
Each machine runs these for its own player only:
- the interface: the HUD, every window, the chat and the window positions;
- the camera: the zoom and the first person view, taken from each machine's own data;
- vision: walls hide only for the player standing inside, and each player's own line of sight hides zombies and other survivors;
- name tags, shown above other survivors only, and not through walls;
- rain and snow, spawned around the local survivor;
- the audio listener, on your own survivor;
- the options: window, quality, keys, scale, text size, language, volumes and gamepad choices.
In your own graphs, keep the interface, the camera, vision, material parameter collections and the
audio listener behind IsLocallyControlled or IsLocalController.
Network dormancy¶
Everything built on BP_Interactable starts dormant on the network (Net Dormancy is Initial in
its Class Defaults). The host sends a placed door, cupboard or campfire once, then stops looking at
it, which keeps a town of several thousand interactables cheap.
A Blueprint Set node on a replicated variable of the actor wakes it by itself, so a child of
BP_Interactable that sets its own variables needs nothing more.
Add a replicated action relies on that.
Two kinds of change do not wake it. A change made from a component needs Flush Net Dormancy on
the owner right after, as AC_HeatSource does when a campfire is lit or fed. An array or a struct
changed in place (Add, Remove, Set Array Elem, Set Members) needs the same call on the actor.
You can also avoid the change in place: copy the variable, change the copy, then Set the whole
variable, as BP_Container does with Contents. That Set wakes the actor like any other.
Without the wake, the clients keep the old state: the campfire stays dark on their screens. Alone on the host everything looks right, so this only shows with a second player.
Names and tests¶
Events are named after the machine they run on: Server_* on the host, Client_* on one client,
Multicast_* on every machine, and OnRep_* for replication callbacks. Name yours the same way.
Test a change in four cases, with two players in the editor: the host acts and a client sees it, a client acts and the host sees it, a client acts and another client sees it, and a player arrives in the middle of the game.