I'm presently focusing on a videogame, and i wish to possess the user have the ability to save their character to a different file. I understand ways to use the file io (typically), but i've been while using 'serialize' to serialize an entire object (that consists of all of the variables for that character) and save it to some file. However , i'm constantly upgrading the item and making changes into it, then when i attempt to load that old character using the new object, it errors and crashes. Same goes with levels as-well (an item holding a couple of 2d-variety of variables).
There has to be an easy method to get this done so it's suitable for future versions. If there's a means, would anybody please offer some source code and/or perhaps a connect to a pleasant tutorial? All assistance is appreciated, thanks!!!
Use XML or perhaps an embedded database (fast and light-weight) for example Derby or H2. You can make use of an ordinary old qualities file.
Actually, find out if the qualities file is useful for you. And only when that will not work, try XML or even the embedded database approach.
if you're searching for java serializers this is actually the benchmark for you personally https://github.com/eishay/jvm-serializers/wiki/
Apache Avro appears to do well.
You are able to implement [cde] rather than Serializable, as well as in the
readExternal() techniques place the the logic to seeOrcreate the item. By doing this you've full charge of serialization/deserialization and may make changes fairly easily. Alternatively you should use JSON serialization by utilizing Gson. I wouldn't recommend XML, but when you need to you should check out xstream for the similar factor.
If you're stretching your objects in backwards compatible ways, i.e. add fields, and never getting rid of fields. Make certain you have declared a serialVersionUID according to the serializable javadoc.
One additional choice to consider since you are already using serialization, you can implement [cde] rather than [cde]. The code you utilize to serialize objects would stay the same. In your class you'd specify just how you would like it serialized by overriding
Be sure that you keep your order exactly the same when reading through and writing. Attempt to only add fields, however if you want to remove a area leave a gap to take into account old versions.
Ultimately though if you are making lots of changes it could better to change to a qualities or XML file as LES2 recommended. It will be more portable and readable this way.