[SOLVED] OutputCapsule need some help

Hi i need some help with OutputCapsule .
It looks pretty nice and useful ,but i wonder if i can make some sort of list or alse of capsuls.
Lets say i have several castles villages and soldiers,and i need to save them all and then read them all…
The problem is here https://wiki.jmonkeyengine.org/jme3/advanced/save_and_load.html
it teach me how to make a capsul for simple class made of classes existing in java ,like int string and soo on .
But lets say i need to take a list of Soldier’s that are made of ints and other made by me classes . And more than this
i actually need to have a list ,soo in game i will make a FOR and create all soldiers from all capsules …
Soo what i need is may be some massive usage of capsule or some thing similar …

Any one had any expirience or ideas ?

I cant paste mutch classes simply couse i have not even created all of them but soldier
Has all most common ints - lvl ,cash ,stamina soo on , while castles have houses ,walls other stuff ,houses has soldiers and soo on .

Is it possible to make entire self made classes saveble object ? soo lets say save list of soldiers and not only data soldier have ?

1 Like

JME’s serialization system (JmeImporter/JmeExporter/InputCapsule/OutputCapsule/Savable) should work for what you’re doing. The key is to put all your savable state into classes that implement the Savable (read/write) interface. InputCapsule and OutputCapsule provide methods for reading and writing arrays, arraylists, maps, and so on.

Or just use Java’s serialization.

…or GSON

…or JAXB.

There are many equivalent or better options than JME’s built in system in the long run.

by the way, in stable IDE i noticed that there is problem with removing/replacing/renaming any .ser files. i need do this out of IDE.

ehm could you please if possible give me a code sample of how it could look.
Like lets say you have 3 objects of class Shop and 10 objects of class Car in each shop, soo how could i save it properly ,and most important get to read them properly ?

 Class Shop {
  int cash;
   ArrayList <Car> list;
      }
      Class Car{
     int carType;
        }
1 Like

For class Shop, something like:

class Shop implements Savable {
    int cash;
    ArrayList<Car> list;
    public Car() {
        list = null;
    }
    public void read(JmeImporter im) throws IOException {
        InputCapsule ic = im.getCapsule(this);
        cash = ic.readInt("cash", 0);
        list = ic.readSavableArrayList("list", new ArrayList<Car>());
    }
    public void write(JmeExporter ex) throws IOException {
        OutputCapsule oc = ex.getCapsule(this);
        oc.write(cash, "cash", 0);
        oc.writeSavableArrayList(list, "list", new ArrayList<Car>());
    }
}

For class Car, something like:

class Car implements Savable {
    int carType;
    public Car() {
        carType = 0;
    }
    public void read(JmeImporter im) throws IOException {
        InputCapsule ic = im.getCapsule(this);
        carType = ic.readInt("carType", 0);
    }
    public void write(JmeExporter ex) throws IOException {
        OutputCapsule oc = ex.getCapsule(this);
        oc.write(carType, "carType", 0);
    }
}
1 Like

should be right now :smiley:
I 've got it how to save one object ,but dont know how to save lots of objects ,like a full list of objects with their lists of objects.
And more complicated is how to load a list of objects and create list of objects from incapsulated list

Oh soo if i make Car Savable i can actually save object of Car Class or a list of Car objects ?

Thank you wery mutch for your help , i have one more little question.
IF i have a

          Class Car{
           int carType;
          Shop shopWhereItFrom;
              }

will it create a new shop for a car ,or will it just connect all cars to one shop?

1 Like

If the JmeImporter hasn’t already seen the shopWhereItFrom object, it will create a new one. If it has seen it before, then I believe it uses the pre-existing one.

1 Like

Thank you ,but i would try to stay in limits ,and get to some thing simple if possible,but thank you :slight_smile:

Thank you :slight_smile: for all your help, it helped me really mutch

1 Like

Ok… not sure what’s simpler than “implements Serializable” but you can do the harder/uglier JME way if you like.

1 Like

Java serialization versions of Car and Shop:

class Shop implements Serializable {
    int cash;
    ArrayList<Car> list;
    public Car() {
    }
}

class Car implements Serializable {
    int carType;
    public Car() {
    }
}

And in this case, GSON versions of Shop and Car:

class Shop {
    int cash;
    ArrayList<Car> list;
    public Car() {
    }
}

class Car {
    int carType;
    public Car() {
    }
}

…because there are no shared references, GSON will work without anything special.

Java’s built in serialization is super powerful, though. Can handle complex object graphs, overriding read/write if necessary, etc… I think the only feature JME has over it is the ability to alternately output XML… but it gives up a lot and is way uglier to do that. Plus, I think there are already tools to examine Java serialization files. (If not then I should write one because it would be simple.)

1 Like

Well car refers to shop and shop to car ,and lots of car refers to one shop ,while one shop refers to lots of cars, so we have all types of refferences

Either way, the relationship should be based on IDs. The cars will have a shop ID as well as an ID themselves. Databases love these relationships.

Java serialization will still work.

The only issue with java serialization is that generally people use it without any “class version management”, so old saves often quickly become unreadable. In many contexts this doesn’t matter. But in some, it does.

I am using JME serialization for mesh data. Seems fine to me. Any reason why i shouldn’t use it?

1 Like

You have identical problems with JME’s format. There is no magic to serialization… except at least in Java serializations case if you set the serialization version of the class then a bunch of stuff happens automatically for you.

It’s ugly and limiting. You have to write all kinds of serialization code just to make it work as good as Java serialization does without any of that code.