What you end up doing is then basically switching on serialVersionUID to a series of private loader/saver delegate classes. This is sort of more complicated than just settling on a custom binary format in the first place in some ways but actually it’s quite a good solution, just not many people appear to think about this technique.
What happens is that if you add stuff to a class, it will load just fine. If you remove something (not transient), it will explode. You can work around it by doing your own serialization for that class something along the lines of what princec said, but it’s not a pretty solution, and it’s much MUCH more complex than just designing a proper file format in the first place.
Ah, that makes sense then. See, I’ve never done that.
I almost always end up either:
Saving my own text/binary file.
Serializing a SavedEntity class for everything that constructs the proper Entity when loaded.
The latter option gives me enough control that I so far have avoided any problems, which the former obviously won’t have issues. There’s also Externalizable if you want to get fancy and still use Java’s output streams.
XML is to a developer as taking a screenshot of a text document, drag and dropping it into a word document, zipping it, and emailing it is to a PHB. Both of these really happen.
Can I just add my kudos to all the others? Fun sandbox game, and it must be really cool to see all the positive comments on this around the place. Keep up the good work.
A little of topic but… why binary? A simple text format can be pretty compact, not compared to binary but is that an issue?
Text is more human readable but also editable. I couldn’t care less if some guy cheated to beat my game.
Am I missing something?
Easier for a machine to read and write. And therefore easier for you to code. Why deal with Integer.parseInt() when you can just read 4 bytes as an int and know it will work and not throw a NumberFormatException? and so on.
Sure, but if it breaks, and you have to do debugging, binary looks like a bunch of random bits, whereas you can probably figure out what went wrong when browsing through human readable data.