There are different ways in which Lag can affect a game, from having a constant Lag that will make your game feel a little weird or slippery, to having massive Lag-Spikes that could shuffle everything in your game depending on how you do things.
There could be very different approaches on how to deal with Lag and Lag-Spikes depending on what kind of game you’re doing. Something that has a big impact on your game latency is what protocol you’re using, UDP or TCP.
– UDP vs TCP
Most games use UDP since its generally a faster protocol that does not check the integrity or order of the incoming packets. If you’re using the built in technology on your game engine, it’s probably using a modified version of UDP, that makes it a little more reliable without sacrificing so much on performance and speed.
If you’re programming your own solution for this (Sockets), I would highly recommend to use UDP for most cases, it really makes a huge difference vs using TCP, especially if it’s an online game. When communicating over a network there is always some packet-loss, even more when on the internet vs a LAN. This packet-loss is what slows down TCP so much.
Can packet-loss affect my game when using UDP? Yes, but in reality, not that much. If you think about it, in most games you’re going to be sending several messages a second to keep the server/clients updated, if some of these don’t make it through, it will not affect your game so much since the newer messages that do make it through will update everything anyways.
When would I use TCP? If you’re making a turn-based game that does not require a “real-time” response. Many Mobile games actually use TCP as their protocol. Lets face it, mobile games are not there yet to actually have a fully functional real time FPS experience compared to computers. There are not many mobile games that actually try to do this.
– LAN Games
Although the technology now is allowing for easier, cheaper, and faster online games, there are still some people that would like to have their game tied strictly to a LAN.
Remember those glorious LAN-Party days? Well these types of games are the ones we’re talking about, from FPS games like in the old days Counter Strike, Quake and Unreal Tournament, to newer games that are leaning more on the indie side like Artemis Spaceship Bridge Simulator.
When making a strictly LAN game, we are tied to making a game that will act or be sold as a product, meaning that there are no monthly fees, no services tied to it, a self-contained product.
There is one big advantage to having a LAN game, and that is, an extremely low Lag. Unless we are connected through a VPN which simulates a LAN and still works, it can definitely introduce some significant Lag into the whole experience.
Why make a LAN game?… Well, if you don’t really have much experience with networked games, this is the place to start. It’s certainly easier and simpler to manage a few IPs inside your LAN than to manage several URLs, especially when you’re not used to it. Having to deal with either your cloud provider or hosting services just adds an extra layer of unnecessary complexity and cost.
If you’re just starting with multiplayer games, I highly suggest you start with some LAN projects, this will get you going and will allow you to learn the most important and useful things on multiplayer games for the least amount of work and virtually no extra cost.
Even though LAN games have very little Lag, there are still some system designs that could make your game feel and play better. These are the differences between a fully authoritative server vs non-authoritative servers (more on these later).
– Online Games
Here is where we really need to worry about Lag and the consequences that it will have in our game. When a game is laggy, it can sometimes feel choppy, slippery, with slow response, or just weird in general. This weird feeling while playing your game could have a great impact on the whole experience for the users playing.
What can we do about Lag? There are several ways we can approach Lag. We will be discussing the most useful and common ways to approach this, including: client predictions, regional servers, network/server optimizations.
– Client Predictions
This simply means that our Client (whatever the user is running on his computer) application will try to predict all the movable object’s positions over time in case there is a Lag-Spike or a slight disconnect.
Lets review a simple example. Assume you’re running your online game and the server updates the position of another character to your Client. Based on the previous position and in relation to the new position, the client will know the direction in which the updated character is moving. This is usually known as interpolation. So if in case of a Lag-Spike the server fails to update the position on the next frame, the client is able to “predict” the position of the character for the next frame. This is assuming that the character will keep doing the same action.
So if we didn’t have any type of client prediction and had a Lag-Spike, we will have to wait for the server to reconnect. And whenever the server updates the position of everything in the game, things will just snap to their new position. Even when we don’t have considerable Lag-Spikes, just having a rather high Lag will make our game look and feel very choppy.
A possible negative effect of having a not so good client prediction is that the game will feel more slippery. It is important to know in your code when the client predictions kick in and, if possible, have a variable strength depending on the general latency and/or delay of the messages.
– Regional Servers
One possible solution for handling high latency in online games is to have regional servers.
Lets say the server is physically located in California, and someone is playing your online game in Australia. There would be a massive amount of Lag in this case, considering the distance, many in-between connection nodes, unoptimized routing tables, etc. This is why regional servers are a great way of providing a better service to many regions around the world.
Keep in mind, you need enough users in each region to make this worth it for the users and yourself. Meaning, if there are not enough players connected to a specific region, it is not worth it to have that region functional since it incurs a greater cost to you and affords no player diversity for the users in these low-usage regions.
I would generally recommend to have fewer regions and to have them as centralized to your target markets as possible. For instance, if your target market is English speaking countries, you can have two servers, one centralized in the US that would cover both the US and Canada, and another centered in between the English speaking areas of Europe.
– Network and Server Optimizations
These types of optimizations require a high knowledge of the whole network structure, how your system works, and in most cases they go hand in hand with custom network solutions, meaning that you coded your own socket based communication system.
There are a lot of great improvements to be done if this is the case. We will go into detail about this in a later article.
Some of these could include:
- Minimizing message usage – when you code your own communication solution, you can actually control the frequency and/or eventuality of the messages you’re sending. This can improve not only on Server/Client process performance but also better communication. The fewer packages that are sent, the fewer packages that are lost.
- Minimizing the bytes sent – simply put: the smaller the byte usage in your network messages, the smaller and fewer packages to send and to get lost. There are several ways to do some Bit-wise optimizations that can greatly improve this aspect of your communications.
- Choosing the specs for your servers once you have an MVP server running, you can stress test and load test your servers, and based on this choose the best configuration for the specs of your servers, whether you’re hosting or using cloud services.
Please post some questions, comments, or suggestions. I want to be able to help you guys as much as I can by covering as much as possible. We can go into a very detailed and specific discussion on whatever you need.