3
\$\begingroup\$

I would like to be able to change the desired fps (foregroundFPS) during runtime without having to restart the game. I know how to set it before launch with LwjglApplicationConfiguration but I have no clue how to set it during runtime.

An example use would be in a menu where the player can select his FPS.

How can I do that ?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

So you want to change a member of a class in your desktop launcher from the code in your game. A very easy (but not scalable) way is to override a method of your game class in your launcher. So in your game class you add a method:

public class MyGame extends ApplicationAdapter {
    ...
    protected void setForegroundFPS(int value) {}
    ...
}

Then in your desktop launcher you override that method:

public class DesktopLauncher {
    LwjglApplicationConfiguration config
    public static void main (String[] arg) {
        config = new LwjglApplicationConfiguration();
        config.width = 1024;
        config.height = 768;
        new LwjglApplication(new MyGame() {
            @override
            protected void setForegroundFPS(int value) {
                config.foregroundFPS = value;
            }
        }, config);
    }
}

When you find yourself overriding a lot of method this way, then it's better to add an interface instead. See also: https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .