Is there any way to cap a whole game at 30fps?
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
Thanks, this works fine
No errors at all!
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.
I found this code make my game’s fps doubled(from 120 to around 240) Could I use this as an optimizer?