Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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.
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.
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).
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?
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 questionAsk a Question
731 491 924 answers to any question