- Home /
Networking sync an array of arrays?
I have an array of arrays that holds the information on if each tile in a 5000x5000 2d top-down world is currently solid or not.
public bool Solid[,];
On the server side the solid always reads the correct value for if a tile is solid or not, and the client side does not ever change the value only reads it. how do i make sure that the cluent side is always seeing the correct value for Solid? It seems syncing arrays is very hard and i have found nothing for arrays of arrays (I'm very new to unity and self learning so I could be using the wrong name)
Currently the biggest issue is when new players join. Every time a player joins things that should be solid for them are not. The value on the server side is correct but not on the client side. I do realize the problems with client side detectionn for the solid value and would like to try to do it anyways. Does anyone know how i could sync the values?
post the code that sends and recieves and i can help
Answer by PandixDevelopment · Jan 24, 2021 at 04:02 AM
I had ended up setting up a new 2D array that holds a bool for if a tile has changed which is set across all players when a new tile is created. The player checks the small area the player can see to check if any of the bools are true. if a bool is true it checks the server's ID for that tile and changes it acordingly as well as setting that tile to false on the player side. If I were to give any advice to my past self or anyone reading this it would be to use gameobjects instead of a tilemap because it made it all alot more of a problem.
Answer by Bunny83 · Jan 18, 2021 at 11:47 PM
Well it all depends on how you manage your data. First of all arrays of arrays for 5k x 5k is a bad idea. It means you would have 5000 arrays each has a size of 5000 elements. This is quite impracticable as a change in neighboring "rows" would require you to update the whole array. Using jagged arrays is kinda slow due to the double reference you need for every tile you want to access. However if you do want to subdivide space, it's better to use square areas instead of single rows. By splitting the area into squares, you get neighboring tiles more likely in the same "chunk".
When a client enters a new chunk you would only need to send that chunk over. Note that when the solid state can change rapidly (I guess it's about some sort of digging minecraft / terraria style) you want to send individual changes of single tiles in order to keep them synced. So whenever the server changes a tile, every client that is "in range" of that tile should receive this change message through the network. Such changes may be send unreliable if a high number of them are expected. From time to time you may do a reliable full sync of the loaded / visible chunks to avoid long time desyncs.
Of course if you want to send over a whole chunk, it makes sense to pack the data into the smallest format possible. Without applying any kind of compression, you can pack 8 boolean values into a single byte. So to describe a 2d area that has the size 16x16 ( == 256 tiles) you would only need 32 bytes (8bits per byte so we have 8*2*16 == 8bits*32 == 32 bytes). You may use larger chunks if it's just about 2d data. Also a common and recommended way to reduce the data that need to be send is to mark chunks that are completely empty or completely full as such. So you don't have to send the actual content of such a chunk but just that it's full or empty.
Such grid data is generally stored in a flattened array as it's usually the fastest solution. Though for rather small chunks (say 32x32) we could simply use a single dimensional array of 32 unsigned integer values. Each integer can represent 32 tiles (since an integer is a 32 bit number).
Of course your network protocol need to provide a way for a client to request a certain chunk section so the client can request the chunks around itself or even the whole map. It's also possible that the server decides what data the client should be able to see and just sends out messages for the chunks in range of the player.
We can't really get more into detail without knowing what networking solution you're using