Back to Top

Training

Fighting multiplayer lag

Designing a MORPG that can handle hundreds, and sometimes even thousands of simultaneous players can be a very complex task. There are some aspects that can be controlled by us, the developers, such as building a fast server that's got a lot of RAM memory; still, other aspects are way beyond our control.

 

We can't make sure that all the clients/players utilize modern computers and have fast, lag-free Internet connections, for example. Sadly, network latency is often a frequent problem for most MORPGs. If you work as a one-man team, you can simulate Internet lag locally, by using your Wi-Fi connection. George Hardesty, CEO of Data Alliance, has come up with a clever trick. He states that under ideal conditions, the average latency of a Wi-Fi connection is 3 to 4 milliseconds, but you can increase this value to 100... 200 ms by building a Wi-Fi network that utilizes a cheap router, which has a slower processor, and then adding as many devices (phones, tablets, baby monitors, IoT devices, etc.) to the Wi-Fi network as possible. Tools such as NetSpot can provide a lot of useful info about your laggy network, including latency and packet loss, according to him.

 

Everyone agrees that creating a MORPG game that looks, feels and plays great over the Internet is essential for its success. And since we can't eliminate lag and packet loss, we will need to use several clever programming tricks.

 

As you already know, most games that are played over the Internet utilize the client/server architecture. The server runs the main game code, while the clients play a much smaller part. At a basic level, clients will only send players' input (keyboard strokes, mouse movement and clicks, etc.) to the server, which processes them, and then tells the clients what they should do next.

server architecture

So, player's actions won't ever have an immediate impact in the game; they will only achieve their goal AFTER, AND ONLY IF the server has sent back the corresponding data packets. It's quite clear that things can quickly go wrong if the players have high-latency Internet connections. If a particular client has a 200 milliseconds latency, for example, its firing event will be received by the server 200 ms later, and server's response will be acknowledged by the same client only 400 ms (we've got a round-trip here!) after the actual event took place! This kind of delay is simply not acceptable for MORPGs, and here are two methods that have been proven to be very useful time and time again.

 

1. Client-side prediction.

 

Most of the data that flows from the client to the server and back has to do with client movement. To minimize it, high-level games allow the clients to run most of their own movement code, syncing it with the server's position corrections regularly. If network latency is 400 ms, as discussed in the previous example, the player will adjust its position every 400 ms. This may lead to sudden (though minor) player position changes, but it is a very effective way of beating Internet lag.

 

The client will always use the server's most recent coordinates as a starting point. Then, it will try to predict the server's next coordinates based on its local calculations, and move the player accordingly until the next "reality check" signal from the server arrives.

 

2. Interpolation.

 

With interpolation, the server will measure the average latency of a particular client, and then look back at the previously sent world updates for that client. Then, it computes an average position using the already sent world update and the one that is about to be sent out, moving the client to its average position (a little bit back in time, if you will). This way, since the game is played tens or hundreds of milliseconds in the past, the server can correctly evaluate the clients' firing events, for example. So, clients won't miss their targets because of Internet lag.

 

Due to client-side prediction and interpolation, the clients will run more of the game code on their systems. This means that multiplayer lag will be significantly reduced, but having decent client hardware is mandatory. Fortunately, the problems is often solved for good by providing various graphics and sound settings in the game's "Options" panel, and so on.

Need help with code for your MORPG? I can help!