Preferences values are not saved - libgdx

I would like to save the player’s best score+2 other values in the game’s preferences file. Now, I know I need to call the flush() method in order for the values to get persisted, but from some reason they are not saved at all.

For instance, let’s say I turn off the music in the game. When I exit the app and then start it, the music is on.

I call the flush method in my preferences object but it still doesn’t save the value into the preferences file.

Here is my settings class:

public final class Settings {
    private boolean soundOn;
    private boolean musicOn;
    private int bestScore;
    private static Settings instance = new Settings();

    public static Settings getInstance() {
        return instance;
    }

    private Settings() {
        soundOn = ScreenHandler.getInstance().getGame().getPreferences().getBoolean(Values.KEY_SOUND, true);
        musicOn = ScreenHandler.getInstance().getGame().getPreferences().getBoolean(Values.KEY_MUSIC, true);
        bestScore = ScreenHandler.getInstance().getGame().getPreferences().getInteger(Values.BEST_SCORE, 0);
    }
    
    public void setSoundOn(boolean soundOn) {
        this.soundOn = soundOn;
        ScreenHandler.getInstance().getGame().getPreferences().putBoolean(Values.KEY_SOUND, soundOn);
    }

    public void setMusicOn(boolean musicOn) {
        this.musicOn = musicOn;
        ScreenHandler.getInstance().getGame().getPreferences().putBoolean(Values.KEY_MUSIC, musicOn);
    }

    public void setBestScore(int bestScore) {
    	this.bestScore = bestScore;
    	ScreenHandler.getInstance().getGame().getPreferences().putInteger(Values.BEST_SCORE, bestScore);
    }
    public void save() {
    	ScreenHandler.getInstance().getGame().getPreferences().flush();
    }
    public boolean isSoundOn() {
        return soundOn;
    }

    public boolean isMusicOn() {
        return musicOn;
    }
    
    public int getBestScore() {
    	return bestScore;
    }
}

Whenever I set the values in the game, I call the save method which invokes the flush method in the preferences object.

I don’t know if you can use this in libgdx (due to cross-platform issues), but I use the Java Preferences API for these kinds of settings.

Just a little confused. Do you have sound and music stored in your preferences? When the player turns the music/sound on or off, do you directly change the value you already have stored in the preferences? Thanks :slight_smile:

Just don’t change the options you don’t want updated when you flush().

Yea. I have them stored in the preferences but only when the game is on. That means whenever the game is disposed, these values(music and sound) are not stored permanently.

But, if I call the flush method, it saves all the values that are exist in the preferences file and I don’t want that.

Yea, but if I don’t change these values, how the game will know if the user prefers the music on/off?

I don’t understand what your problem. You just said that you only want 1 value to be saved. Then you said you want all values to be saved.

I have 3 values - music, sound and bestscore. Now, I want music and sound to be saved only inside the game. Whenever the game is disposed, I don’t want them to get persisted. Like a Session key in PHP.

The third value however I do want to save permanently so the game will track the player’s best score.

You have two possibilities:

  1. Don’t put the values in the settings but keep them in a variable or class that you can access where needed.
  2. Keep it in the settings. If I don’t like a games sound or don’t want it playing and I disable it it is annoying if you have to do it every time launch the game…

Then why would you want to save them to preferences file? Can’t you just keep it in memory? The whole process of saving to file is so that you can read the file after the program has been restarted with the state it previously had.

OK guys I decided to save all the values permanently but I have a new issue here:

The values are not saved when the application is exitted. For instance, let’s say I turn off the music in the game. When I exit the app and then start it, the music is on.

I call the flush method in my preferences object but it still doesn’t save the value into the preferences file.