How do you write SaveGame feature?

Hi guys,
I was wondering how do you write SaveGame feature?
Long time ago, when my game used only String’s and int’s I used to use Serializable interface.

But now I stuck with this stupid problem, because of BufferedImage…
I know that there must be better solution than serializing objects.

Greetings,
Sonic96PL

I believe you can serialize ImageIcons, so if you’d change your BufferdImage to be an ImageIcon you could serialize it.

I guess that’s not really the solution you are looking for though :stuck_out_tongue:

No, saving textures is impractical…
I thought that XML can be nice, but I don’t know.

It really depends on the type of game you are making. Obviously save data for an RPG is harder (in my opinion) that saving data for an RTS. How I do it in my most recent game (an RTS) is what I like to call saving the state. A player can not save in the middle of a battle (other games allow you to, I don’t because it is easier) so the only data I have to save is player information, like hours played, times won / lost, a score (if i want there to be one) and what level in the campaign they are currently at. That is it! All of this data is saved as bytes in a txt file.

Make it per stage/level checkpoint, then you only need to store what the player had when they reached that checkpoint.

Man, I recently made a thread like this. I ended up implementing a HashMap and writing it to a file. I used Serializables, although apparently there are some alternatives that are more effective than Java’s built in Serialization. The save data might be something like…


public class SaveData
{
    private HashMap saveData<String, Serializable>;

    public void addProperty(String key, Serializable value)
    {
        saveData.put(key, value);
    }

    public void removeProperty(String key)
    {
        saveData.remove(key);
    }

    // I don't feel like writing this.  Just know that you can use an ObjectOutputStream to write a Serializable object to a file, and an ObjectInputStream
    // to read an object from a file.  It should save the Object as it is so long as you did everything right.  I recommend looking up how to use the
    // Serializable Interface.  In conclusion, you'd be planning to write the HashMap to a file.
    public void save()
    {
    }

    // Yeah, use an ObjectInputStream to read the HashMap into the SaveData if it exists.  Otherwise, make a new HashMap
    public void load()
    {
    }
}

Keep in mind that not everything about an object can be serialized. The attributes of an object will become Serialized along with the object so long as they either implement the Serializable Interface, or are of a primitive data type. A HashMap implements the Serializable interface, so it is fair game.

Yeah, and this code won’t run… Haven’t tested it. Just demonstrates.

Thanks, that’s what I was looking for.

Greetings,
Sonic96PL