Spatials (that is Nodes and Geometries) can contain audio and light nodes, particle emitters, controls, and user data (player score, health, inventory, etc). For your game distribution, you must convert all original models to a faster binary format. You save individual Spatials as well as scenes using com.jme3.export.binary.BinaryExporter
.
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.
com.jme3.export.Savable
spatial’s user data“
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 example overrides stop()
in SimpleApplication to save the rootNode to a file when the user quits the application. The saved rootNode is a normal .j3o binary file that you can open in the SDK.
assets
/* 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(Main.class.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.
[source,java]
---- @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);
}
----
–
== Custom Savable Class
JME's BinaryExporter can write standard Java objects (String, ArrayList, buffers, etc), JME objects (Savables, such as Material), and primitive data types (int, float, etc). If you are using any custom class together with a Spatial, then the custom class must implement the `com.jme3.export.Savable` interface. There are two common cases where this is relevant:
* The Spatial is carrying any <<jme3/advanced/custom_controls#,Custom Controls>>. +
Example: You used something like `mySpatial.addControl(myControl);`
* The Spatial's user data can contain a custom Java object. +
Example: You used something like `mySpatial.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:
[source,java]
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", 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()); } }
To make a custom class savable: . Implement `Savable` and add the `write()` and `read()` 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 the `write() method`), and again specify a default value. <tags><tag target="convert" /><tag target="j3o" /><tag target="models" /><tag target="load" /><tag target="save" /><tag target="documentation" /><tag target="serialization" /><tag target="import" /><tag target="export" /><tag target="spatial" /><tag target="node" /><tag target="mesh" /><tag target="geometry" /><tag target="scenegraph" /><tag target="sdk" /></tags>