- Home /
Saving a randomly generated Terrain
I have a randomly generated terrain and a functional Save/Load system using XML. In order to save the generated terrain, I tried serializing the heights in XML but it gives me an error (2d arrays aren't accepted as primitives). This is basically what I did :
var heights;
var xRes = Terrain.activeTerrain.terrainData.heightmapWidth; var yRes = Terrain.activeTerrain.terrainData.heightmapHeight; heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, xRes, yRes);
And then the data is serialized.
I tried serializing other types like int, string or even Array() and it works, so it's not the Saving system that causes the problem.
Are there any other methods to save this type of data? Would exporting the heightmap on the hard drive and importing it when loading the game work? If so, how can I export the heightmap from script?
For reference, this is the Save/Load code I'm using : http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML
Thanks for any answers.
That script can't handle 2D array serialization, obviously, so you'd either need to modify the script, or write your own serialization for it.
Answer by Tetrad · Jun 19, 2010 at 11:15 PM
You could use .NET's built-in serialization.
http://msdn.microsoft.com/en-us/library/4abbf6k0(v=VS.71).aspx
Basically, save your data using BinaryFormatter.
Example here: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx