Pausing Games on Arcadible
Arcadible pauses your game when it needs the screen or the game has to stop: the page is hidden, Arcadible covers it, or the page is about to close or reload it. Tell your game what to do with arcadible.on("pause") and arcadible.on("resume"), and it stops, goes quiet, and keeps the player's progress every time.
arcadible.on("pause", async () => {
// … stop your game loop and timers, and silence your audio …
await arcadible.saves.write("progress", JSON.stringify(progress));
});
arcadible.on("resume", () => {
// … start your game loop and timers again, and your audio …
});
What a pause means
Stop, go quiet, and save. Your game isn't told why it was paused, and doesn't need to be: every pause asks the same of it.
- Stop anything that moves on its own: the game loop, timers, a computer opponent's move.
- Go quiet: mute or suspend your audio. Arcadible can't mute your game for you.
- Save: every pause is a moment to save. Before Arcadible closes or reloads your game, it waits a moment for a pause listener's promise to settle, so a save there lands.
- Draw nothing over your game. Leave its last frame as it is: Arcadible may show its own menu over it, and a pause menu of yours underneath would be a second one to close.
Resuming
Carry on, or give the player a moment. On resume, nothing holds your game any more.
- A puzzle, or anything turn-based, carries on where it was.
- An action game shows its own pause menu, or a "Ready?", before play starts again, so the player isn't dropped back into danger.
You get one pause and one resume, however many reasons overlap, and never a resume without a pause before it.
Where to subscribe
Subscribe as your game starts, as soon as the loop or the audio it stops exists. A game opened in a background tab, or behind Arcadible's menu, is paused already: subscribing then gets the pause just after on() returns, so your setup finishes first and the pause stops it.
on() returns a function that unsubscribes. A listener that throws doesn't stop the others.
Pause on pause alone, not on visibilitychange too: Arcadible's pause covers a hidden page, and handling both does everything twice.
Saving well
Save at checkpoints too, not only on a pause. When a player closes a browser tab, no page can keep it open to wait, so the pause arrives as the page is hidden and its save is best effort. Saves at checkpoints mean little is ever lost. See Saves.
Without the SDK, and off Arcadible
The pause comes from the play page, so:
- Off Arcadible, such as on your dev server, nothing pauses your game. Pause on
visibilitychangethere too if you want it to stop in a hidden tab, and save on it. - Without the SDK, Arcadible can't pause your game or wait for its save. Pause on
visibilitychangeandblur, and save onvisibilitychangeandpagehide.
Trying it
arc serve pauses your game as Arcadible does: switch to another tab, or minimize the window, and your game gets pause; come back, and it gets resume.