Skip to content

Action State

ActionStateData is computed each frame by the input manager systems and provides read access to the current state of all actions.

MethodReturnsDescription
pressed(action)booleantrue while the action is held
justPressed(action)booleantrue for one frame on press
justReleased(action)booleantrue for one frame on release
value(action)numberAxis value, or 1/0 for buttons
axis2d(action){ x: number, y: number }2D axis value

When configured with globalMap, the action state is available as a world resource:

const MovementSystem = Define.system().update(({ resources }) => {
const state = resources.get('actionState');
const dir = state.axis2d('move');
// dir.x, dir.y
});

Spawn entities with the InputMap component. The ActionState component is added automatically by AutoAddActionStateSystem.

import { InputMap, ActionState, InputMapData } from '@shell/input-manager';
commands.spawn(
InputMap.set({
data: new InputMapData()
.bind('attack', { type: 'button', source: { kind: 'key', code: 'KeyJ' } })
.bind('move', { type: 'dualAxis', source: wasd() }),
}),
);

Query for entities and read their state:

const Players = Define.query({ data: [InputMap, ActionState] });
const PlayerSystem = Define.system()
.queries({ players: Players })
.update(({ queries }) => {
queries.players.forEach((entity) => {
const state = entity.$actionState.data;
if (state.justPressed('attack')) {
// handle attack
}
const dir = state.axis2d('move');
// move entity by dir.x, dir.y
});
});

InputMapData is a plain serializable class that maps actions to input bindings. It provides a fluent API for binding configuration.

const map = new InputMapData()
.bind('jump', { type: 'button', source: { kind: 'key', code: 'Space' } })
.bind('move', { type: 'dualAxis', source: wasd() })
.bind('move', { type: 'dualAxis', source: arrowKeys() }); // multiple bindings per action
MethodDescription
bind(action, ...inputs)Add one or more bindings for an action
unbind(action)Remove all bindings for an action
clear()Remove all bindings for all actions
ComponentFieldsDescription
InputMapdata: InputMapDataPer-entity input bindings
ActionStatedata: ActionStateDataPer-entity evaluated action state
SystemScheduleDescription
AutoAddActionStateSystemPreUpdateAutomatically adds ActionState to entities with InputMap but no ActionState
GlobalInputSystemPreUpdateEvaluates the global InputMapData and writes to the global ActionStateData resource
InputManagerSystemPreUpdateEvaluates per-entity InputMap and writes to per-entity ActionState