Best practice to pause the game in playcanvas

so i have a bunch of spawner scripts and moving scripts and they are using the update function to do what they do, now when the player changes browser tab, the game just stops but the code inside update or setInterval keeps on executing, is there a proper way to pause the game?

Hi @sir_akc! I think you can use the method below.

this.app.timeScale = 0;
3 Likes

so this will stop all the action?
I am going to use this statement inside
window.onblur
and then turn it back on inside
window.onfocus

wait how do you turn the game back on?
this.app.timeScale =1;???

So yeah, timeScale handles most things.

For anything else that isn’t linked directly to the dt of the update function, you can also do something like:

if (this.app.keyboard.wasPressed(pc.KEY_P)) {
    this.app.fire('pausegame');
}

Then, in every script that needs to do something special:

this.app.on('pausegame', function () {
    // do whatever needs to be done to pause things correctly
}, this);
1 Like

well some code is running under SetInterval and I am using bool to check if game active or not and do accordingly, so far the most modular and easy way to handle

That’s correct.