S
S
SvetlyiAkaPro2014-11-02 21:27:09
Canvas
SvetlyiAkaPro, 2014-11-02 21:27:09

What is the best way to sync game data between server and client?

I understand that the best architecture for the game is the one in which the server is the main one, moves the players, bullets and counts everything. But what if there are many elements of the game, each one needs to be moved, something to be calculated depending on their position relative to each other? For example, there is something like this on the server:

setTimeout(function() {
//все просчитываем, двигаем, посылаем
}, 30);

Can you somehow improve, optimize it? For example, to read and move something on the client and send it to the server only when some elements are created, the same bullets, for example, but so that everything is synchronous on the clients and the server, so that there are no situations when there is something on the server already happened, exploded, and on the client bullets, grenades, some elements have not even reached yet.
I will clarify
For each player there are an average of 20 bullets that fly in a straight line and their coordinates must be calculated, for which setTimeout, which every moment of time moves in the direction of their movement (and counts the coordinates of the players). Actually, the problem is in the desynchronization of coordinates, because at this stage, the coordinates are considered both on the server and on the client, but they are not exchanged, only the score is synchronized when it hits the enemy.
I would like to synchronize the coordinates, I see the following options:
  • Send bullets in a bunch along with the coordinates of the players
  • Keep the current architecture and synchronize the coordinates of the bullets every 60ms is acceptable
  • Calculate the delay between the server and the client and add the difference to the fps in setTimeout

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Lerg, 2014-11-02
@SvetlyiAkaPro

30 fps on the server is too much. Depending on the game, you can significantly reduce the calculation frequency to 5..15 times per second.
The client calculates everything at 30 or 60 fps, comparing the data with the server ones. If the client does not calculate anything himself, then terrible lags will be visible.

S
Sergey, 2014-11-02
@begemot_sun

Only the server has a complete picture of the world. Each client has only a local picture of the world that he needs at this very moment and never again. Transferring the calculation logic from the server to the client brings additional difficulties:
1. Cheating (how to trust the results received from the client?)
2. Synchronization. Each client is just a tool for displaying the game environment, but what about the fact that the results of the calculation from one client should be distributed to other clients?
3. Maybe something else???
In short, they never do.

S
Sergey, 2014-11-02
Protko @Fesor

on the server it is necessary to calculate what the logic of the game depends on, so that the users could not correct anything on the client. Calculation of some things related to the display of this - of course on the client.
Well, it’s quite difficult to think about optimizations without knowing the whole picture. And again, it's not clear where setTimeout has to do with it. It's
better to hang data exchange on WebSockets, and even better on WebRTC (there is UDP support for data exchange).

E
Elizaveta Borisova, 2014-11-02
@Elizaveta

Aha, there is not enough prediction on the client.
You can read about synchronization here forrestthewoods.com/synchronous-rts-engines-and-a-...
And search here 0fps.net
Do you make HTML5?

S
SvetlyiAkaPro, 2014-11-02
@SvetlyiAkaPro

Okay, let me explain, by calculating on the client, I meant duplication of calculations on the server as well. Calculation on the client in order not to drive the data once again. Well, the results of the game are naturally calculated on the server, so there can’t be cheaters here :) But then there are problems with desynchronization
WebSockets are used, setTimeout for moving elements, they do not depend on any events, just some elements go, swim, fly , move all the time at a certain distance and this must be reflected on the client
In setTimeout(....., 30); 30 just for example, to show the essence of the work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question