Save game state

Hello again!
I’ve started working on a game, and I’ve made a save file in which I write a serializable object using the following code. The only problem I’m having is that every time I modify one of the classes which are written with the save, the file becomes incompatible with the current version of the software, therefore I need to make a new file everytime I modify something, loosing any backwards compatibility. I was thinking about using json, or even writing each field manually without using automatic serialization. Do you have any recommandations?

public static byte[] serialize(Object object) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(object);
        return out.toByteArray();
}

public static void writeBytes(File file, byte[] data) throws Exception{
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(data);
        fileOutputStream.close();
}

I would not go with serialization. If you want to stick with it, you are going to keep looping back to that problem. I would simply save different attributes to a file, and read them back. Like a .cfg file!

The problem of saving all game data manually to a file is that it is easy to cheat, just changing the file values.

While that’s true, why would you care? Unless you are working for a major company that will distribute thousands of copies of a game, you don’t need to worry about the hassle of cheating. Its a pain in the ass to stop hackers, and honestly, why do you care? Let people play their game how they want to. It’s not worth it to secure your save data, but it is a interesting topic to look into.

But what’s better: stopping people cheating the game in the future or making development easier so the game actually gets made? But seriously it’s a good point but you can always change back to serialization later.

In addition to my first post, there is a utility built into java for doing this, for your convenience.

If you want it secured you can hash/encrypt a bit.

Thank you for answers. Cheating is not something I would consider preventing, because it would require much more time than it is worth it. I’m considering as viable alternatives writing all the data manually to a file, and then encrypting it so it may not be as easily accessible, or even using the Java Properties. Also another solution might be XML or JSON serialization.

Ok, if you go with XML, a few things:

Files will be enormous if you are saving the level itself, like the tile data. Use it to save entities if you want.

We have attributes. Use them:


// No No No No No!
<playerx>10</playerx>
<playery>10</playery>
<playertex>res/test/texture.png</playertex>

// Yes Yes Yes Yes Yes!
<player x="10" y="10" tex="res/test/texture.png"></player>

Do not see it as an easy way of doing things, think of it as an organized way of doing things. Too many people using XML I see are using it as a shortcut, and using it to store EVERYTHING. Every data type has its place, and XML is no exception.

Just remember this your goals for data storage, and you should be okay:

  • Keep it small
  • Keep it informative

I hope this helps,
-wes :wink:

And if were on the subject of keeping it small,

[quote]
[/quote]
could be just

[quote]
[/quote]
Every byte helps.

XML should, in my opinion, only be used for special situations. It should not be used for saving large amounts of similar objects (objects in a world). I find it has a lot of excess “stuff” that wastes memory, just so a human can read it. Why do you need to read it? Save the memory and develop a format that is efficient. There is absolutely no point in wasting space when the computer can decipher custom formats that a human can’t easily.

I had this crazy idea the other day.
I was thinking about qr-code and how they translate image to text and, why not a library to generate a encrypted image that can store all your game data?
Parse one image is not that hard and it is fast. Also, is hard to cheat one qr-code. ;D

[quote]encrypted
[/quote]
Nope. You don’t need to keep anything a secret, so why try? Plus, it’s java.

I did that for a while with a project a while back. It was called Horde. It’s dead now, and I did not keep the code for it. But it does not have to be a qr-code. Just make an image, and read the RGB values off of the image. Less trouble.

I’ve actually thought about this too; would be a fun project, though not entirely practical.

Actually, that would be fun for sharing! If you had a level on a phone game, and you wanted to give out a level, just upload it, and it is on the web!

Relevant library zxing meant for just this.