Set callbacks on actions
Setting callbacks on actions is one of the two main ways to respond to actions using the recommended workflow.
When you set up callbacks for your Action, the Action informs your code that a certain type of input has occurred, and your code can then respond accordingly.
The Input System offers the following ways to set up input callbacks:
- The PlayerInput component.
- Action callbacks.
- Action map callbacks
- The global On Action Change callback.
You can also use InputActionTrace to record all changes happening on actions, which is useful for debugging.
The Player Input component
With the Player Input component, you can set up callbacks using an interface in the inspector without requiring intermediate code. Refer to the PlayerInput component for further information.
Action callbacks
Every Action has a set of distinct phases it can go through in response to receiving input.
| Phase | Description |
|---|---|
Disabled |
The Action is disabled and can't receive input. |
Waiting |
The Action is enabled and is actively waiting for input. |
Started |
The Input System has received input that started an Interaction with the Action. |
Performed |
An Interaction with the Action has been completed. |
Canceled |
An Interaction with the Action has been canceled. |
You can read the current phase of an action using InputAction.phase.
The Started, Performed, and Canceled phases each have a callback associated with them:
jumpAction = InputSystem.actions.FindAction("Jump");
jumpAction.started += context => /* Action was started */;
jumpAction.performed += context => /* Action was performed */;
jumpAction.canceled += context => /* Action was canceled */;
Each callback receives an InputAction.CallbackContext structure, which holds context information that you can use to query the current state of the Action and to read out values from Controls that triggered the Action (InputAction.CallbackContext.ReadValue).
The contents of the callback context structure are only valid during the callback. In particular, it's not safe to store the received context and later access its properties from outside the callback.
When and how the callbacks are triggered depends on the interactions present on the respective bindings. If the bindings have no interactions that apply to them, the default interaction applies.
Action map callbacks
Instead of listening to individual actions, you can listen to an entire action map for state changes on any action it contains.
playerActionMap = InputSystem.actions.FindActionMap("Player");
playerActionMap.actionTriggered +=
context => { ... };
The argument received is the same InputAction.CallbackContext structure that you receive through the started, performed, and canceled callbacks.
Note
The Input System calls InputActionMap.actionTriggered for all three of the individual callbacks on Actions. That is, you get started, performed, and canceled all on a single callback.
Global input callback
Similar to InputSystem.onDeviceChange, your app can listen for any action-related change globally.
InputSystem.onActionChange +=
(obj, change) =>
{
// obj can be either an InputAction or an InputActionMap
// depending on the specific change.
switch (change)
{
case InputActionChange.ActionStarted:
case InputActionChange.ActionPerformed:
case InputActionChange.ActionCanceled:
Debug.Log($"{((InputAction)obj).name} {change}");
break;
}
}