Saving and Loading Game State

Howdy guys,

I was wondering if anyone can give me some tips about how to create a decent method for saving and loading the sate of my game. I thought about making all the classes in the game implement the Serializable interface and then output all of the objects to a file. However, some of my objects have properties such as their material which cannot be serialized. The properties I want to store are things like the position, velocity and orientation of all the objects in the game as well as a few game properties such as the players’ scores. Somehow I need to store all of these properties into a single object and also be able to restore all the properties when loading the game. What solutions to this problem are there?

No no no bad bad bad. This kind of midless Serialization leads to performance disasters on load and save.

Serialization can be useful but you need to think carefully about what you really want to save and only Serialize that.

Either well isolate just those thinsg in small number of seperate classes and use Transient to make absolutely sure you arent pulling in otther refernced objects and their data OR just open a DataOutputStream aroudna FileOutputStream for writing it out and a DataInputStream around a FIleInput Stream for reading abck and write logic to ull the values out/create obejcta s necessary when reading back.

TANSTAAFL

Ok, well I got over my problem by making the things I didn’t need to save transient. Still l don’t know if that’s the best way to do it. I still have to load up the correct materials and textures when I load the game state. Still I can’t think of any other way short of making seperate classes for each object that hold all the info I need to save.

Anyway still open to more suggestions…

Here’s a simple one: whatever you’re saving, start out by saving it in a human-readable way. You can always hide it later, but it can make your life a lot easier when you’re testing your save and load stuff if you can actually read the saved files.

Best way? probably not. Its likely to be both bigger and slower then a properly thought out save,
OTOH its probably the least work so i its good enough, then its good enough.

But Im not sure why you are saving model properties (materails and textures) or any 3D data actually, aren’t those already part of the model files you load to start the game??