Saving game data to file/loading it

Hi, I’ve recently started looking at how I can save game data, such as scores, ammo etc. I can’t seem to find an easy way to do it, if anyone could tell me/point me in the right direction of how I can easily do this. I am not looking to save many variables, just a few integers and strings.

Apologies if this seems a nooby question, I’m just getting confused with it all.

The serializable interface is what you might be looking for

Searching that now, will edit it once I’ve looked/tried it. Thankyou

EDIT: Thankyou! This seems to be working appreciation to you:slight_smile:

Writing to a text file would be really easy, but then people can access it and change their stats. Apart from that I think that is very easy.

Well the main thing for serializing is for making the file smaller.

I suggest making a singleton status object, serializing it, and saving the object as a whole.

Google Object saving and singletons for Java.

Very simple and effective.

But the issue with serialization is any changes to the class can potentially render it unusable, just to be aware of it.

That’s why you add a serial version ID. Java will convert it for you most of the time.

e.g.:

public class DataManager implements Serializable{

	private static final long serialVersionUID = 1L;

By using the native serialization of Java you lock your code very close to the data representation on disk. It’s easy, but if you later want to do things differently, you might find this solution a bit inflexible.

If you want small files, use GzipOutputStream.
If you want flexibility, try XML exports.

If you want security, try some sort of encryption and file signing. There is nothing secure if the player can access the file, but it raises the bar notably for such attempts. Often even the simple old XOR scrambling is enough to hide data from curious eyes.

Personally I use text files and FileWriter/BufferedReader for most things. I can always supply them with compressing and/or crypting Input/Output streams, and for debugging I can peek easily into the files.

I am very happily using Kryo now.

And using the CompatibleFieldSerializer I have exactly the behavior I need:
Any fields that a loaded save-game file has, that my code doesnt have (anymore) will be ignored.
Any fields that might be missing from a save-game file, will just left out, hence those fields will be NULL, or actually the default value.

It’s very nice, since, I use save-games for debugging as well: I walk around the world, do stuff; and then when I stop the game it automatically saves a auto-save-game, which will be load be default on next startup.
So in debugging wherever I go, whatever I do its saved… which is really helpful when you have to restart and need to have what you just did.

And it helps to avoid save-game compatibility problems for end-users if implemented properly.

Is this really true in the 21st century?

In my experience, no. Try to serialize a byte array with four elements. You’ll end up with abaut 200 or more bytes of serialized data, instead of four. Even compressed (gzip) it still will be much more than four bytes.

Ok, this is an extreme example, but Java serialization comes with an overhead.

I got 28 bytes

My memory was off quite a bit there, indeed. Also I had the byte array as a member of an object, which added more overhead, but most likely still not 200 bytes. It’s been a long time since I did those tests.

Thanks for the correction :slight_smile:

With Java’s built-in serialization the overhead is in the form of headers that are written per type the first time the type is encountered. This means the overhead is high percentage-wise for a small amount of data, but mostly negligible for a large amount of data. There are plenty of other reasons to avoid the built-in serialization though. :slight_smile: