• 12
• 12
• 9
• 10
• 13

# World Saving

This topic is 3412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All - In the case of developing a WYSIWYG world tool in C#/XNA that allows for terrain editing, texturing, object placing, and possibly some scripting - how does the process of storing that world work? I'm an application programmer for a living but I've always loved graphics and I have experimented with implementing these features in code but I have never looked into how to actually store the world in a file so there is some form of persistence in world editing. Can anyone direct me to a few algorithms or techniques that would allow me to experiment with this?

##### Share on other sites
I'm writing something similar at the moment, also in C#/XNA.

(Well, in theory anyway. I don't have time to fully flesh out the editor part of this at the moment, so I'm designing the rest of my game to take account of the fact that the data will eventually be coming from an editor.)

One way to go, is to serialize the editor representation of the world as XML, and write a custom XNA pipeline extension to handle the conversion of the XML level data, to a format more suited for your game.

.NET has great support for XML, and the fact that XML is human readable, makes it really easy to create test data, or create simple test levels before the editor functionality is complete.

##### Share on other sites
Would XML saving/loading be slow with the amount of data possibly contained in a world file? XML seems a little messy for this type of operation... however, if XML is the way to go in a case like this that is what I will implement. Does anyone else have any ideas for some alternatives? Otherwise I will certainly look into using XML.

I suppose I could define my own file format. If I were to do that, does anybody know of any articles or books out there to read on parsing world data into a file? Although I've done alot of parsing, I've never worked with such algorithms.

Thanks for any advice...

Eric

##### Share on other sites
Quote:
 Original post by elondonWould XML saving/loading be slow with the amount of data possibly contained in a world file? XML seems a little messy for this type of operation... however, if XML is the way to go in a case like this that is what I will implement. Does anyone else have any ideas for some alternatives? Otherwise I will certainly look into using XML.I suppose I could define my own file format. If I were to do that, does anybody know of any articles or books out there to read on parsing world data into a file? Although I've done alot of parsing, I've never worked with such algorithms.
Convert your tiles to bytes somehow and write them into a binary data file, it will be faster than any other method.

For example (in C++, but should be easy to convert to C#), if your tiles are
struct tile{unsigned char tile_type;unsigned char something_else;unsigned short more_stuff;};
, you can easily convert one tile into one integer with
unsigned int new_integer = (tile_type << 24)|(something_else << 16)|(more_stuff);
Then just write it into a file with fwrite() and repeat as many times as you have tiles (if you want to write four chars instead of an integer if your tiles are not exactly 32 bits in size, just convert is with something like
char *data = new char[5];data = *((unsigned int*)new_integer);*(data+4) = 0;
You might consider saving map width and height somewhere too, so you can allocate memory for world when reading it from your database.

##### Share on other sites
If you want it to be fast, it hast to be simple.
Saving the data in a raw binary form is the way to go.
Using this method you wont have to parse for data.

edit: darn, you beat me to it...

##### Share on other sites
Quote:
 Original post by elondonWould XML saving/loading be slow with the amount of data possibly contained in a world file? XML seems a little messy for this type of operation... however, if XML is the way to go in a case like this that is what I will implement. Does anyone else have any ideas for some alternatives? Otherwise I will certainly look into using XML.I suppose I could define my own file format. If I were to do that, does anybody know of any articles or books out there to read on parsing world data into a file? Although I've done alot of parsing, I've never worked with such algorithms.Thanks for any advice...Eric

It's kind of irrelevant. If I understand your question correctly, you're referring to the act of serializing the world(s) you make in your editing program. All you need to do is figure out a consistent method for storing what kind of thing you have in the world along with what properties that thing has. Let's say for example purposes you're using a tile-based map system not unlike Neverwinter Nights. The first thing you'd probably want to include is a header. This might contain information general to your engine/application, such as some sort of identifier string, a level name, tileset, etc. Go hog wild here. Typically this would also hold some information regarding the actual contents of the file-- perhaps specifying the number of entities in the map, stuff like that. This is mostly dependent on what you need for your game.
Next you might decide it prudent to store how many tiles are in this particular area-- maybe two 32-bit unsigned integers for width and height. (overkill, but just for the sake of example) Read those, and then create an array of chars or whatever to store the tile types equal in size to your world. Then just copy the values for tile types out of your world file and into the array for use with your renderer. What you do with these specifically is outside of our scope here, but hopefully you can see what I'm getting at.

Once we're done with the world, etc. we come to the (slightly) more involved problem of entities-- things with a more complex set of properties. Fortunately, most of the things I mentioned carry over more or less without modification. The trick here is that there might be a mix of types and there are almost certainly more properties to manage. What you might want to consider is the use of a pluggable factory (article on the subject is here on GDNet, although I couldn't track it down) along with some ID bytes/tags and class-specific parsing functions. I'll leave you with this in mind as I don't want to cramp your style too much ;)

##### Share on other sites
Thanks for the direction guys, I appreciate it. This is exactly what I was looking for as it is difficult to google "saving a world". :)

Eric