Settings - GameDevWeek/CodeBase GitHub Wiki

LibGDX Supports storing preferences, but since their way is a bit error prone (raw strings as keys), helper classes where created.

Settings class

It is recommended to create a class with static members for settings like this:

public class Settings {
	private static final Preferences prefs = Gdx.app.getPreferences(Settings.class.getName() + ".xml");

	// These are all created in the same way: prefs, key, defaultValue
	public static final StringSetting PLAYER_NAME = new StringSetting(prefs, "player_name", "Spieler");
	public static final IntegerSetting LAST_SCORE = new IntegerSetting(prefs, "last_score", 0);

	public static final FloatSetting SOUND_VOLUME = new FloatSetting(prefs, "sound_volume", 1.0f);
	public static final BooleanSetting SOUND_MUTE = new BooleanSetting(prefs, "sound_mute", false);

	public static void flush() {
		prefs.flush();
	}

	public static void reset() {
		SettingsUtil.reset(Settings.class);
	}
}

Getters and Setters

// Get the sound volume setting
SoundEmitter.setGlobalVolume(Settings.SOUND_VOLUME.get());

// Change a couple of settings, then flush them to the file:

Settings.SOUND_VOLUME.set(soundSlider.getValue());
Settings.SOUND_MUTE.set(!soundMuteButton.isChecked());
Settings.flush();

The file

The file is stored in a user folder .prefs, as described here. The filename is the full classpath with .xml extension. For example:

c:\Users\Me\.prefs\de.hochschuletrier.gdw.ss12.Settings.xml