@gameable/game-host
Classes
GameInstance
Game customisation hooks for the packaged host. Override choices, scene setup and callbacks; the engine retains boot, start and failure handling. Constructed once per runHost call.
Example
ts
import { GameInstance, runHost } from 'gameable/game-host';
class MyGame extends GameInstance {
override onStart(reason: 'player' | 'capture' | 'playtest'): void { super.onStart(reason); }
}
void runHost({ game: () => import('./game'), instance: MyGame });Constructors
Constructor
ts
new GameInstance(): GameInstance;Returns
Properties
engine
ts
engine: Engine | null = null;Booted engine, also available to failure overrides after engine creation.
left
ts
readonly left: Promise<void>;Resolves when this session asks to return to its menu.
Example
ts
const done = new GameInstance().left;Methods
adapterOptions()
ts
adapterOptions(
_context,
_engine,
_characters
): Omit<EngineAdapterOptions, "modules" | "thing">;Custom HUD, character bridge and presentation policy for the host adapter.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
_engine | Engine | Booted engine. |
_characters | | HostCharacters<unknown> | undefined | Feature-provided characters. |
Returns
Omit<EngineAdapterOptions, "modules" | "thing">
Adapter customisations; the host supplies modules and the world bridge.
afterAttach()
ts
afterAttach(_context, _sandbox): void;Bind page UI and persistence after the guest has initialized and the host's start handler is registered (so a page may start the game at once), before engine.start().
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameHostContext | Running page services, including the online hero when present. |
_sandbox | Sandbox | Loaded guest, available for liveness checks. |
Returns
void
bind()
ts
bind(_context): void | Promise<void>;Bind scene services once features and the adapter are available. Async binding completes before guest attachment.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameHostContext | Running page services. |
Returns
void | Promise<void>
dispose()
ts
dispose(): Promise<void>;Stop and release this host, including partially booted resources. All cleanup runs even if one release fails. Repeated calls share the same completion.
Returns
Promise<void>
Completion of teardown.
Example
ts
await new GameInstance().dispose();engineOptions()
ts
engineOptions(_context): Pick<CreateEngineOptions, "renderer" | "xr">;Renderer and frame-clock choices after preparation and XR selection.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
Returns
Pick<CreateEngineOptions, "renderer" | "xr">
Overrides merged with the shared XR and renderer defaults.
features()
ts
features(
context,
multiplayer,
options
): FeatureTable;Feature providers, optionally using choices made during prepare.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameBootContext | Parsed page choices. |
multiplayer | PageRoomOptions | undefined | Chosen room options. |
options | RunHostOptions | Host configuration. |
Returns
Providers for the definition's features.
headset()
ts
headset(
_context,
inputs,
options
): Promise<HostXr | undefined>;Create headset modules on the shared lighter path, using prepared page choices.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
inputs | { boot: XrBoot; inputModule: InputModule; support: XrSupport; } | Shared XR decision and input module. |
inputs.boot | XrBoot | Shared XR entry decision. |
inputs.inputModule | InputModule | Page input module. |
inputs.support | XrSupport | Detected XR capabilities. |
options | RunHostOptions | Host configuration. |
Returns
Promise<HostXr | undefined>
Headset modules and binding, or none to offer the shared entry button.
hero()
ts
hero(_context): number;Hero entity for XR follow views; templates can use their character roster.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameHostContext | Running page services. |
Returns
number
No hero by default.
inputOptions()
ts
inputOptions(context): InputOptions;Input choices. Capture and test pages do not require pointer lock.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameBootContext | Parsed page choices. |
Returns
Input options.
installTestHooks()
ts
installTestHooks(_context): void;Install game-specific browser test hooks, only in test mode.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameHostContext | Running page services. |
Returns
void
leave()
ts
leave(): void;End this session. runSessions awaits disposal before opening the next menu.
Returns
void
Example
ts
new GameInstance().leave();loopOptions()
ts
loopOptions(context): string | undefined;Init options for the local guest, after online attachment has been considered.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameHostContext | Running page services. |
Returns
string | undefined
Serialized guest options, or undefined for the guest defaults.
manifestOptions()
ts
manifestOptions(context, options):
| string
| object
| AssetManifest
| Promise<
| string
| object
| AssetManifest
| undefined>
| undefined;Resolve a manifest after prepare has chosen the game's assets.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameBootContext | Parsed page choices. |
options | RunHostOptions | Host configuration. |
Returns
| string | object | AssetManifest | Promise< | string | object | AssetManifest | undefined> | undefined
The configured manifest, or none for an empty registry.
onFailure()
ts
onFailure(error): "handled";Report a failed boot or dead online loop and leave its seat. Return handled in an override to replace this policy. Page errors are routed here only with capturePageErrors.
Parameters
| Parameter | Type | Description |
|---|---|---|
error | unknown | Failure to show. |
Returns
"handled"
Handled, after the default failure card and room cleanup.
onLoadFailure()
ts
onLoadFailure(message): "handled";Route a legacy load callback's text through the host failure policy. Character bridges expose strings, so recognise browser network messages before the structured error reaches the engine failure card. Other load errors remain crashes.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The loader's original failure message. |
Returns
"handled"
The failure hook's handled result.
Example
ts
import { GameInstance } from 'gameable/game-host';
const page = new GameInstance();
page.onLoadFailure('TypeError: Failed to fetch');onStart()
ts
onStart(_reason): void;Play callback; pointer-lock policy remains in the engine host.
Parameters
| Parameter | Type | Description |
|---|---|---|
_reason | "player" | "capture" | "playtest" | Why Play started. |
Returns
void
pageModules()
ts
pageModules(_context): EngineModule[];Page modules following physics and input. Override to add or configure modules.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
Returns
Audio, splats and made-world support by default.
physicsOptions()
ts
physicsOptions(_context): PhysicsOptions | null;Physics choices; null omits physics. A non-predicting room client always omits it.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
Returns
PhysicsOptions | null
Physics options, supplied by a game's instance.
pointerLockOnStart()
ts
pointerLockOnStart(_context): boolean;Whether player starts and later canvas clicks request pointer lock. Capture and playtest starts never request it.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
Returns
boolean
True by default; false keeps a page's pointer free for UI or side views.
prepare()
ts
prepare(_context): Promise<void>;Await game-owned page choices before creating modules or resolving features.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
Returns
Promise<void>
Completion of preparation.
readiness()
ts
readiness(context, frame):
| {
characters?: string;
}
| null;Decide when the host publishes readiness and which optional labels it includes.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameHostContext | Running page services. |
frame | number | Rendered frame number. |
Returns
| { characters?: string; } | null
Labels when ready, or null to keep waiting.
readyCharacters()
ts
readyCharacters(context): string;Character readiness label. Override for alternate character representations.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameHostContext | Running page services. |
Returns
string
Label published to capture and playtest.
setupLights()
ts
setupLights(_context, engine): void;Fallback place lights; override when a game supplies a lighting block.
Parameters
| Parameter | Type | Description |
|---|---|---|
_context | GameBootContext | Parsed page choices. |
engine | Engine | Booted engine. |
Returns
void
setupScene()
ts
setupScene(
context,
engine,
options
): Promise<void>;Load the start level's place or a made world, with the pilot's fallback lights. Guest-free pages have no default arena; override to supply their scene.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | GameBootContext | Parsed page choices. |
engine | Engine | Booted engine. |
options | RunHostOptions | Game-owned asset and collider configuration. |
Returns
Promise<void>
Completion of scene loading.
use()
ts
use(cleanup): void;Own a resource from a hook, including resources acquired before engine creation. Cleanup runs once, in reverse order, and awaits asynchronous releases.
Parameters
| Parameter | Type | Description |
|---|---|---|
cleanup | () => void | Promise<void> | Release the resource. |
Returns
void
Example
ts
new GameInstance().use(() => console.log('released'));Interfaces
GameBootContext
Parsed page choices and game definition available to boot hooks.
Example
ts
import type { GameBootContext } from 'gameable/game-host';
function seedOf(context: GameBootContext) { return context.seed; }Extended by
Properties
canvas
ts
canvas: HTMLCanvasElement;definition
ts
definition: GameDefinition;guest
ts
guest: boolean;Whether this page has a guest. False supplies an empty definition to page hooks.
levelName
ts
levelName: string;online
ts
online: boolean;predict
ts
predict: boolean;query
ts
query: URLSearchParams;seed
ts
seed: number;testMode
ts
testMode: boolean;GameHostContext
Running page services passed to test and binding hooks.
Example
ts
import type { GameHostContext } from 'gameable/game-host';
function engineOf(context: GameHostContext) { return context.engine; }Extends
Properties
adapter
ts
adapter: EngineAdapterHandle;boundFeatures
ts
boundFeatures: ReadonlyMap<string, unknown>;Services returned by feature binding, keyed by their declared name.
canvas
ts
canvas: HTMLCanvasElement;Inherited from
characters
ts
characters:
| HostCharacters<unknown>
| undefined;definition
ts
definition: GameDefinition;Inherited from
engine
ts
engine: Engine;guest
ts
guest: boolean;Whether this page has a guest. False supplies an empty definition to page hooks.
Inherited from
hero
ts
hero: (() => number) | null;levelName
ts
levelName: string;Inherited from
online
ts
online: boolean;Inherited from
predict
ts
predict: boolean;Inherited from
query
ts
query: URLSearchParams;Inherited from
seed
ts
seed: number;Inherited from
testMode
ts
testMode: boolean;Inherited from
HostOnline
Lazy multiplayer integration; the page owns room selection and client attachment.
Example
ts
import type { HostOnline } from 'gameable/game-host';
const load: () => Promise<HostOnline> = () => import('./online');Methods
attachOnline()
ts
attachOnline(room, parts): Promise<() => number>;Parameters
| Parameter | Type |
|---|---|
room | ChosenRoom<InPageAuthority> |
parts | { adapter: EngineAdapterHandle; definition: GameDefinition; engine: Engine; fail: (error) => void; predict: boolean; sandbox: Sandbox; seed: number; slot: GameSlot; } |
parts.adapter | EngineAdapterHandle |
parts.definition | GameDefinition |
parts.engine | Engine |
parts.fail | (error) => void |
parts.predict | boolean |
parts.sandbox | Sandbox |
parts.seed | number |
parts.slot | GameSlot |
Returns
Promise<() => number>
chooseOnlineRoom()
ts
chooseOnlineRoom(
definition,
seed,
warn
): Promise<ChosenRoom<InPageAuthority>>;Parameters
| Parameter | Type |
|---|---|
definition | GameDefinition |
seed | number |
warn | (message) => void |
Returns
Promise<ChosenRoom<InPageAuthority>>
HostXr
Headset modules loaded only when the shared XR gate chooses the lighter path.
Example
ts
import type { HostXr } from 'gameable/game-host';
const headset: HostXr = { modules: [], bind() {} };Properties
modules
ts
modules: EngineModule[];Methods
bind()
ts
bind(
engine,
adapter,
hero
): void;Parameters
| Parameter | Type |
|---|---|
engine | Engine |
adapter | EngineAdapterHandle |
hero | () => number |
Returns
void
RunHostOptions
Game-owned data and optional lazy integrations for the engine-owned page.
Example
ts
import { runHost, type RunHostOptions } from 'gameable/game-host';
const options: RunHostOptions = { game: () => import('./game') };
void runHost(options);Properties
badge?
ts
optional badge?: boolean;Omit the engine badge for pages that did not offer one.
canvas?
ts
optional canvas?: HTMLCanvasElement;canvasStart?
ts
optional canvasStart?: boolean;Disable canvas Play for games with a dedicated menu or keyboard start.
capturePageErrors?
ts
optional capturePageErrors?: boolean;Route uncaught page errors and rejections to onFailure; defaults to false.
collider?
ts
optional collider?: (engine, source?) => Promise<void>;Parameters
| Parameter | Type |
|---|---|
engine | Engine |
source? | string |
Returns
Promise<void>
debug?
ts
optional debug?: boolean;features?
ts
optional features?: (context, multiplayer?) => FeatureTable;Parameters
| Parameter | Type |
|---|---|
context | GameBootContext |
multiplayer? | PageRoomOptions |
Returns
game
ts
game:
| false
| (() => Promise<
| false
| {
default: GameDefinition;
}>);Game loader, or false for an engine page without a guest. A loader may also return false after resolving page inputs. Page mode skips features, online, the game slot, sandbox and host loop; definition is an empty declaration in hooks.
Example
ts
import { runHost } from 'gameable/game-host';
void runHost({ game: false });guestUrl?
ts
optional guestUrl?: string;instance?
ts
optional instance?: GameInstance | (() => GameInstance);Class to construct, or a fresh instance carrying this session's menu choices.
levelFromQuery?
ts
optional levelFromQuery?: boolean;Allow level selection through the page query; defaults to true.
manifest?
ts
optional manifest?: (context) =>
| string
| object
| AssetManifest
| Promise<
| string
| object
| AssetManifest
| undefined>
| undefined;Parameters
| Parameter | Type |
|---|---|
context | GameBootContext |
Returns
| string | object | AssetManifest | Promise< | string | object | AssetManifest | undefined> | undefined
mode?
ts
optional mode?: "direct" | "wasm";online?
ts
optional online?: () => Promise<HostOnline>;Returns
Promise<HostOnline>
seed?
ts
optional seed?: number;Default guest seed; a seed query still overrides it unless seedFromQuery is false.
seedFromQuery?
ts
optional seedFromQuery?: boolean;Fixed-seed games can retain their seed even when a query supplies another.
worldUrl?
ts
optional worldUrl?: string;xr?
ts
optional xr?: false | ((inputs) => Promise<HostXr>);RunSessionsOptions
Menu and session choices for a page with repeatable engine sessions.
Example
ts
import type { RunSessionsOptions } from 'gameable/game-host';
const page: RunSessionsOptions = { async menu() { return false; } };Methods
menu()
ts
menu(previous): Promise<false | RunHostOptions>;Show the menu and return the next host configuration, or false to end. The previous instance has finished disposal; it can retain rejoin choices.
Parameters
| Parameter | Type |
|---|---|
previous | GameInstance | undefined |
Returns
Promise<false | RunHostOptions>
Functions
runHost()
ts
function runHost(options): Promise<GameInstance>;Boot an engine-owned game page. GameInstance hooks customise choices without copying boot code. A failed instance constructor emits GAMEABLE_INSTANCE_FALLBACK and uses the base instance. Boot failures go to onFailure; a handled failure resolves to the instance for inspection. Pass game: false (or return false from the loader) for a page without a guest.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | RunHostOptions | Game definition loader, instance class and game-owned configuration. |
Returns
Promise<GameInstance>
The selected instance after starting the engine or reporting a boot failure.
Example
ts
import { runHost } from 'gameable/game-host';
void runHost({ game: () => import('./game'), manifest: () => manifest });runSessions()
ts
function runSessions(options): Promise<void>;Run menu -> host -> menu in one document using runHost's single boot path. Call instance.leave() to return. Each session releases its engine, listeners and hook-owned resources before the next menu. A failed session retains the engine failure card with a Back to menu action. Canvas attributes survive, but its disposed rendering context is replaced between sessions.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | RunSessionsOptions | The page's menu, returning game-owned host choices. |
Returns
Promise<void>
Completion when the menu returns false. Menu/cleanup failures show the engine card.
Example
ts
import { runSessions } from 'gameable/game-host';
void runSessions({ async menu() {
return confirm('Play?') ? { game: () => import('./game') } : false;
} });
// A game-owned Leave control calls its GameInstance.leave().