Actions are a powerful system for handling input and events in your game while maintaining context-aware control over when these events can occur. The system consists of three main components: contexts, actions, and controls.
Contexts
Contexts represent different states or modes of your game. For example:
Common contexts include:
default - Base game state
game - Active gameplay
menu - Menu navigation
popup - Popup/modal dialogs
pause - Paused state
Defining Actions
Actions are defined with their allowed contexts using defineActions:
Each action specifies:
An action identifier (e.g., move_left)
Context(s) where the action is allowed
'*' means the action is allowed in all contexts
Defining Buttons
The action system supports touch controls through buttons and joysticks. Here’s how to configure them:
Connecting Controls
Actions can be mapped to different input methods using defineControls:
3. Registering Buttons
In your game code, create and register button instances with the touch controls:
How It Works
Context Management:
The ActionsPlugin maintains the current context and validates actions against it:
Action Dispatching:
Actions can be dispatched using:
Action Handling:
You can listen for actions:
Benefits
Context Control: Actions are automatically filtered based on the current context, preventing unwanted inputs during inappropriate game states.
Input Abstraction: The action system separates input handling from game logic, making it easier to:
Support multiple input methods (keyboard, touch, gamepad)
Change control schemes without modifying game logic
Add new input methods
Type Safety: The system is fully typed, providing autocomplete and compile-time checking:
Example
The following example demonstrates how the action system ensuring that player movement is automatically disabled when the game is paused, while still allowing pause-specific actions like “resume” to function.