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 UI container
What setup(ctx) receives.
GroupFields
Raw librariesscene, camera, rapierWorld, rapier, composer
Engine managersinput, collisions, physics, audio, particles, debugDraw, animGraphs, assets, systems, components
UIuiContainer

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 time (via timeScale).
  • 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.

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.