Multiplayer tutorial 1: concepts

Favourite 219 favourites
Tutorial written by AshleyOriginally published on 3rd, March 2014 - 4 revisions

Compensating for network conditions

To minimise bandwidth and packet loss, by default the multiplayer engine transmits data up to 30 times a second. Games typically render at 60 frames per second, so this means data is only sent approximately every other frame.

Compensating for peers

Peers will be receiving data from the host indicating object data, such as their positions. If they simply directly displayed what was received, object movements would appear choppy as they only get updated every other frame. They would also move irregularly, with the updates subject to PDV and packet loss.

To improve the gameplay experience, the multiplayer engine interpolates between updates for values like X and Y co-ordinates. In the case of positions, it will linearly interpolate between updates. For example if data is coming in every other frame, then the frame which does not receive an update will use a value half way between the two updates either side. This makes the motion smooth again without needing any more data.

However when rendering an interpolated frame, it does not know what the next value is yet, because that will arrive in the future! Instead of using a time machine, the multiplayer engine solves this by adding an artificial extra delay of 80ms for the peers. This means the multiplayer engine can know about the next few updates ahead of what it is currently rendering. Even if the next update is missing due to packet loss, it may be able to use the next update after that to interpolate towards. Even if poor network conditions mean there are no updates for 80ms, the engine can keep extrapolating from the direction of the previous two updates. This however is just guesswork, and the object might jump if the next update shows the object has in fact gone somewhere else.

The multiplayer engine allows you to use three possible interpolation modes for values: linear (such as for object positions), angular (such as for object angles), and none (for data that should not be interpolated, like a boolean indicating if a laser is on or not).

Latency variations

It should be noted that the latency can change. For example a routing device somewhere may become overloaded, and the data between two players suddenly gets re-routed down a different path which is slower; or perhaps a new faster path becomes available. Latencies sometimes also gradually improve in the first minute or so after connecting, as the devices along the connection route gradually optimise the connection and figure out the fastest possible path. The latency is constantly re-measured to detect such variations.

If the latency decreases, the multiplayer engine temporarily runs the game in a very slight fast-forward so it reduces how far behind you are seeing the game. On the other hand, if the latency increases it very slightly runs the game in slow-motion and increases how far behind you are seeing the game. This is necessary to avoid the choppy, irregular movement that occurs when the 80ms delay no longer covers any updates. These adaptations should happen slowly enough to be imperceptible, but it helps ensure the best possible experience even if the connection quality varies during the game.

Compensating for the host

The host has the authoritative version of the game. They have zero latency, since there is no need to transmit data to themselves. In other words, the host is participating in the "real" game, and the peers are doing their best to render the same game with delayed and possibly irregular updates from the host. The host has a slight gameplay advantage as a result.

You might think that the host would need to interpolate between updates of the object positions received from peers. This actually does not happen, because the peers should not tell the host where they are. In fact, the host should tell the peers where they are. The reason for this is covered in the next section.

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


stefanos 3,482 rep

Yeah that's nice :)

Monday, March 03, 2014 at 8:36:42 PM
Vallhalen 3,067 rep


Monday, March 03, 2014 at 8:43:51 PM
Leandrus 6,001 rep


Monday, March 03, 2014 at 8:55:27 PM
SgtConti 4,941 rep

Very interesting, hopefully the beta release comes tomorrow ;)

Monday, March 03, 2014 at 9:02:03 PM
DUTOIT 12.8k rep

Fantastic :)

Monday, March 03, 2014 at 9:05:34 PM
emoaeden 9,812 rep

I can't wait to try this !

Monday, March 03, 2014 at 9:07:53 PM
NeoBiel 4,639 rep

Meu sonho se realizando...

Monday, March 03, 2014 at 9:08:29 PM
albrektv 8,606 rep

Thank you for the lesson that you are waiting for him

Monday, March 03, 2014 at 9:11:31 PM
valdarko 6,357 rep

how it will handle data save, like itens and stuff?

Monday, March 03, 2014 at 9:33:35 PM
Joskin 6,129 rep

Yes ! Thanks a lot !

Monday, March 03, 2014 at 10:23:48 PM
jobel 17.2k rep

I'm very excited to go through this! thanks!

Monday, March 03, 2014 at 10:35:05 PM
DatapawWolf 30.9k rep

I notice this does not address the subject of host cheating. Preventing the clients is nice and all, but, for example, say the host hacks their client, they could now send whatever they want to the peers.

Now you could say that anti-cheating measures and checks could be implemented, but then you could apply that same logic to each client as well.

Thoughts, anyone?

Monday, March 03, 2014 at 11:17:34 PM
popyui 2,499 rep

Me gusta mucho esto y me da una mejor idea de como funcionan mis juegos de mis consolas

Monday, March 03, 2014 at 11:19:48 PM
DatapawWolf 30.9k rep

Also, not to spam, but addressing the "Lag Compensation" techniques. I'm sorry but if a player has a bad connection in a shooter game, too bad for them. Newer games in the Call of Duty series have notoriously infuriating lag comp which is responsible for all sorts of nonsense.

In my opinion it's a technique that is often overused and should not be looked as much as interpolation or other ways of synchronizing between peers.

Monday, March 03, 2014 at 11:29:12 PM
DatapawWolf 30.9k rep

"By default updates are sent 30 times a second"
We will be able to modify this, correct? Or send out updates only when needed? It'd be wasteful to constantly spew out that much data for something like a grid/turn based game, where movement may be as low as 1 grid square per 100ms, or not at all when a player is AFK.

Monday, March 03, 2014 at 11:32:42 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.