Serializing a 2D array
I realize this question has been asked in the past but it has been at least 2 years and I'm praying that another solution has been found.
I have a game that is similar to chess and I'm wanting to make a Save Game option. The problem is the unit locations are saved in a Unit[,].
Is it true that the only way around this is to flatten the array or is there some other solution?
Answer by IgorAherne · May 25, 2016 at 11:15 PM
The thing with 2D array is they are still linear in memory. Typically, you would indeed, flatten it into a a long stripe, then serialize it into sequence of bytes.
During Loading, re-construct 2D array from those bytes, just like you would any other entity.
Also, check here https://social.msdn.microsoft.com/Forums/en-US/90c98754-2580-404a-81ae-aedba5f2604d/serialize-multidimensional-arrays?forum=csharplanguage
using System.Runtime.Serialization.Formatters.Binary;
...
int[,] theArray = new int[2,3]={...};
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, theArray);