Recipe: Bind Ability to Input¶
Goal: Press a button, activate an ability.
Prerequisites: Enhanced Input plugin enabled. A character with an ASC. An ability to bind. See Input Binding for the full architecture and complete code examples.
Quick Version¶
There are two approaches to input binding in GAS. Pick the one that fits your project -- both are covered in detail on the Input Binding page.
| InputID | Tag-Based | |
|---|---|---|
| Complexity | Lower | Higher |
| Scalability | Fixed enum slots | Unlimited tags |
| Ability task support | Automatic | Manual (AbilitySpecInputPressed/Released) |
| Works well for | Fixed input layouts | Dynamic ability sets, loadouts |
Steps (InputID Approach)¶
1. Create the Input Action¶
- Content Browser > right-click > Input > Input Action
- Name it:
IA_Attack - Set Value Type to
Digital (Bool)
2. Add to Input Mapping Context¶
- Open your IMC (e.g.,
IMC_Default) - Add a mapping: Input Action =
IA_Attack, Key = Left Mouse Button
3. Define an Input Enum (C++)¶
If you haven't already, create an EAbilityInput enum with entries for each bindable slot (PrimaryAttack, Dodge, Jump, etc.). See Input Binding -- Step 1 for the full enum.
4. Map InputAction to InputID¶
Add an entry to your character's AbilityInputBindings array (in Class Defaults or C++):
| Input Action | InputID |
|---|---|
IA_Attack |
PrimaryAttack |
5. Wire SetupPlayerInputComponent (C++)¶
Your character class needs a SetupPlayerInputComponent override that loops through the bindings and calls AbilityLocalInputPressed / AbilityLocalInputReleased on the ASC. See Input Binding -- Steps 3-4 for the complete code.
6. Grant the Ability with InputID¶
When granting the ability, pass the InputID to the FGameplayAbilitySpec constructor:
FGameplayAbilitySpec Spec(AbilityClass, 1, static_cast<int32>(EAbilityInput::PrimaryAttack), this);
AbilitySystemComponent->GiveAbility(Spec);
7. Test¶
- PIE
- Press the bound key
- Verify the ability activates (
showdebug abilitysystem) - Release the key -- verify held abilities respond to release
- Check Stamina/cooldown if the ability has cost/cooldown effects
Checklist¶
- Input Action created (
IA_) - Input Action added to Input Mapping Context with a key
- Input enum defined with an entry for this ability
- InputAction mapped to InputID (character array or data asset)
-
SetupPlayerInputComponentwires the binding loop (C++) - Ability granted with the correct InputID
- Tested in PIE
Using Tag-Based Routing Instead?¶
Follow the same first two steps (create IA, add to IMC), then:
- Create an
InputTag(e.g.,InputTag.Attack) - Map the IA to the tag in your character's input config
- Grant the ability with the tag added to
DynamicSpecSourceTags - Call
AbilitySpecInputPressed/Releasedin your routing callbacks -- without this,WaitInputReleasebreaks silently
See Input Binding -- Tag-Based Routing for the complete walkthrough and The Critical Detail for why the AbilitySpecInputPressed call matters.
Related¶
- Input Binding -- full architecture, both approaches, complete code
- Add an Ability -- creating and granting abilities
- Net Execution Policies -- how input interacts with networking