# Saving and Loading Games

You can save and load scenes and individual Nodes using com.jme3.export.binary.BinaryExporter and com.jme3.export.binary.BinaryImporter. Use standard Java serialization to load game data or use the Savable interface. The jMonkeyEngine binary file format is .j3o. You can open, view, and edit .j3o files in the jMonkeyEngine SDK.

## Sample Code

## Saving a Node

      @Override
      public void destroy() {
        System.getProperty("user.home");
        BinaryExporter exporter = BinaryExporter.getInstance();
        File(userHome+"/somefile.j3o");
        try {
          exporter.save(rootNode, file);
        } catch (IOException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to save node!", ex);
        }
        super.destroy();
      }

## Loading a Node

      @Override
      public void simpleInitApp() {
        System.getProperty("user.home");
        BinaryImporter importer = BinaryImporter.getInstance();
        importer.setAssetManager(assetManager);
        File(userHome+"/somefile.j3o");
        try {
          Node loadedNode = (Node)importer.load(file);
          loadedNode.setName("loaded node");
          rootNode.attachChild(loadedNode);
        } catch (IOException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "No saved node loaded.", ex);
        }
        ...

## Custom Savable

If you have a custom class that you want to save or assign using `setUserData()`, the class must implement the `com.jme3.export.Savable` interface.

    import com.jme3.export.InputCapsule;
    import com.jme3.export.JmeExporter;
    import com.jme3.export.JmeImporter;
    import com.jme3.export.OutputCapsule;
    import com.jme3.export.Savable;
    import com.jme3.material.Material;
    import java.io.IOException;

    public class MyCustomClass implements Savable {
        private int      someIntValue;
        private float    someFloatValue;
        private Material someJmeObject;

        ...
        // your other code...
        ...

        public void write(JmeExporter ex) throws IOException {
            OutputCapsule capsule = ex.getCapsule(this);
            capsule.write(someIntValue,   "someIntValue",   1);
            capsule.write(someFloatValue, "someFloatValue", 0f);
            capsule.write(someJmeObject,  "someJmeObject",  new Material());
        }

        public void read(JmeImporter im) throws IOException {
            InputCapsule capsule = im.getCapsule(this);
            someIntValue   = capsule.readInt(    "someIntValue",   1);
            someFloatValue = capsule.readFloat(  "someFloatValue", 0f);
            someJmeObject  = capsule.readSavable("someJmeObject",  new Material());
        }
    }

For each class field that you want to save and load,

- Add one line that `write()`s the data to the JmeExport output capsule.
  - Specify the variable to save, give it String name (can be the same as the variable name), and specify a default value.
- Add one line that `read…()`s the data to the JmeImport input capsule.
  - On the left side of the assignment, specify the class field that you are restoring
  - One the right side, use the appropriate `capsule.read…()` method for the data type. Specify the String name of the variable (must be the same ones that you used in the `write() method`) and a default value.

view online version