Pausing the Game
world.pause() stops the game and world.resume() carries on from exactly where it stopped. The world keeps rendering and reading input the whole time, so a headset stays tracked and a pause menu can work.
this.world.pause(); // stop the game
this.world.resume(); // carry on
this.world.togglePause(); // whichever applies; returns true if now paused
this.world.isPaused(); // true while pausedPausing twice or resuming twice does nothing the second time, so it's always safe to call.
What stops and what keeps going
| Stops | Keeps going |
|---|---|
onUpdate and ScriptObject.update | Rendering, including post-processing and the headset view |
| Physics simulation | Keyboard, mouse and XR input |
| Animation, bone holds and IK | Spatial UI, including onUiPress and the other UI hooks |
| Grabbing, throwing and locomotion | Pointer rays, hand tracking and XR anchors |
| Hand poses | The multiplayer connection |
| Playing sounds and videos, which hold their place | Script loading, and new sounds and videos |
| Your custom systems, unless they opt in | Transforms, materials, lights and visibility |
The screen looks frozen because nothing moves the scene, not because rendering stopped. A script can still change what is on screen, which is how a pause menu appears.
Physics takes no catch-up steps on resume, however long the pause was, and animations continue from the same frame. A body spawned during a pause is ready immediately and starts moving on resume.
A complete pause menu
This Behaviour pauses on Escape, shows a DOM overlay with a Resume button, and handles a first-person pointer lock. Put it on any entity that stays visible.
import { Behaviour, KeyboardKeys } from "@relu-interactives/spatial-ecs";
export default class PauseMenu extends Behaviour {
private overlay: HTMLDivElement | null = null;
private stopWatchingLock: (() => void) | null = null;
init() {
this.overlay = this.buildOverlay();
// With the pointer locked, Escape belongs to the browser. It drops the lock
// and the page may never see the key, so this is how the game hears it.
this.stopWatchingLock = this.world.onPointerLockChange((locked) => {
if (!locked) this.world.pause();
});
}
onUpdate() {
// Without a lock, Escape arrives like any other key.
if (this.world.getKeyDown(KeyboardKeys.Escape)) {
this.world.pause();
}
if (this.world.getMouseDown("left") && !this.world.isMousePointerLocked()) {
this.world.lockMousePointer();
}
}
onPausedUpdate() {
if (this.world.getKeyDown(KeyboardKeys.Escape)) {
this.resumeGame();
}
}
onPause() {
if (this.overlay) this.overlay.style.display = "flex";
this.world.unlockMousePointer();
}
onResume() {
if (this.overlay) this.overlay.style.display = "none";
}
dispose() {
this.stopWatchingLock?.();
this.overlay?.remove();
}
private resumeGame() {
this.world.resume();
this.world.lockMousePointer();
}
private buildOverlay(): HTMLDivElement {
const overlay = document.createElement("div");
overlay.style.cssText =
"position:fixed;inset:0;z-index:10;display:none;align-items:center;" +
"justify-content:center;background:rgba(0,0,0,0.6)";
const resume = document.createElement("button");
resume.textContent = "Resume";
resume.style.cssText = "padding:12px 32px;font-size:18px";
resume.addEventListener("click", () => this.resumeGame());
overlay.append(resume);
document.body.append(overlay);
return overlay;
}
}The onPointerLockChange listener doesn't need to check whether the game is already paused: onPause unlocks the pointer, which fires the listener, and that second pause() does nothing. If your game also unlocks the pointer for something else, such as an inventory screen, check for that in the listener first.
Getting the pointer lock back
A browser only grants the lock from a real user gesture, and the Escape key doesn't count as one. So:
- Clicking Resume locks straight away.
- Pressing Escape to resume leaves the pointer unlocked until the next mouse press. The engine retries the lock on that press by itself, and that press also reaches
onUpdateas a normal click. - Some browsers refuse a new lock for a moment after the player pressed Escape. The retry on the next press covers this too.
If you'd rather not have a game running without its lock, leave onPausedUpdate out and resume only from the button.
Hooks
Behaviours get three hooks. See Lifecycle & Events for the whole order.
| Hook | When | Hidden entities? |
|---|---|---|
onPause() | Once, when the game pauses. | Yes |
onResume() | Once, when the game resumes. | Yes |
onPausedUpdate() | Every frame while paused, instead of onUpdate(). this.deltaTime is valid. | No, like onUpdate() |
A pause or resume starts from the next script: scripts that haven't run yet this frame get neither onUpdate nor onPausedUpdate until the next frame. So one Escape press can't both open and close a menu.
A script that loads during a pause gets onPausedUpdate straight away with no onPause first, and onResume when the game resumes. Check this.world.isPaused() in init() if it matters.
A ScriptObject has the same hooks as optional methods. Its onPausedUpdate(deltaTime) takes the time as an argument, like its update.
Anything that isn't a script can listen too. onPauseChange returns an unsubscribe function, and runs after systems and scripts have heard:
import { Behaviour } from "@relu-interactives/spatial-ecs";
export default class PauseBadge extends Behaviour {
private stop: (() => void) | null = null;
init() {
this.stop = this.world.onPauseChange((paused) => {
document.title = paused ? "Paused" : "My Game";
});
}
dispose() {
this.stop?.();
}
}Sound
Pausing holds everything that is playing, and resuming continues it from the same point. That covers AudioComponent sounds and sound bank slots. A sound that was already stopped or paused stays that way.
Three things keep a menu's sound working:
- The
uibus is never held. Put menu sounds on it. The click of the button that opened the menu finishes, and the bus has its own volume. - Sounds started during a pause play normally, on any bus. Only what was playing at the moment of the pause is held.
world.pause({ audio: "keep" })leaves all sound playing.
import { Behaviour } from "@relu-interactives/spatial-ecs";
export default class MenuSounds extends Behaviour {
onPause() {
// A slot with "bus": "ui" in its sound bank.
this.playSlot("menu-open");
}
}A script can stop a held sound during the pause, and it stays stopped on resume. See Mixing and sound banks.
Video
Videos work the same way: pausing holds the ones that are playing, and resuming continues those and only those. A video started during a pause plays normally. world.pause({ video: "keep" }) leaves all video playing, and is separate from the audio option.
Input
Input keeps working while paused, so getKeyDown in onPausedUpdate reads a key the frame it's pressed.
A press never crosses a pause. The engine drops any waiting key, mouse button, scroll and mouse movement when the game pauses and again when it resumes. The click on a Resume button doesn't fire a gun, and the Escape that opens a menu doesn't close it. getKey still reports keys that are physically down, so a player holding W keeps walking.
This means the matching edge can be missed. If W goes up during a pause, gameplay never sees getKeyUp. Read held state with getKey rather than tracking presses yourself, and reset anything press-based in onPause or onResume.
These don't fire while paused, so a click on a menu can't reach gameplay:
onSelectandonDeselect. A tap during a pause is dropped, not saved for later.onSelectStartandonSelectEnd, so a script that tracks a trigger press should reset inonPause.onGrab,onReleaseand the other grab hooks. The exception is a hand disappearing while it holds something, or the session ending, either of which releases the object.onHandPoseStartandonHandPoseEnd.
onUiPress, onUiToggle, onUiChange and the hover hooks do fire, and so do onPlayerJoin, onPlayerLeave and onNetworkProp.
A pause menu in spatial UI
In a headset there is no DOM to overlay, so build the menu from spatial UI. Pointer rays and pinches keep working on UI during a pause, even with a hand that's holding something. Nothing else can be pointed at, so a prop in front of the menu doesn't get in the way.
Hidden entities still hear onPause and onResume, so the script can live on the panel it shows:
import { Behaviour, type UiInteractionEvent } from "@relu-interactives/spatial-ecs";
export default class SpatialPauseMenu extends Behaviour {
init() {
this.threeRef.visibility = false;
}
onPause() {
this.threeRef.visibility = true;
}
onResume() {
this.threeRef.visibility = false;
}
onUiPress(event: UiInteractionEvent) {
if (event.actionId === "resume") this.world.resume();
}
}A held object stays where it was while the hand is free to move, and comes back to the hand on resume. If the player let go during the pause it drops on resume, without being thrown.
A teleport being aimed is cancelled when the game pauses. Letting go of the stick during the pause won't teleport the player on resume.
Custom systems
A custom system stops while paused, which is what a system that moves the game on wants. To keep ticking, set runsWhilePaused:
import { type EcsSystem, type World } from "@relu-interactives/spatial-ecs";
export class ClockHud implements EcsSystem {
readonly runsWhilePaused = true;
update(world: World) {
// Runs on every frame, paused or not.
}
}A system can also hear the change with two optional methods, which run before scripts are told:
import {
type EcsSystem,
type PauseOptions,
type World,
} from "@relu-interactives/spatial-ecs";
export class MusicDirector implements EcsSystem {
update() {}
onWorldPause(world: World, options: PauseOptions) {
// options is what was passed to world.pause()
}
onWorldResume(world: World) {}
}Multiplayer
A room has no shared pause. world.pause() pauses this player only: everyone else carries on, the connection stays up, and the first pause in a room logs one console warning saying so.
While paused, other players keep moving and their sounds still play. Your own character stops, so to them you stand still, and they can still affect you when you resume. Most multiplayer games should show a menu without calling pause(), or pause everyone from the server. See Multiplayer.

