Skip to content

The GameContext & runtime

Every game setup(ctx) receives a GameContext (packages/engine/src/runtime/types.ts) — the one object through which you reach the whole engine.

GameContext groups raw libraries, engine managers, and the Game root
What setup(ctx) receives.
GroupFields
Raw librariesscene, camera, rapierWorld, rapier, composer
Engine managersinput, collisions, physics, audio, particles, debugDraw, assets, systems, components
Game rootgame (optional — set by the host)

ctx.game is the Game root (T7.1): the fixed-step loop, the declaration-ordered world registry, and the cross-root component query (game.queryByComponent(Cls, { worldId?, kind? })). UI is a separate declared React adapter root, not a field on a Three.js setup context.

The raw libraries are the real Three.js / Rapier objects — call them directly. The managers bridge what a raw library doesn’t provide (the Object3D↔body map, system phases, the component manager, and so on).

A game’s setup(ctx) registers systems / loads scenes / sets up state and returns a GameCleanup ({ dispose() }). Every setup must return a dispose that removes everything it created — the scene switcher calls it on change.

createGameRuntime(...) boots the engine and returns a GameSession to control it:

  • stop() — full teardown.
  • pause() / resume() — freeze / unfreeze the game’s simulation via the game-level play state (Game.play, D10). Only worlds marked pausable freeze (a pausable: false menu/HUD world keeps ticking), a paused world’s rendering keeps running (the screen never goes black), and its audio is silenced through its audio adapter — or reported loudly when it can’t be. timeScale is untouched: it remains the separate “speed up / slow down” control.
  • step() — advance a single fixed tick while paused.
  • resize(width, height) — resize renderer + camera.
  • read-only getters for scene, camera, and mounted (the underlying mounted game).

When launched from the editor’s play mode, setup also receives an EditorPreview (scenePath, in-memory sceneData with unsaved edits, and the viewportCamera transform) so the running game matches what you’re editing. The hosted (browser) build adds two routing fields only its play entry reads: exampleId (dispatch a bundled example’s own setup) and assetPrefix (play the open scene generically but resolve its relative assets from the example’s staged /examples/<id>/ root — Track H).

ctx.debugDraw (packages/engine/src/dev/debug-draw.ts) is an immediate-mode helper, cleared each frame:

  • line(from, to, color?), sphere(center, radius?, color?), box(center, size, color?), arrow(origin, direction, length?, color?);
  • clear() and a visible toggle.