Skip to content

@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 ​

GameInstance

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 ​
ParameterTypeDescription
_contextGameBootContextParsed page choices.
_engineEngineBooted engine.
_characters| HostCharacters<unknown> | undefinedFeature-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 ​
ParameterTypeDescription
_contextGameHostContextRunning page services, including the online hero when present.
_sandboxSandboxLoaded 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 ​
ParameterTypeDescription
_contextGameHostContextRunning 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 ​
ParameterTypeDescription
_contextGameBootContextParsed 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 ​
ParameterTypeDescription
contextGameBootContextParsed page choices.
multiplayerPageRoomOptions | undefinedChosen room options.
optionsRunHostOptionsHost configuration.
Returns ​

FeatureTable

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 ​
ParameterTypeDescription
_contextGameBootContextParsed page choices.
inputs{ boot: XrBoot; inputModule: InputModule; support: XrSupport; }Shared XR decision and input module.
inputs.bootXrBootShared XR entry decision.
inputs.inputModuleInputModulePage input module.
inputs.supportXrSupportDetected XR capabilities.
optionsRunHostOptionsHost 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 ​
ParameterTypeDescription
_contextGameHostContextRunning 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 ​
ParameterTypeDescription
contextGameBootContextParsed page choices.
Returns ​

InputOptions

Input options.

installTestHooks() ​
ts
installTestHooks(_context): void;

Install game-specific browser test hooks, only in test mode.

Parameters ​
ParameterTypeDescription
_contextGameHostContextRunning 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 ​
ParameterTypeDescription
contextGameHostContextRunning 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 ​
ParameterTypeDescription
contextGameBootContextParsed page choices.
optionsRunHostOptionsHost 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 ​
ParameterTypeDescription
errorunknownFailure 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 ​
ParameterTypeDescription
messagestringThe 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 ​
ParameterTypeDescription
_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 ​
ParameterTypeDescription
_contextGameBootContextParsed page choices.
Returns ​

EngineModule[]

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 ​
ParameterTypeDescription
_contextGameBootContextParsed 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 ​
ParameterTypeDescription
_contextGameBootContextParsed 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 ​
ParameterTypeDescription
_contextGameBootContextParsed 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 ​
ParameterTypeDescription
contextGameHostContextRunning page services.
framenumberRendered 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 ​
ParameterTypeDescription
contextGameHostContextRunning 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 ​
ParameterTypeDescription
_contextGameBootContextParsed page choices.
engineEngineBooted 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 ​
ParameterTypeDescription
contextGameBootContextParsed page choices.
engineEngineBooted engine.
optionsRunHostOptionsGame-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 ​
ParameterTypeDescription
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 ​

GameBootContext.canvas

characters ​
ts
characters: 
  | HostCharacters<unknown>
  | undefined;
definition ​
ts
definition: GameDefinition;
Inherited from ​

GameBootContext.definition

engine ​
ts
engine: Engine;
guest ​
ts
guest: boolean;

Whether this page has a guest. False supplies an empty definition to page hooks.

Inherited from ​

GameBootContext.guest

hero ​
ts
hero: (() => number) | null;
levelName ​
ts
levelName: string;
Inherited from ​

GameBootContext.levelName

online ​
ts
online: boolean;
Inherited from ​

GameBootContext.online

predict ​
ts
predict: boolean;
Inherited from ​

GameBootContext.predict

query ​
ts
query: URLSearchParams;
Inherited from ​

GameBootContext.query

seed ​
ts
seed: number;
Inherited from ​

GameBootContext.seed

testMode ​
ts
testMode: boolean;
Inherited from ​

GameBootContext.testMode


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 ​
ParameterType
roomChosenRoom<InPageAuthority>
parts{ adapter: EngineAdapterHandle; definition: GameDefinition; engine: Engine; fail: (error) => void; predict: boolean; sandbox: Sandbox; seed: number; slot: GameSlot; }
parts.adapterEngineAdapterHandle
parts.definitionGameDefinition
parts.engineEngine
parts.fail(error) => void
parts.predictboolean
parts.sandboxSandbox
parts.seednumber
parts.slotGameSlot
Returns ​

Promise<() => number>

chooseOnlineRoom() ​
ts
chooseOnlineRoom(
   definition, 
   seed, 
   warn
): Promise<ChosenRoom<InPageAuthority>>;
Parameters ​
ParameterType
definitionGameDefinition
seednumber
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 ​
ParameterType
engineEngine
adapterEngineAdapterHandle
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 ​
ParameterType
engineEngine
source?string
Returns ​

Promise<void>

debug? ​
ts
optional debug?: boolean;
features? ​
ts
optional features?: (context, multiplayer?) => FeatureTable;
Parameters ​
ParameterType
contextGameBootContext
multiplayer?PageRoomOptions
Returns ​

FeatureTable

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 ​
ParameterType
contextGameBootContext
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 ​

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 ​
ParameterType
previousGameInstance | 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 ​

ParameterTypeDescription
optionsRunHostOptionsGame 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 ​

ParameterTypeDescription
optionsRunSessionsOptionsThe 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().