Loading/saving using the Savable interface

Hi , this is my first post here ! I’ve been playing with jMonkey for some time, it’s great by the way, and I have some questions concerning saving and loading of games using the Savable system:

I’ve checked this thread , this page of the wiki and this template, but…

Say I got:

class BigBox implements Savable {

private SmallBox aField = new SmallBox();

public void write(JmeExporter ex) throws IOException {
OutputCapsule capsule = ex.getCapsule(this);
capsule.write(aField, “aField”, new SmallBox());
}

public void read(JmeImporter im) throws IOException {
    InputCapsule capsule = im.getCapsule(this);
    aField = capsule.readSavable("aField", new SmallBox() );
}

}
class SmallBox{
String colour = “red”;
}

  1. Does SmallBox needs to implements savable ?
  2. If so, to load my game, do I need to call read() on both my objects for the whole thing(a small red box in the big box) to be complete ? If so, is there a specific order in which to do so ?
  3. Is there a way to iterate through my saved game to instantiate all the objects it contains ? For example, I save 100 instances of BigBox to one file, then I want to re-create as many instances of BigBox as there is saved (knowing that each BigBox has different value in it’s fields and that ) ?

I know this might be a bit basic, but if you could point me in the right direction that would be very cool. Sorry for the long post & thanks for any help…

  1. Yes it does. you can’t write arbitrary non savable objects except few exception like Float, String, Booleans, ArrayLists, pretty much the basic stuff. SmallBox also have to implement read and write and take care about its own fields writing and reading.

  2. You never call read yourself. read is called by the loader. Make your SmallBox Savable and in the BigBox read() call readSavable(“aField”,SmallBox());
    Then when you want ot load your game call one of the loadGame methods of the SaveGame class

  3. Encapsulate a list (ArrayList) of BigBox in another Savable (let’s say BigBoxes). input and output capsules have methods to read and write ArrayLists of savables. The BigBoxes read and write methods needs to use these.