Loading Custom Preferences with AppSettings

Hey,

I’m saving my game settings by using the AppSettings.save(String preferencesKey) method, and it saves sucefully all settings(“all” I mean both the defaults and my custom settings e.g.: settings.putString(“soundVolume”, “1f”)) but the AppSettings.load(String preferencesKey) method just loads the defaults ones, then my custom settings aren’t loaded. Shouldn’t it really load them? I took a look at the save and load methods and I already know why my custom settings aren’t loaded, see bellow my comment in code:



AppSettings



[java]

public void load(String preferencesKey) throws BackingStoreException {

Preferences prefs = Preferences.userRoot().node(preferencesKey);

String[] keys = prefs.keys();

if (keys != null) {

for (String key : keys) {

//here, instead of defaults.get(key) it should be just get(key) ? Then it would load all saved preferences, not just the defaults.

Object defaultValue = defaults.get(key);

if (defaultValue instanceof Integer) {

put(key, prefs.getInt(key, (Integer) defaultValue));

} else if (defaultValue instanceof String) {

put(key, prefs.get(key, (String) defaultValue));

} else if (defaultValue instanceof Boolean) {

put(key, prefs.getBoolean(key, (Boolean) defaultValue));

}

}

}

}

[/java]



As you can see, the save method saves sucefully all preferences, including my custom ones:



AppSettings



[java]

public void save(String preferencesKey) throws BackingStoreException {

Preferences prefs = Preferences.userRoot().node(preferencesKey);

for (String key : keySet()) {

//here is get(key), diferently of the load method

prefs.put(key, get(key).toString());

}

}

[/java]



Then the problem here is when loading them!?..

Hm, yeah it probably should. We’ll look into this.

Yes its a known issue. We cannot know the type of variable that the user has written into the preferences so its not possible to read it back. We may have to write additional data into the converted string to indicate the type of variable



EDIT: I added new issue:

http://code.google.com/p/jmonkeyengine/issues/detail?id=426

Though I wonder why not let the game use its own Preferences object/access instead of trying to do it all in AppSettings? Or is it worth complicating AppSettings to support this sort of general storage or should it be a separate mechanism?

@pspeed said:
Though I wonder why not let the game use its own Preferences object/access instead of trying to do it all in AppSettings? Or is it worth complicating AppSettings to support this sort of general storage or should it be a separate mechanism?


I just wanted to be fast ;). It's just three objects "resolution", "musicVolum" and "soundVolum", then I reused the AppSettings serialization :).

Yeah, but it’s just so trivial to grab your own Preferences object and set it directly. That’s what I do and I think it’s actually easier then dealing with AppSettings since I can do it anywhere and not have to worry about having a reference to that object.

Since AppSettings are stored automatically and are very easy to use (and will probably be used by any game anyhow) I think it does make sense to get this working properly. Why not store all global settings in once place?