To save a node, you call the save()
method on an instance of the com.jme3.export.binary.BinaryExporter
class. To restore the node, call the load()
method on an instance of the com.jme3.export.binary.BinaryImporter
class.
The following code snippet shows how you save a node as a binary file by overriding the stop()
method of SimpleApplication
:
Make a copy of
Main.java
and name the classSaveAndLoad.java
. Remember to also refactor the first line of themain()
method toSaveAndLoad app = new SaveAndLoad();
.Override the
stop()
method to save the game:@Override public void stop() { String userHome = System.getProperty("user.home"); BinaryExporter exporter = BinaryExporter.getInstance(); File file = new File(userHome+"/mycoolgame/savedgame.j3o"); try { exporter.save(rootNode, file); } catch (IOException ex) { Logger.getLogger(SaveAndLoad.class.getName()).log(Level.SEVERE, "Failed to save node!", ex); } super.stop(); }