Friday, March 22, 2019

java - What is a good file format for saving game data?




I need to save some custom game data. Map, player, etc.


All of them will have "sub objects". For example, a map and map will have an "array" of tiles. ie, hierarchical data. Hopefully nothing binary.


What would be a good format for these?



So far I'ved considered:


Serailization: This is FAST and easy, but tends to break when I change the underlying classes :(


XML: I really hate parsing this. My test case was over 100+ lines of code and seemed likes tons of "busy work" for even a very simple format.


INI: would be really clumsy for hierarchical data.


Protobuf: Never used it, but read you have to do a lot of manual mucking around and breaks if you change the class.


Other options? That's why I'm here!


Edit: this is Java btw.


Edit 2:


I settled on "Controlled Binary Serialization" (see below).


Pros:





  • it's fast




  • it's small (on disk) and can be easily compressed/decompressed during read/write.




  • it's super easy to read/write from game and toolset.





  • I can decide what to include/exclude of the object.




  • Objects/Data can be nested.




Cons:





  • Can't edit it by hand (like XML, YAML, etc)




  • Can't easily read/modify it with scripts




  • Java Serialization by default is pretty slow/bloated compared to other implentations, but it's stable and works





Answer




To display hierachical data, YAML or JSON would be good options. They are far simpler and easier to parse than XML.


Another option would be a "controlled" binary serialization process. Every object writes it's state out in a controlled way, i.e.


void player::save(savegame &sgm)
{
sgm.write(this->position);
sgm.write(other properties);
inventory.save(sgm);
}

id Tech 4 (Quake 4 / Doom 3 engine) uses such an approach.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...