- Home /
Saving world info
Hi,
I want to save and load a 3 dimensional(multidimensional array) int array, and it contains all my world info. And have been searching for hours, with no luck.
Answer by Xnary · Dec 10, 2012 at 11:05 PM
Okay, after hours of hairpulling i got it to work, starting by making the 3 dimensional array to a 1d, by doing this:
private int CalculateNewPos(int x, int y, int z) { return z + y * size.z + x * size.z * size.y; } private int[] CalculateOldPos(int pos) { int x = 0; int y = 0; int z = 0; if (pos >= size.y * size.z) { x = pos / (size.y * size.z); pos %= size.y*size.z; } if (pos >= size.z) { y = pos / size.z; pos %= size.z; } z = pos; return new int[]{x, y, z}; }
Then after i got a 1 dimensional array i could just use filestream and BinaryFormatter to get it to save and load.
Thanks for adding the final solution, much appreciated. It would be interesting to know why you chose this.
Because i have so many object to save, that if i used X$$anonymous$$L like you recommended, it would have taken up a lot more hard disk space. If i where only to save a few objects with float precision, i maybe would have chosen X$$anonymous$$L.
Out of curiosity, did you look into serializing? I'm curious if it's more efficient. If it's not (which I doubt), it would also 'hide' the data from the player.
Answer by FlowStateGames · Dec 10, 2012 at 09:31 PM
Though we could use a bit more information, I think that there is a generic solution that could avail you. C# can serialize data to files, and deserialize it when reading. This has the upside of making the data compact, and the downside of not allowing it to be human readable.
Serialized
Here's a link that should get you started with serialization. There's a bit of a learning curve, but if you're worried about performance, you can go this route pretty easily.
XML
If you want human-readable code, think about using XML. Here's a google start for the syntax.
For the schema, assuming that each dimension represents a location in the scene graph, something simple like this could work:
<world>
<worldX>
<worldY>
<worldZ item="car"/>
<worldZ/>
</worldY>
<worldY>
<worldZ/>
<worldZ item="boat"/>
</worldY>
</worldX>
<world>
Where your scene has a car at
world[0][0][0]and a boat at
world[0]['1]['1].
sorry for the apostrophes above, the markup was assuming I was trying to write a link.
Like I said, I can only make assumptions based on the limited information you give, but those are the two generic solutions which occur to me.
Thanks for your comment. And you're right, i didn't give enough information. But your method would have been great for a small worlds, but some information i forgot to include is that i need to save 8'750'000 objects, with an id(int) and the position is all ready written to the multidimensional array. Since i don't need more precision than an int can give me.
Your answer
Follow this Question
Related Questions
Loading data from a txt file - C# 3 Answers
Only able to save inside the editor problem 2 Answers
Can't get serialization Load/Save to work!C# 3 Answers
from bool array to binary file? 1 Answer
Filestream/SaveManager 2 Answers