Time for action – save and load game data

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:

  1. Make a copy of Main.java and name the class SaveAndLoad.java. Remember to also refactor the first line of the main() method to SaveAndLoad app = new SaveAndLoad();.
  2. 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"); ...

Get jMonkeyEngine 3.0 Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.