Skip to content

Class: ChoicekitEngine<TEngineGenerics>

Defined in: engine/if-engine.ts:162

The main engine for Choicekit that provides headless interface to basic utilities required for Interactive Fiction

Dispatches custom events that can be listened to with “addEventListener”

TEngineGenerics extends ChoicekitEngineGenerics = ChoicekitEngineGenerics

$

$: MapPluginsToApiSurface<TEngineGenerics["plugins"]> = {}

Defined in: engine/if-engine.ts:170


readonly name: TEngineGenerics["name"]

Defined in: engine/if-engine.ts:167

Must be unique to prevent conflicts

get index(): number

Defined in: engine/if-engine.ts:1034

The current position in the state history that the engine is playing.

This is used to determine the current state of the engine.

READONLY VERSION

number


get passage(): TEngineGenerics["passages"] | null

Defined in: engine/if-engine.ts:1042

Returns the passage data for the current state.

If the passage does not exist, returns null.

TEngineGenerics["passages"] | null


get passageId(): TEngineGenerics["passages"]["name"]

Defined in: engine/if-engine.ts:1047

Returns the id to the appropriate passage for the current state

TEngineGenerics["passages"]["name"]


get random(): number

Defined in: engine/if-engine.ts:1052

Based off an internal PRNG, returns a random float between 0 and 1, inclusively

number


get vars(): Readonly<typeof this._type.state.complete>

Defined in: engine/if-engine.ts:1072

Returns a readonly copy of the current state of stored variables.

May be expensive to calculate depending on the history of the story.

Readonly<typeof this._type.state.complete>

addPassage(passageData): void

Defined in: engine/if-engine.ts:428

Adds a new passage to the engine.

The passage id should be unique, and the data can be anything that you want to store for the passage.

If the passage already exists, it will be overwritten.

TEngineGenerics["passages"]

void


addPassages(…passageData): void

Defined in: engine/if-engine.ts:433

Like addPassage, but takes in a collection

…readonly TEngineGenerics["passages"][]

void


backward(step?): void

Defined in: engine/if-engine.ts:443

Moves at least one step backwards in the state history.

Does nothing if already at the first state snapshot.

number = 1

void


deleteAllSaveSlots(): Promise<unknown>

Defined in: engine/if-engine.ts:453

Promise<unknown>


deleteSaveSlot(saveSlot?): Promise<unknown>

Defined in: engine/if-engine.ts:473

Deletes any save data associated with the provided save slot.

number

if not provided, defaults to the autosave slot

Promise<unknown>

if the save slot is invalid or if the persistence adapter is not available


forward(step?): void

Defined in: engine/if-engine.ts:506

Moves at least one step forward in the state history.

Does nothing if already at the most recent state snapshot.

number = 1

void


getPassages(query): readonly TEngineGenerics["passages"][]

Defined in: engine/if-engine.ts:555

Returns an array of passages that match the specified tags.

{ tags: [...TEngineGenerics["passages"]["tags"][]]; type: "all"; } | { tags: [...TEngineGenerics["passages"]["tags"][]]; type: "any"; }

{ tags: [...TEngineGenerics["passages"]["tags"][]]; type: "all"; }

[...TEngineGenerics["passages"]["tags"][]]

"all"

Matches any passage that has all of the given tags


{ tags: [...TEngineGenerics["passages"]["tags"][]]; type: "any"; }

[...TEngineGenerics["passages"]["tags"][]]

"any"

Matches any passage that has at least one of the given tags

readonly TEngineGenerics["passages"][]

An array of passages that match the specified tags.


getSaves(): AsyncGenerator<{ data: SaveData<TEngineGenerics["vars"]>; type: "autosave"; } | { data: SaveData<TEngineGenerics["vars"]>; slot: number; type: "normal"; }>

Defined in: engine/if-engine.ts:517

Returns an object containing the data of all present saves

AsyncGenerator<{ data: SaveData<TEngineGenerics["vars"]>; type: "autosave"; } | { data: SaveData<TEngineGenerics["vars"]>; slot: number; type: "normal"; }>


getVisitCount(passageId?): number

Defined in: engine/if-engine.ts:600

Gets all the times the passage has been visited by looping through each snapshot and initial state.

Use this in place of hasVisited(id), i.e getVisitCount(id) > 0

string = ...

TODO: benchmark this later to see if caching will be beneficial

number


loadFromExport(data): Promise<void>

Defined in: engine/if-engine.ts:621

Can be used when directly loading a save from an exported save on disk

string

Promise<void>

if the save was made on a later version than the engine or if a save migration throws


loadFromSaveSlot(saveSlot?): Promise<void>

Defined in: engine/if-engine.ts:647

number

if not provided, defaults to the autosave slot

Promise<void>

if the save slot is invalid or if the persistence adapter is not available


loadRecentSave(): Promise<void>

Defined in: engine/if-engine.ts:675

Loads the most recent save, if any. Doesn’t throw

Promise<void>


loadSaveFromData(save): Promise<void>

Defined in: engine/if-engine.ts:695

Loads the save data from the provided save data object.

This is used to load saves from the getSaves() method.

Readonly<{ intialState: TEngineGenerics["vars"] & SnapshotMetadata; lastPassageId: string; plugins: Record<string, ChoicekitPluginSaveStructure>; savedOn: Date; snapshots: Partial<TEngineGenerics["vars"] & SnapshotMetadata>[]; storyIndex: number; version: `${number}.${number}.${number}`; }>

The save data to load

Promise<void>

if the save was made on a later version than the engine or if a save migration throws


navigateTo(passageId?): Partial<TEngineGenerics["vars"] & SnapshotMetadata>

Defined in: engine/if-engine.ts:857

Creates and moves the index over to a new snapshot with the given passage id (or the previous one) and returns a reference to it.

This is essentially the way of linking between passages in the story.

Yes, you can navigate to the same passage multiple times, and it will create a new snapshot each time. It’s intended behavior.

TEngineGenerics["passages"]["name"] = ...

Partial<TEngineGenerics["vars"] & SnapshotMetadata>

if the passage id hasn’t been added to the engine


off<TEventName>(eventName, listener): void

Defined in: engine/if-engine.ts:902

Unsubscribe from an event

TEventName extends keyof ChoicekitEvents<TEngineGenerics["passages"], TEngineGenerics["vars"]>

TEventName

(payload) => void

void


on<TEventName>(eventName, listener): () => void

Defined in: engine/if-engine.ts:887

Subscribe to an event.

TEventName extends keyof ChoicekitEvents<TEngineGenerics["passages"], TEngineGenerics["vars"]>

TEventName

(payload) => void

a function that can be used to unsubscribe from the event.

() => void


once<TEventName>(eventName, listener): () => void

Defined in: engine/if-engine.ts:894

TEventName extends keyof ChoicekitEvents<TEngineGenerics["passages"], TEngineGenerics["vars"]>

TEventName

(payload) => void

() => void


registerClasses(…customClasses): void

Defined in: engine/if-engine.ts:910

Any custom classes stored in the story’s state must be registered with this

ChoicekitClassConstructorWithValidSerialization[]

void


registerMigrators<TOldSaveStructure, TNewSaveStructure>(…migrators): void

Defined in: engine/if-engine.ts:922

Use this to register custom callbacks for migrating outdated save data

TOldSaveStructure

TNewSaveStructure = TEngineGenerics["vars"]

object[]

void

if a migration for the same version already exists


reset(resetSeed?): void

Defined in: engine/if-engine.ts:950

Clears all snapshot data and reverts to the initial state.

Use this if you want the engine to essentially, start “afresh”

boolean = false

if true, the initial seed is randomised

void


saveToExport(): Promise<string>

Defined in: engine/if-engine.ts:968

For saves the need to exported out of the browser

Promise<string>


saveToSaveSlot(saveSlot?): Promise<void>

Defined in: engine/if-engine.ts:999

Using the provided persistence adapter, this saves all vital data for the combined state, metadata, and current index

number

if not provided, defaults to the autosave slot

Promise<void>

if the persistence adapter is not available


setVars(producer, emitEvent?): void

Defined in: engine/if-engine.ts:364

Immer-style way of updating story variables

Use this solely for setting values. If you must read a value, use this.vars

If you need to replace the entire state, return a new object instead of directly assigning the value

((variables) => void) | ((variables) => TEngineGenerics["vars"])

boolean = true

If true, a “stateChange” event will be emitted. Set this to false if you use it within a stateChange listener

void


static init<TGenerics>(args): Promise<ChoicekitEngine<TGenerics>>

Defined in: engine/if-engine.ts:298

TGenerics extends ChoicekitEngineGenerics

Partial<ChoicekitEngineArguments<TGenerics>>

Promise<ChoicekitEngine<TGenerics>>