How to handle saving settings

There is a weird relationship between setting load defaults to true and app settings auto-loading and being able to preset your own stuff when not already set by the user previously.

I’ve found the best general pattern is what I do in the main method in the example in this comment:

Reproduced here with less context:

    public static void main( String... args ) throws Exception {

        Main main = new Main();
        AppSettings settings = new AppSettings(true);

        // Set some defaults that will get overwritten if
        // there were previously saved settings from the last time the user
        // ran.
        settings.setWidth(1280);
        settings.setHeight(720);
        settings.setVSync(true);

        String title = "MyApp";
        settings.load(title);
        settings.setTitle(title);

        settings.setUseJoysticks(true);

        main.setSettings(settings);

        main.start();
    }

new AppSettings(true) will have app settings fill in values with default values.

Then you can set any that you want different defaults for (common one is screen resolution because default is 640x480 or something). And any other app settings setup.

Then specifically load the previous settings using the app title.

The settings dialog will automatically save any changes the user makes.

2 Likes