Use properties files as a save/load FILE

hey guys,
im implementing save/load in my game, but there are so many ways, im looking for an easy one, we can use XML, text files, Serialized objects, properties files,

i personally consider using property files, i think is the easy way,
but i want to know whats the best aproach to do that, for all kinds of games, scrollers, cenital RPG, platforms?

It depends on the game, on the data you want to store and how detailed you want to store it.
Example:
You have a gam with levels. You can say, only completed levels are saved. So you only need to save the number of the last completed level.
You can also have checkpoints in the level, so you need to save level number + checkpoint number.
You could also store the exact progress in the level, including the exact position, the enemies position, the enemies health etc.

In some games you may have a complex inventory you have to save, in others you might not have an inventory at all.
So it highly depends on the game and the data you want/need to store.
If the data is pretty simple, you might decide to store them as preferences. If the data gets more complex, you might want to store it as a textfile (for example xml).

Hi

I advise you to indicate the version of the file so that you have a chance to remain backward compatible, i.e that the latest version of your game can still read old versions of the save files.

Moreover, I advise you to implement this feature as late as possible in your game project. You’ll have a better idea of what you have to save and load when your game is stable and you’ll have less old versions to support except if you like frustrating your players.

I advise you to store the settings into a separate file.

Finally, I advise you to use XML or JSON only if your data are complicated enough to be worth it. Otherwise, a plain text file or a property file is enough. If you use the default Java binary serialization, you might have some problems with the serial version UID, you’ll have to set it everywhere to avoid problems with the automatically generated one.

Edit.: There are tons of APIs to handle XML, SimpleXML, XStream, JDOM, DOM, SAX, Java beans XML encoder and decoder, JAXB, Stax (JAXP), … The 5 last ones are parts of the standard Java API.

Edit.2: You can use java.util.Properties to read and write property files.

Edit.3: You can use JSON-Java to read and write JSON files.

thanks i think isn’t a big deal use properties file for save basic games,
and i would use JSON instead XML.

+1 for JSON, XML is a massive pain in the arse.
Actually +2 for HJSON, which is considerably more sensible than JSON.

Cas :slight_smile:

Easy way is serialization. Literally you write something like < 20 lines of code to both save and load the file. Then you just need to rebuild the graphics on load, which is easy as well, considering you have that code already.

Is it the best way, no, but it’s fast to implement. Basically zero backwards compatibility and file sizes can/do get quite large.

I’d really recommend not using java’s native serialization. It’s easy on the short term, but a major PITA on long term.
Just use a text-based serialization method like json. Libraries like gson make it just as easy as native java serialization, and the points where it isn’t as straight-forward as java serialization are important to address anyway.

I have to say I’ve rarely had any issues with Java serialization that aren’t the same issues you’ll get with any other persistent format. Except you can’t hand-edit the files of course.

Cas :slight_smile:

How? Other than what Cas said, and what I outlined in my post as downsides I can’t see of a reason not to use it as a simple easy solution. If you ever want anything nicer then yes by all means don’t use serialization.

It’s great for short-term serialization solutions. Long-term requires much more planning.

Cas :slight_smile:

The standard build-in XML encoder produces more compact (but still readable) XML code than XStream, think about using @Transient instead of the “transient” keyword. JAXB and JAXP have become better than XStream to me. Hjson seems to be less error prone than plain JSON.

If you don’t need any structure, just use property files.