- Home /
Too subjective and argumentative
Best practices to synchronize object states
Imagine a reversi game in realtime, without the turn-based structure. Now imagine there's, for example, 500 tiles that can be either white, black or blank (most of the blank on the start).
What's the best way to keep in sync the 500 tile states (example as an enum blank=0,white=1,black=2), to prevent a desync. Should the client look for a mismatch (imagine a case of packet loss)
Imagine all the tiles are on one array, whats the most efficient to contrast them with the client and update them if needed? Apparently one can't send arrays(built-in) over the network, or am I wrong?
Answer by daggada2 · Mar 12, 2017 at 06:12 PM
If you want to send the entire board state, you might consider converting the array data to a string, sending an RPC with that string as a parameter, and then parsing the string on the other side. Could even use an xml generator to generate the string, could use a simple comma-separated-value, etc. You also can go more fancy with BinaryFormatter. See this link: http://answers.unity3d.com/questions/318593/using-rpc-to-send-a-list.html
If the string gets absurdly long you could look into using compression like: http://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp
If you feel that might not be ideal because the game board is changing several times a second, maybe send the tile changes from the server, and have the client use a request RPC.
So client request change tile x,y. Server receives, updates board, tracks changed tiles for that particular move request, and sends an RPC as above with a string of changed tiles.
i.e. String format could be [tile x],[tile y] ,[color] ;
So the changed RPC string might look like: "1,1,0;1,2,0;1,3,0"
Client parses and sees tile 1,1 & 1,2 & 1,3 have changed to black.
I ended up just not needing what I initially thought I needed. Every tile has a syncVar with an integer for the value, and if they have changed on the server compared to the client it updates it through a command. I'm kind of a networking greenhorn atm.
But your explanation is great and would help in the scenario I pictured so I accepted your answer.
Thanks
Follow this Question
Related Questions
Unity Editor Crash !!! 0 Answers
UNet server spawning objects can't be interacted with 1 Answer
UNET moving the players via the server. 1 Answer
Authoritative Server / Player Communication 1 Answer
Turn based game server instances 1 Answer