Action State
ActionStateData is computed each frame by the input manager systems and provides read access to the current state of all actions.
Methods
Section titled “Methods”| Method | Returns | Description |
|---|---|---|
pressed(action) | boolean | true while the action is held |
justPressed(action) | boolean | true for one frame on press |
justReleased(action) | boolean | true for one frame on release |
value(action) | number | Axis value, or 1/0 for buttons |
axis2d(action) | { x: number, y: number } | 2D axis value |
Global Input Map
Section titled “Global Input Map”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});Per-Entity Input Maps
Section titled “Per-Entity Input Maps”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
Section titled “InputMapData”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| Method | Description |
|---|---|
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 |
Components
Section titled “Components”| Component | Fields | Description |
|---|---|---|
InputMap | data: InputMapData | Per-entity input bindings |
ActionState | data: ActionStateData | Per-entity evaluated action state |
Systems
Section titled “Systems”| System | Schedule | Description |
|---|---|---|
AutoAddActionStateSystem | PreUpdate | Automatically adds ActionState to entities with InputMap but no ActionState |
GlobalInputSystem | PreUpdate | Evaluates the global InputMapData and writes to the global ActionStateData resource |
InputManagerSystem | PreUpdate | Evaluates per-entity InputMap and writes to per-entity ActionState |