[SOLVED] Any way to cap game at 30fps?

Is there any way to cap a whole game at 30fps?

2 Likes

I’ve never tried it but what about this:

var LimitFps = pc.createScript('limitFps');

LimitFps.attributes.add('targetFps', { type: 'number', default: 30 });

// initialize code called once per entity
LimitFps.prototype.initialize = function() {
    var app = this.app;

    this.limit(this.targetFps);

    // Handle any runtime changes to the FPS target
    this.on('attr:targetFps', function (value, prev) {
        this.limit(value);
    });
};

LimitFps.prototype.limit = function(targetFps) {
    var app = this.app;

    if (this.intervalId) {
        clearInterval(this.intervalId);
        this.intervalId = null;
    }

    if (targetFps >= 60) {
        app.autoRender = true;
    } else {
        app.autoRender = false;

        this.intervalId = setInterval(function () {
            app.renderNextFrame = true;
        }, 1000 / targetFps);
    }
};

Please note: I haven’t tested the above code - I just wrote it directly into my response. So beware, there may be bugs (and it might simply just not work very well!). LOL

3 Likes

Thanks, this works fine :grinning:
No errors at all!

1 Like

Should this script reflect the frame rate cap when launching from editor and viewing frames per second in the profiler? Im not seeing any change after adding this script to an object in my scene

The code above doesn’t actually cap the browser framerate (which is why the profiler will still show the maximum possible framerate).

The update loop still runs at the max possible but it only renders every nth frame.

1 Like

I found this code make my game’s fps doubled(from 120 to around 240) :rofl: Could I use this as an optimizer?

2 Likes