Turn off pausing when browser tab not in focus?

I’m working on an idle game and I’d like to use Playcanvas for it, but it’s a pretty hard requirement that the game be able to run when it’s not in focus (since these games are often left running while people do other things). I found a forum post from a few years ago saying this wasn’t possible - has that changed? Thanks!

I don’t think the update loop has change since then so no, it’s not possible without customising the engine.

That said, if you are making an idle game, you can add callbacks to the window.onfocus and windows.onblur` (http://www.thefutureoftheweb.com/blog/detect-browser-window-focus) and work out the time elapsed from losing focus to regaining focus and working out what the score/state of the game should be over the time elapsed.

1 Like

Thanks yaustar! Doing an “offline” calculation certainly works for just evaluating earnings but gets more complicated if you have a system that has progression in addition to earning. That said, I might be able to make the calculation work, and if so then looking for the window.onfocus/onblur events sounds perfect. Appreciate the insight!

If you move the logic out of the ‘update’ function, when the tab has focus, you can run the simulation in a loop without rendering.

ie Assuming, the game has been out of focus for 5 secs and you are updating at 30fps,:

for(var i = 0; i < 5 * 30; ++i) {
    game.tick() // Runs the game logic

As long as you are not calling anything PlayCanvas related such as creating textures etc, you should be fine although this can take a while pending on how long the game was out of focus for.

1 Like