Skip to content

Saving data to disk and reading from disk

Saxion Game App has methods to save and load objects to disk. You can use binary files for fast serialization of any object or JSON files for human-readable data. Usually the binary files are used for objects that should not be readable by the user on disk. This is suitable for save games. Settings can be stored in JSON files, that can also be adjusted outside the game.

Binary Save and Load

Save to binary File

You can pass an object to the saveGame method and save it to disk. Please note, this only works for relatively simple objects (not deeply nested).

// Save the player object to savegame.bin
GameApp.saveBinary(playerObject, "savegame.bin");

Load from binary file

Loads an object from a previously saved binary file. You need to cast it to the correct type after loading.

Object loaded = GameApp.loadBinary("savegame.bin");
// We have to cast it back to the correct type
Player player = (Player) loaded; 

Check if file exists

Use this method to check if a save file is present before trying to load it. This can also be used for other files (e.g. the JSON files).

boolean exists = GameApp.fileExists("savegame.bin");

JSON Save and Load

Save to JSON file

Saves the object as a JSON file. JSON is human-readable, so you can open and inspect it with a text editor. Good for debugging or saving settings.

GameApp.saveJson(playerObject, "savegame.json");

Load from JSON file

Loads an object from a JSON file. You must provide the class type, so LibGDX knows how to convert the JSON back to an object.

Player player = GameApp.loadJson("savegame.json", Player.class);