Skip to content

Kiln API / kiln / App

Class: App ​

The game: a Koota world, resources, events, and a schedule of systems in phases. Build it with createApp(config).use(...).add(Phase, ...), then start(canvas) in the browser or step(n) headless.

Constructors ​

Constructor ​

ts
new App(config?): App;

Parameters ​

ParameterType
configKilnConfig

Returns ​

App

Properties ​

config ​

ts
readonly config: ResolvedConfig;

world ​

ts
readonly world: World;

dev ​

ts
readonly dev: boolean;

time ​

ts
readonly time: TimeState;

bus ​

ts
readonly bus: EventBus;

queue ​

ts
readonly queue: CommandQueue;

commands ​

ts
readonly commands: Commands;

App-level command queue; systems share it.


ext ​

ts
readonly ext: Record<string, any> = {};

Slots plugins attach controllers to (input, renderer, physics world...).


hooks ​

ts
readonly hooks: object;

Lifecycle hooks for plugins.

start ​

ts
start: Hook<[HTMLCanvasElement | undefined]>[];

Before Startup when the app starts in a browser. Receives the canvas.

fixedStart ​

ts
fixedStart: Hook<[]>[];

Before each fixed step (render uses it to snapshot the previous transform).

fixedEnd ​

ts
fixedEnd: Hook<[]>[];

After each fixed step and its commands.

frameStart ​

ts
frameStart: Hook<[number, boolean]>[];

At the start of each frame, with its delta and whether it runs exactly one fixed step (step()).

frameEnd ​

ts
frameEnd: Hook<[]>[];

After a frame's Update and Late phases, before Render. Recordings take their checkpoints here.

built ​

ts
built: Hook<[]>[];

After the schedule is (re)built.

dispose ​

ts
dispose: Hook<[]>[];

When the app is disposed.

error ​

ts
error: Hook<[unknown]>[];

When a browser frame throws. The loop pauses; the browser runtime shows an overlay.


headless ​

ts
headless: boolean = true;

True until start() runs in a browser. Render-only work skips itself when headless.


profile ​

ts
profile: boolean = false;

Record per-system timings (the debug plugin turns this on).


paused ​

ts
paused: boolean = false;

While true the browser loop only renders; the simulation is frozen.


canvas ​

ts
canvas: HTMLCanvasElement | undefined;

frameRate ​

ts
readonly frameRate: object;

Browser loop pacing. vsync: true (default) paces frames with requestAnimationFrame; false runs frames back to back (the compositor still presents at the display rate, so this measures throughput). maxFps caps frames per second in either mode; 0 means no cap. Change these at runtime; the debug panel has controls for both.

vsync ​

ts
vsync: boolean = true;

maxFps ​

ts
maxFps: number = 0;

currentPhase ​

ts
currentPhase: Phase | null = null;

Accessors ​

pluginList ​

Get Signature ​

ts
get pluginList(): readonly Plugin[];

Plugins used, in order.

Returns ​

readonly Plugin[]


isBooted ​

Get Signature ​

ts
get isBooted(): boolean;

True once Startup has run.

Returns ​

boolean


isDisposed ​

Get Signature ​

ts
get isDisposed(): boolean;
Returns ​

boolean


input ​

Get Signature ​

ts
get input(): InputController;

Scripted and live input: app.input.script([{ frame: 10, press: 'jump' }]). Needs the input plugin.

Returns ​

InputController


resAccess ​

Get Signature ​

ts
get resAccess(): Res;

Resource access with the same read-only rules systems get.

Returns ​

Res

Methods ​

use() ​

ts
use(...plugins): this;

Adds plugins. Each plugin is used once, however many times it is passed.

Parameters ​

ParameterType
...pluginsPluginLike[]

Returns ​

this


init() ​

ts
init(...items): this;

Registers resources (created with their defaults) and event types.

Parameters ​

ParameterType
...items( | Resource<any> | EventType<any>)[]

Returns ​

this


insert() ​

ts
insert<T>(r, value): this;

Sets a resource's value, replacing any existing one.

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
rResource<T>
valueT

Returns ​

this


add() ​

ts
add(phase, ...items): this;

Adds systems to a phase. Order inside the phase comes from chain() and .after()/.before().

Parameters ​

ParameterType
phasePhase
...itemsSchedulable[]

Returns ​

this


ready() ​

ts
ready(): Promise<App>;

Resolves when async plugins (e.g. physics WASM) have finished.

Returns ​

Promise<App>


track() ​

ts
track<T>(work): Promise<T>;

Registers background work (asset loads) that never blocks a phase. app.idle() waits for it; scenarios call it after Startup so headless runs stay deterministic.

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
workPromise<T>

Returns ​

Promise<T>


idle() ​

ts
idle(): Promise<App>;

Resolves when all tracked background work (e.g. asset loads) has settled.

Returns ​

Promise<App>


resource() ​

ts
resource<T>(r): T;

The live value of a resource (created on first access). Mutable: for tests and plugins.

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
rResource<T>

Returns ​

T


res() ​

ts
res<T>(r): T;

Shorthand for app.resource(r).

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
rResource<T>

Returns ​

T


hasResource() ​

ts
hasResource(r): boolean;

Parameters ​

ParameterType
rResource<any>

Returns ​

boolean


resourceList() ​

ts
resourceList(): readonly Resource<any>[];

Resources created so far, in creation order.

Returns ​

readonly Resource<any>[]


build() ​

ts
build(): this;

Builds and validates the schedule if anything changed. Throws errors that name the fix.

Returns ​

this


schedule() ​

ts
schedule(): ReadonlyMap<Phase, BuiltPhase>;

The built schedule: each phase's systems in run order.

Returns ​

ReadonlyMap<Phase, BuiltPhase>


systemsIn() ​

ts
systemsIn(phase): readonly CompiledSystem[];

Compiled systems in run order for a phase.

Parameters ​

ParameterType
phasePhase

Returns ​

readonly CompiledSystem[]


replaceSystem() ​

ts
replaceSystem(oldSystem, newSystem): boolean;

Swaps one system for another in place, keeping the world. Used by hot reload ('keep'). The schedule is re-validated first; on error the old system stays.

Parameters ​

ParameterType
oldSystemSystem
newSystemSystem

Returns ​

boolean


boot() ​

ts
boot(): this;

Builds the schedule and runs Startup once. step() and start() call this for you.

Returns ​

this


step() ​

ts
step(n?): this;

Advances exactly n frames of one fixed step each, with no renderer. Deterministic for a given seed and input script. Events emitted during the call are readable with app.events(E).

Parameters ​

ParameterTypeDefault value
nnumber1

Returns ​

this


tick() ​

ts
tick(dt, exact?): this;

Runs one variable-dt frame: input, 0..N fixed steps, update, late, render. With exact, the frame runs exactly one fixed step as step() does (replays use it for frames stepped that way).

Parameters ​

ParameterTypeDefault value
dtnumberundefined
exactbooleanfalse

Returns ​

this


flush() ​

ts
flush(): void;

Applies queued commands now. Runs automatically at every phase boundary.

Returns ​

void


start() ​

ts
start(canvas?, options?): Promise<App>;

Starts the browser loop: runs plugin start hooks (renderer, input devices), Startup, then a requestAnimationFrame loop. Pass { loop: false } to drive frames yourself (kiln shot does), { startup: false } to skip Startup (when restoring a snapshot).

Parameters ​

ParameterType
canvas?HTMLCanvasElement
options?{ loop?: boolean; startup?: boolean; }
options.loop?boolean
options.startup?boolean

Returns ​

Promise<App>


render() ​

ts
render(): this;

Renders one frame now without advancing the simulation. Needs a started app.

Returns ​

this


dispose() ​

ts
dispose(): void;

Stops the loop and frees the world. The app can't be used afterwards.

Returns ​

void


spawn() ​

Call Signature ​

ts
spawn<P>(prefab, ...params): Entity;

Spawns immediately (tests and tools). Systems use commands.spawn.

Type Parameters ​
Type Parameter
P
Parameters ​
ParameterType
prefabPrefab<P>
...paramsP extends void ? [] : [P]
Returns ​

Entity

Call Signature ​

ts
spawn(...traits): Entity;

Spawns immediately (tests and tools). Systems use commands.spawn.

Parameters ​
ParameterType
...traitsConfigurableTrait[]
Returns ​

Entity


despawn() ​

ts
despawn(entity): void;

Despawns immediately (tests and tools).

Parameters ​

ParameterType
entityEntity

Returns ​

void


get() ​

ts
get<T>(entity, trait): TraitRecord<T> | undefined;

A trait's current value on an entity, or undefined.

Type Parameters ​

Type Parameter
T extends Trait

Parameters ​

ParameterType
entityEntity
traitT

Returns ​

TraitRecord<T> | undefined


set() ​

ts
set<T>(
   entity, 
   trait, 
   value
): void;

Sets fields of a trait on an entity (tests and tools).

Type Parameters ​

Type Parameter
T extends Trait

Parameters ​

ParameterType
entityEntity
traitT
valuePartial<TraitRecord<T>>

Returns ​

void


has() ​

ts
has(entity, trait): boolean;

Parameters ​

ParameterType
entityEntity
traitTrait

Returns ​

boolean


query() ​

ts
query(...params): readonly Entity[];

Entities matching a query. Mut() and change filters are accepted and ignored.

Parameters ​

ParameterType
...paramsQueryParam[]

Returns ​

readonly Entity[]


emit() ​

ts
emit<T>(e, ...data): void;

Emits an event from outside a system (tests, DOM callbacks).

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
eEventType<T>
...dataT extends void ? [] : [T]

Returns ​

void


events() ​

ts
events<T>(e): readonly T[];

Every event of this type emitted during the last app.step() call (or browser frame).

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterType
eEventType<T>

Returns ​

readonly T[]


snapshot() ​

ts
snapshot(options?): Snapshot;

The world as JSON: app.snapshot({ traits: [Transform, Health] }). With no traits, every JSON-friendly trait on every entity. Add resources: [Score] or resources: true.

Parameters ​

ParameterType
options?SnapshotOptions

Returns ​

Snapshot


diff() ​

ts
diff(
   a, 
   b, 
   epsilon?
): SnapshotDiff;

Field-level changes between two snapshots. Numbers within epsilon count as equal.

Parameters ​

ParameterTypeDefault value
aSnapshotundefined
bSnapshotundefined
epsilonnumber0

Returns ​

SnapshotDiff


restore() ​

ts
restore(snapshot): object;

Replaces all entities (and listed resources) with a snapshot's. Traits match by name.

Parameters ​

ParameterType
snapshotSnapshot

Returns ​

object

missingTraits ​
ts
missingTraits: string[];

describe() ​

ts
describe(): AppDescription;

The built app as data: phases, systems with reads/writes/order, traits, resources, events.

Returns ​

AppDescription