I am creating a RPG that will take the player some time to complete. Therefor, I need to have gamesaves. Depending on if the player is in the middle of a battle, or in a menu in between battles, the structure of the gamesave file will change. The file structure would go something like this:
1 2 3 4 5 6
unit 1
name william
max-health 78
attack 45
defense 50
end-unit 1
How would I go through the file, find the names (attack,defense,name, etc) and then load the succeeding value?
Here is an idea : You can fix byte positions for each value...
Lets say bytes 1 to 4 (typically a 32 bit INT value) are reserved for the unit number (with values going from 0 to 4.294.967.295)
bytes 5 to 8 are reserved for the max-health
bytes 9 to 12 are reserved for the attack etc...
and you can reserved a defined number of bytes for strings (for example 50 bytes should be more than enough for a name...)
0|1111|222...(x50)...2222|3333|4444|5555|6666 give us a 1+4+50+4+4+4+4 = 71 bytes save file with :
- 0 the type of save format (so you can know how to parse the others bytes)
- 1 the unit number value
- 2 the name value
- 3 the max-health value
- 4 the attack value
- 5 the defense value
- 6 the end-unit value
This way you know when loading a file, that byte x to y is unit number and bytes y+1 to z is the second value etc etc etc.
Edit : if you know that the value will never go upper than 256 you can use 1 byte and not 4 etc. you need to adapt the model to fit your needs, and never change it later...
You just need to define an XML Schema which which can adequately represent the saved game structure, pick a suitable XML library, and then write the code to query and create XML DOM.
I would recommend that, at least to start, you pick save points in the game rather than letting them save the state at any point. This will simplify the structure of what you need to save.