Wednesday, December 25, 2019

multiplayer - How to solve "server lag" problems that break the game


How do real-time multiplayer games deal with latency problems (or “server lag”)?


Imagine an online fighting game where 2 players battle head-to-head in real time. When a player performs an action, there'll be a short delay until that player's action appears on the opponent's computer screen. This delay causes many game-breaking problems.


Let's say 2 hypothetical players, Player A and Player B, are fighting against each other right now in the game, and the latency is 1 second. Player A is moving toward Player B, but then Player B places a wall (or something of the like) that stops Player A from moving toward Player B. Player A doesn’t see the wall appear on his/her computer screen until 1 second later, but by then Player A has already moved past where the wall was placed.


In a more game-breaking situation, Player B is just about to kill Player A. Right before that happens, Player A uses a special attack that instantly kills Player B. But on Player B’s computer, Player B kills Player A, because it takes 1 second before Player A’s special attack happens on Player B’s computer. This makes it so both players die even though only Player B was supposed to die.


One solution to deal with the latency issue is to implement an artificial delay so that when there’s a discrepancy, one player’s game is momentarily paused to wait for the other player’s game state to “catch up.” This is not a good solution because it makes the game feel extremely laggy and unresponsive, and it also renders some reactive actions useless (like dodging) because of the artificial delay.


What can be done to fix these latency problems?



Answer



The easiest solution to the "who did it first?" problem is an authoritative server. Both players connect to the server which handles all the game mechanics. Whatever arrives first at the server is considered as happening first.



So if the server receives a message "I place a wall" from player B and then a millisecond later a message "I move to where the wall is" from player A, the server will reject the move-message as impossible and send player A a corrected position for their character.


What then happens on the screen of player A is that their character moves at first, but then the wall appears and the character is snapped back to its position before the wall.


This method of resolving timing conflicts gives an advantage and a better game experience to those players who have a better connection to the server ("Low Ping Bastards").


A different way to resolve conflicts is to take latency into account when resolving actions. In order to do that, the server needs some way to keep a history of the game state. When the "I move to where the wall is" message arrives at the server, the server checks Player A's latency, looks into the history if a wall was already there a few milliseconds before, and decides accordingly. That means the game looks more consistent for high-pingers, but low-pingers will far more often witness situations where other players seem to walk through walls shortly after they were placed.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...