views:

94

answers:

2

I'm writing an Android game in Java and I need a robust way to save and load application state quickly. The question seems to apply to most OO languages.

To understand what I need to save: I'm using a Strategy pattern to control my game entities. The idea is I have a very general Entity class which e.g. stores the location of a bullet/player/enemy and I then attach a Behaviour class that tells the entity how to act:

class Entiy { float x; float y; Behavior b; }

abstract class Behavior { void update(Entity e); {}

// Move about at a constant speed
class MoveBehavior extends Behavior { float speed; void update ... }

// Chase after another entity
class ChaseBehavior extends Behavior { Entity target; void update ... }

// Perform two behaviours in sequence
class CombineBehavior extends Behavior { Behaviour a, b; void update ... }

Essentially, Entity objects are easy to save but Behaviour objects can have a semi-complex graph of dependencies between other Entity objects and other Behaviour objects. I also have cases where a Behaviour object is shared between entities. I'm willing to change my design to make saving/loading state easier, but the above design works really well for structuring the game.

Anyway, the options I've considered are:

  • Use Java serialization. This is meant to be really slow in Android (I'll profile it sometime). I'm worried about robustness when changes are made between versions however.

  • Use something like JSON or XML. I'm not sure how I would cope with storing the dependencies between objects however. Would I have to give each object a unique ID and then use these IDs on loading to link the right objects together? I thought I could e.g. change the ChaseBehaviour to store a ID to an entity, instead of a reference, that would be used to look up the Entity before performing the behaviour.

I'd rather avoid having to write lots of loading/saving code myself as I find it really easy to make mistakes (e.g. forgetting to save something, reading things out in the wrong order).

Can anyone give me any tips on good formats to save to or class designs that make saving state easier?

+1  A: 

You should definitely check serialization before using it. I don't know how it stands on Android, but for Java code, it's a known and efficient way to save objects graphs. Anyway, you can also take a look at replies to this question, which considers saving an object graph using XML.

Riduidel
Thanks. I've read developers from Google say serialization is super super slow. If I don't have many objects (I might need to save about 200-odd small objects), it might be fast enough. I really hate writing save/load code; it's so easy to make mistakes! I'd be interested to know about the alternatives as well.
mrteacup
A: 

You haven't said why you want to save/load state. If you want to protect against shutdown, you might want to look at using Bundle and PathClassLoader along with onSaveInstanceState and onRestoreInstanceState/onCreate.

Mark Borgerding
I need to save state if the OS decides to kill my activity so the player can return to their game. Also, I may make it the player can save/restore games at will. I know about Bundle etc. but that doesn't help with how I actually store the data, seeing as regular serialization is meant to be really slow.
mrteacup