Search code examples
javaandroidlibgdxpreferences

Preferences Libgdx Java


I have been trying to implement a high score feature into my Libgdx game, using getPreferences to save the scores. When i run the program as a desktop app it saves the scores after the initial use, however when i close it and re-run it these scores do not remain. Will it remain completely even it the program is terminated when the game is published to android, ios, desktop etc?

Here is the code that I am using:

Preferences scores = Gdx.app.getPreferences("High Scores");


int currentHighScore = scores.getInteger("currentHighScore", 0);
                        if(currentHighScore < gamescore){
                        scores.putInteger("currentHighScore", gamescore);

int currentHighScore = scores.getInteger("currentHighScore",0);

Solution

  • You have to make sure you call flush() before the application exits. Adding scores.flush() after you call putInteger(...) should be sufficient.

    https://github.com/libgdx/libgdx/wiki/Preferences