Skip to content

Storing save games and other data 0.10.x

Thomas Cashman edited this page May 28, 2015 · 1 revision

Since different operating systems store user data in different locations, storing save games, user preferences and other player data can be troublesome. Luckily mini2Dx abstracts this away through the Data API.

We can easily represent data we want to save as Java objects, e.g.

public class PlayerData {
    @Field
    private int levelsComplete;

    public int getLevelsComplete() {
        return levelsComplete;
    }

    public void setLevelsComplete(int levelsComplete) {
        this.levelsComplete = levelsComplete;
    }
}

The @Field annotation provides a marker for mini2Dx to know which fields to serialise. Then, through the Data API, we can save this data in XML or JSON format.

PlayerData data = new PlayerData();
data.setLevelsComplete(20);
Mdx.data.writeXml(data, "save.xml");

And read it back later on.

PlayerData data = Mdx.data.readXml(PlayerData.class, "save.xml");

Depending on the platform, the data will be saved in that platform's preferred user data location under a folder named with the identifier you passed to mini2Dx, e.g. The following identifier would save data to the locations below.

new LwjglApplication(new DesktopMini2DxGame("org.mini2dx.sample.examplegame", new ExampleGame()), cfg);
Platform Location
Windows C:\Users\username\AppData\Roaming\org.mini2dx.sample.examplegame\
Mac OS X /Users/username/Library/Application Support/org.mini2dx.sample.examplegame/
Linux /home/username/.org.mini2dx.sample.examplegame/
Android Not yet supported
iOS Not yet supported
Clone this wiki locally