Skip to content

API Reference

Scene-specific state that automatically cleans up when the scene is destroyed.

function withLocalState<T>(
scene: Phaser.Scene,
key: string,
initialValue: T,
options?: HookOptions<T>
): HookState<T>
ParameterTypeDescription
scenePhaser.SceneThe Phaser scene instance
keystringUnique identifier for this state
initialValueTInitial state value
optionsHookOptions<T>Optional configuration

HookState<T> - State management object

const playerState = withLocalState(this, 'player', {
hp: 100,
level: 1,
});
console.log(playerState.get()); // { hp: 100, level: 1 }

Application-wide state that persists across all scenes.

function withGlobalState<T>(
scene: Phaser.Scene,
key: string,
initialValue: T,
options?: HookOptions<T>
): HookState<T>
ParameterTypeDescription
scenePhaser.SceneThe Phaser scene instance
keystringUnique identifier for this state
initialValueTInitial state value
optionsHookOptions<T>Optional configuration

HookState<T> - State management object

const settingsState = withGlobalState(this, 'settings', {
soundVolume: 0.8,
musicEnabled: true,
});
console.log(settingsState.get()); // { soundVolume: 0.8, musicEnabled: true }

State with automatic localStorage or sessionStorage persistence.

function withPersistentState<T>(
key: string,
initialValue: T,
storage: 'local' | 'session',
options?: HookOptions<T>
): HookState<T>
ParameterTypeDescription
keystringUnique identifier for this state
initialValueTInitial state value (used if no saved value exists)
storage'local' | 'session'Storage type: ‘local’ for localStorage, ‘session’ for sessionStorage
optionsHookOptions<T>Optional configuration

HookState<T> - State management object

const userSettings = withPersistentState(
'settings',
{ volume: 0.8, difficulty: 'normal' },
'local'
);
// Updates automatically save to localStorage
userSettings.set({ volume: 0.5, difficulty: 'hard' });

Derived state that automatically updates when source state changes.

function withComputedState<TSource, TComputed>(
scene: Phaser.Scene,
key: string,
sourceState: HookState<TSource>,
computeFn: (source: TSource) => TComputed,
options?: HookOptions<TComputed>
): HookState<TComputed>
ParameterTypeDescription
scenePhaser.SceneThe Phaser scene instance
keystringUnique identifier for this state
sourceStateHookState<TSource>Source state to watch
computeFn(source: TSource) => TComputedFunction to compute derived value
optionsHookOptions<TComputed>Optional configuration

HookState<TComputed> - Computed state object (read-only)

const playerState = withLocalState(this, 'player', {
hp: 75,
maxHp: 100,
});
const healthPercent = withComputedState(
this,
'healthPercent',
playerState,
(player) => Math.round((player.hp / player.maxHp) * 100)
);
console.log(healthPercent.get()); // 75

State with built-in undo/redo functionality.

function withUndoableState<T>(
scene: Phaser.Scene,
key: string,
initialValue: T,
historyLimit?: number,
options?: HookOptions<T>
): UndoableHookState<T>
ParameterTypeDescription
scenePhaser.SceneThe Phaser scene instance
keystringUnique identifier for this state
initialValueTInitial state value
historyLimitnumberMaximum number of history entries (default: 50)
optionsHookOptions<T>Optional configuration

UndoableHookState<T> - State object with undo/redo methods

MethodDescriptionReturns
undo()Reverts to previous stateboolean - Success status
redo()Advances to next stateboolean - Success status
canUndo()Checks if undo is availableboolean
canRedo()Checks if redo is availableboolean
clearHistory()Clears undo/redo historyvoid
const gameHistory = withUndoableState(this, 'history', 'start', 10);
gameHistory.set('move-1');
gameHistory.set('move-2');
gameHistory.undo(); // Back to 'move-1'
gameHistory.redo(); // Forward to 'move-2'
console.log(gameHistory.canUndo()); // true
console.log(gameHistory.canRedo()); // false

State with debounced updates to prevent rapid successive changes.

function withDebouncedState<T>(
scene: Phaser.Scene,
key: string,
initialValue: T,
delay: number,
options?: HookOptions<T>
): HookState<T>
ParameterTypeDescription
scenePhaser.SceneThe Phaser scene instance
keystringUnique identifier for this state
initialValueTInitial state value
delaynumberDebounce delay in milliseconds
optionsHookOptions<T>Optional configuration

HookState<T> - State management object

const searchState = withDebouncedState(this, 'search', '', 300);
// These rapid calls will be debounced
searchState.set('a');
searchState.set('ab');
searchState.set('abc');
// Only 'abc' will be set after 300ms

Batches multiple state updates together.

function batchStateUpdates(updateFn: () => void): void
ParameterTypeDescription
updateFn() => voidFunction containing state updates
batchStateUpdates(() => {
playerState.set({ ...playerState.get(), hp: 90 });
inventoryState.set([...inventoryState.get(), 'sword']);
scoreState.set(scoreState.get() + 100);
});

The core state management interface returned by all hooks.

interface HookState<T> {
get(): T;
set(value: T): void;
on(event: 'change', callback: (newValue: T, oldValue: T) => void): () => void;
once(event: 'change', callback: (newValue: T, oldValue: T) => void): () => void;
off(event: 'change', callback: (newValue: T, oldValue: T) => void): void;
clearListeners(): void;
}
MethodDescriptionParametersReturns
get()Gets current state valueNoneT
set(value)Sets new state valuevalue: Tvoid
on('change', callback)Registers change listenerevent: 'change', callback: (newValue: T, oldValue: T) => void() => void (unsubscribe function)
once('change', callback)Registers one-time listenerevent: 'change', callback: (newValue: T, oldValue: T) => void() => void (unsubscribe function)
off('change', callback)Removes listenerevent: 'change', callback: (newValue: T, oldValue: T) => voidvoid
clearListeners()Removes all listenersNonevoid

Extended state interface with undo/redo functionality.

interface UndoableHookState<T> extends HookState<T> {
undo(): boolean;
redo(): boolean;
canUndo(): boolean;
canRedo(): boolean;
clearHistory(): void;
}

Configuration options for hooks.

interface HookOptions<T> {
validator?: (value: T) => true | string;
debug?: boolean;
}
PropertyTypeDescription
validator(value: T) => true | stringOptional validation function. Return true if valid, or an error message string if invalid.
debugbooleanEnable debug logging for this state (default: false)
const healthState = withLocalState(
this,
'health',
100,
{
validator: (value) => {
if (value < 0) return 'Health cannot be negative';
if (value > 100) return 'Health cannot exceed 100';
return true;
},
debug: true,
}
);