2 <h1><a>Saving and Loading Games (.j3o)</a></h1>
7 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 <code>com.jme3.export.binary.BinaryExporter</code>.
11 The jMonkeyEngine's binary file format is called <code>.j3o</code>. You can convert, view and edit .j3o files and their materials in the jMonkeyEngine <a href="/com/jme3/gde/docs/sdk.html">SDK</a> and compose scenes (this does not include editing meshes). For the conversion, you can either use the BinaryExporters, or a context menu in the <acronym title="Software Development Kit">SDK</acronym>.
15 <p><div>The jMonkeyEngine's serialization system is the <code>com.jme3.export.Savable</code> interface. JME3's BinaryExporter can write standard Java objects, JME3 objects, and primitive data types that are included in a <a href="/com/jme3/gde/docs/jme3/advanced/spatial.html">spatial's user data</a>. If you use custom game data classes, see below how to make them "Savable".
20 There is also a com.jme3.export.xml.XMLExporter and com.jme3.export.xml.XMLImporter that similarly converts jme3 spatials to an <acronym title="Extensible Markup Language">XML</acronym> format. But you wouldn't use that to load models at runtime (quite slow).
24 <!-- EDIT1 SECTION "Saving and Loading Games (.j3o)" [1-1239] -->
25 <h2><a>Sample Code</a></h2>
28 <li><div> <object classid="java:org.netbeans.modules.javahelp.BrowserDisplayer"><param name="content" value="http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/tools/TestSaveGame.java"><param name="text" value="<html><u>TestSaveGame.java</u></html>"><param name="textColor" value="blue"></object></div>
33 <!-- EDIT2 SECTION "Sample Code" [1240-1397] -->
34 <h2><a>Saving a Node</a></h2>
39 The following example overrides <code>stop()</code> 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 <a href="/com/jme3/gde/docs/sdk.html">SDK</a>.
43 <p><div>Note that when you save a model that has textures, the references to those textures are stored as absolute paths, so when loading the j3o file again, the textures have to be accessible at the exact location (relative to the assetmanager root, by default the <code>assets</code> directory) they were loaded from. This is why the <acronym title="Software Development Kit">SDK</acronym> manages the conversion on the project level.
46 <pre> /* This is called when the user quits the app. */
48 public void stop() {
49 String userHome = System.getProperty("user.home");
50 BinaryExporter exporter = BinaryExporter.getInstance();
51 File file = new File(userHome+"/Models/"+"MyModel.j3o");
53 exporter.save(rootNode, file);
54 } catch (IOException ex) {
55 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error: Failed to save game!", ex);
57 super.stop(); // continue quitting the game
61 <!-- EDIT3 SECTION "Saving a Node" [1398-2548] -->
62 <h2><a>Loading a Node</a></h2>
67 The following example overrides <code>simpleInitApp()</code> in SimpleApplication to load <code>Models/MyModel.j3o</code> when the game is initialized.
70 public void simpleInitApp() {
71 String userHome = System.getProperty("user.home");
72 assetManager.registerLocator(userHome, FileLocator.class);
73 Node loadedNode = (Node)assetManager.loadModel("Models/MyModel.j3o");
74 loadedNode.setName("loaded node");
75 rootNode.attachChild(loadedNode);
80 <p><div>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.
85 <!-- EDIT4 SECTION "Loading a Node" [2549-3302] -->
86 <h2><a>Custom Savable Class</a></h2>
91 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 <code>com.jme3.export.Savable</code> interface. There are two common cases where this is relevant:
95 <li><div> The Spatial is carrying any <a href="/com/jme3/gde/docs/jme3/advanced/custom_controls.html">Custom Controls</a>. <br/>
96 Example: You used something like <code>mySpatial.addControl(myControl);</code></div>
98 <li><div> The Spatial's user data can contain a custom Java object. <br/>
99 Example: You used something like <code>mySpatial.setUserData("inventory", myInventory);</code></div>
104 If your custom classes (the user data or the Controls) do not implement Savable, then the BinaryImporter/BinaryExporter cannot save the Spatial!
108 So every time you create a custom Control or custom user data class, remember to implement Savable:
110 <pre>import com.jme3.export.InputCapsule;
111 import com.jme3.export.JmeExporter;
112 import com.jme3.export.JmeImporter;
113 import com.jme3.export.OutputCapsule;
114 import com.jme3.export.Savable;
115 import com.jme3.material.Material;
116 import java.io.IOException;
118 public class MyCustomClass implements Savable {
119 private int someIntValue; // some custom user data
120 private float someFloatValue; // some custom user data
121 private Material someJmeObject; // some custom user data
124 // your other code...
127 public void write(JmeExporter ex) throws IOException {
128 OutputCapsule capsule = ex.getCapsule(this);
129 capsule.write(someIntValue, "someIntValue", 1);
130 capsule.write(someFloatValue, "someFloatValue", 0f);
131 capsule.write(someJmeObject, "someJmeObject", new Material());
134 public void read(JmeImporter im) throws IOException {
135 InputCapsule capsule = im.getCapsule(this);
136 someIntValue = capsule.readInt( "someIntValue", 1);
137 someFloatValue = capsule.readFloat( "someFloatValue", 0f);
138 someJmeObject = capsule.readSavable("someJmeObject", new Material());
143 To make a custom class savable:
146 <li><div> Implement <code>Savable</code> and add the <code>write()</code> and <code>read()</code> methods as shown in the example above.</div>
148 <li><div> Do the following for each non-temporary class field: </div>
150 <li><div> Add one line that <code>write()</code>s the data to the JmeExport output capsule. </div>
152 <li><div> Specify the variable to save, give it a String name (can be the same as the variable name), and specify a default value.</div>
156 <li><div> Add one line that <code>read???()</code>s the data to the JmeImport input capsule. </div>
158 <li><div> On the left side of the assignment, specify the class field that you are restoring</div>
160 <li><div> On the right side, use the appropriate <code>capsule.read???()</code> method for the data type. Specify the String name of the variable (must be the same as you used in the <code>write() method</code>), and again specify a default value.</div>
170 <p><div>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.
174 <a href="/wiki/doku.php/tag:convert?do=showtag&tag=tag%3Aconvert">convert</a>,
175 <a href="/wiki/doku.php/tag:j3o?do=showtag&tag=tag%3Aj3o">j3o</a>,
176 <a href="/wiki/doku.php/tag:models?do=showtag&tag=tag%3Amodels">models</a>,
177 <a href="/wiki/doku.php/tag:load?do=showtag&tag=tag%3Aload">load</a>,
178 <a href="/wiki/doku.php/tag:save?do=showtag&tag=tag%3Asave">save</a>,
179 <a href="/wiki/doku.php/tag:documentation?do=showtag&tag=tag%3Adocumentation">documentation</a>,
180 <a href="/wiki/doku.php/tag:serialization?do=showtag&tag=tag%3Aserialization">serialization</a>,
181 <a href="/wiki/doku.php/tag:import?do=showtag&tag=tag%3Aimport">import</a>,
182 <a href="/wiki/doku.php/tag:export?do=showtag&tag=tag%3Aexport">export</a>,
183 <a href="/wiki/doku.php/tag:spatial?do=showtag&tag=tag%3Aspatial">spatial</a>,
184 <a href="/wiki/doku.php/tag:node?do=showtag&tag=tag%3Anode">node</a>,
185 <a href="/wiki/doku.php/tag:mesh?do=showtag&tag=tag%3Amesh">mesh</a>,
186 <a href="/wiki/doku.php/tag:geometry?do=showtag&tag=tag%3Ageometry">geometry</a>,
187 <a href="/wiki/doku.php/tag:scenegraph?do=showtag&tag=tag%3Ascenegraph">scenegraph</a>,
188 <a href="/wiki/doku.php/tag:sdk?do=showtag&tag=tag%3Asdk">sdk</a>
192 <!-- EDIT5 SECTION "Custom Savable Class" [3303-] -->
193 <p><em><a href="http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:save_and_load?do=export_xhtmlbody">view online version</a></em></p>