Saving and Loading Games (.j3o)¶
Spatials (nodes and geometries) can contain audio nodes, lights, particle emitters, controls and user data such as player score or inventory. You can save individual spatials or scenes with com.jme3.export.binary.BinaryExporter. Converting every model to .j3o is not required for distribution: a project generated by the Initializer can also package and load glTF directly.
The jMonkeyEngine’s binary file format is called .j3o. You can convert, view and edit .j3o files and their materials in the jMonkeyEngine SDK and compose scenes (this does not include editing meshes). For the conversion, you can either use the BinaryExporters, or a context menu in the SDK.
Important
jMonkeyEngine serialization uses the com.jme3.export.Savable interface and the supported capsule types. Spatial user data supports specific built-in types and Savable objects; arbitrary Java objects are not automatically serializable. See below for custom data classes.
There is also a com.jme3.export.xml.XMLExporter and com.jme3.export.xml.XMLImporter that similarly converts jme3 spatials to an XML format. But you wouldn’t use that to load models at runtime (quite slow).
Sample Code¶
Saving a Node¶
The following desktop example overrides stop() in SimpleApplication to save the root node when the application stops. The saved root node is a .j3o file that you can open in the SDK. Mobile platforms have different lifecycle and writable-storage requirements; use a suitable platform-specific location and save at an appropriate lifecycle event.
Warning
A saved model can retain texture asset keys. When you load it again, those textures must be available through a registered locator at the corresponding asset keys; the .j3o is not necessarily a self-contained bundle.
/* This is called when the user quits the app. */
@Override
public void stop() {
String userHome = System.getProperty("user.home");
BinaryExporter exporter = BinaryExporter.getInstance();
File file = new File(userHome+"/Models/"+"MyModel.j3o");
try {
exporter.save(rootNode, file);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Error: Failed to save game!", ex);
}
super.stop(); // continue quitting the game
}
Loading a Node¶
The following example overrides simpleInitApp() in SimpleApplication to load Models/MyModel.j3o when the game is initialized.
@Override
public void simpleInitApp() {
String userHome = System.getProperty("user.home");
assetManager.registerLocator(userHome, FileLocator.class);
Node loadedNode = (Node)assetManager.loadModel("Models/MyModel.j3o");
loadedNode.setName("loaded node");
rootNode.attachChild(loadedNode);
}
Tip
Here you see why we save user data inside spatials – so it can be saved and loaded together with the .j3o file. If you have game data outside Spatials, you have to remember to save() and load(), and get() and set() it yourself.
Custom Savable Class¶
BinaryExporter supports the types exposed by its capsules, including primitives, strings, supported collections, buffers and Savable objects such as materials. A custom object stored in a spatial's user data must implement com.jme3.export.Savable. There are two common cases where this is relevant:
-
The Spatial is carrying any Custom Controls.
Example: You used something likemySpatial.addControl(myControl); -
The Spatial’s user data can contain a custom Java object.
Example: You used something likemySpatial.setUserData("inventory", myInventory);
If your custom classes (the user data or the Controls) do not implement Savable, then the BinaryImporter/BinaryExporter cannot save the Spatial!
So every time you create a custom Control or custom user data class, remember to implement Savable:
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; // some custom user data
private float someFloatValue; // some custom user data
private Material someJmeObject; // some custom user data
...
// 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", null);
}
public void read(JmeImporter im) throws IOException {
InputCapsule capsule = im.getCapsule(this);
someIntValue = capsule.readInt( "someIntValue", 1);
someFloatValue = capsule.readFloat( "someFloatValue", 0f);
someJmeObject = (Material) capsule.readSavable("someJmeObject", null);
}
}
To make a custom class savable:
-
Implement
Savableand add thewrite()andread()methods as shown in the example above. -
Do the following for each non-temporary class field:
-
Add one line that
write()s the data to the JmeExport output capsule. -
Specify the variable to save, give it a 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
-
On the right side, use the appropriate
capsule.read…()method for the data type. Specify the String name of the variable (must be the same as you used in thewrite()method), and again specify a default value.
-
Important
As with all serialization, remember that if you ever change data types in custom classes, the updated read() methods will no longer be able to read your old files. Also there has to be a constructor that takes no Parameters.
Default Value¶
The default value plays an important role in what data is saved to file.
write()
public void write(int value, String name, int defVal) throws IOException {
if (value == defVal)
return;
writeAlias(name, BinaryClassField.INT);
write(value);
}
The write methods of the BinaryOutputCapsule.java class do not write the defVal to file. Instead, they check to see if value is equal to defVal, and if so, will not write anything at all.
There are very good reasons to do this.
-
It takes less space if everything is a default value.
-
You may decide on new defaults later and your objects will automatically upgrade if they didn’t have specifically overridden values.
read()
public int readInt(String name, int defVal) throws IOException {
BinaryClassField field = cObj.nameFields.get(name);
if (field == null || !fieldData.containsKey(field.alias))
return defVal;
return ((Integer) fieldData.get(field.alias)).intValue();
}
When reading your saved file, the BinaryInputCapsule.java class will see that the name field is null and this is when the defVal is set.
Note
If you rely on the compiler to initialize class or instance variables for you, this can lead too unintended consequences.
For example:
capsule.write(someIntValue, "someIntValue", 1);
If you let the compiler initialize someIntValue, it will initialize to zero and if it’s not changed after initialization, zero will be written to file.
someIntValue = capsule.readInt( "someIntValue", 1);
Now when read is called, it will see the “someIntValue” name and set the someIntValue variable to zero. Not one, as you were expecting. Keep this in mind when using Savable.