Recipe: Bind Ability to Input¶
Goal: Press a button, activate an ability. Using the Enhanced Input + InputTag pattern.
Prerequisites: Enhanced Input plugin enabled. An ability to bind. See Input Binding for the concepts.
Steps¶
1. Create the Input Action (IA)¶
- Content Browser > right-click > Input > Input Action
- Name it:
IA_Attack - Set Value Type to
bool(for press/release) orAxis1D/Axis2Dfor analog - Save
2. Add to Input Mapping Context (IMC)¶
- Open your IMC (e.g.,
IMC_Default) - Click + to add a mapping
- Set the Input Action to
IA_Attack - Assign a key (e.g., Left Mouse Button)
- Add modifiers/triggers as needed (e.g., Pressed trigger for single activation)
- Save
3. Create the InputTag¶
If it doesn't already exist, create the tag:
# DefaultGameplayTags.ini
+GameplayTagList=(Tag="InputTag.Attack",DevComment="Primary attack input")
Or add it in Project Settings > GameplayTags > Manage Gameplay Tags.
4. Map InputAction to InputTag¶
This step depends on your input binding implementation. The common approach uses a data asset or array that maps IA to tag:
Create a data asset or DataTable entry:
USTRUCT(BlueprintType)
struct FInputActionTagBinding
{
GENERATED_BODY()
UPROPERTY(EditAnywhere)
TObjectPtr<UInputAction> InputAction;
UPROPERTY(EditAnywhere, Meta = (Categories = "InputTag"))
FGameplayTag InputTag;
};
In your input data:
In your character's input setup:
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(PlayerInputComponent);
for (const FInputActionTagBinding& Binding : InputBindings)
{
EIC->BindAction(Binding.InputAction, ETriggerEvent::Triggered,
this, &AMyCharacter::OnAbilityInputPressed, Binding.InputTag);
EIC->BindAction(Binding.InputAction, ETriggerEvent::Completed,
this, &AMyCharacter::OnAbilityInputReleased, Binding.InputTag);
}
}
void AMyCharacter::OnAbilityInputPressed(FGameplayTag InputTag)
{
ASC->AbilityLocalInputPressed(InputTag);
}
void AMyCharacter::OnAbilityInputReleased(FGameplayTag InputTag)
{
ASC->AbilityLocalInputReleased(InputTag);
}
5. Set InputTag on the Ability¶
Open your ability Blueprint (e.g., GA_LightAttack):
- In Class Defaults, find the InputTag property (or your project's equivalent)
- Set it to
InputTag.Attack
Implementation varies
The exact property name depends on your base ability class. Some projects use AbilityInputAction, ActivationTag, or a custom property. The concept is the same: the ability declares which input tag activates it.
6. Grant the Ability¶
Make sure the ability is granted to the character (see Add an Ability). When granting, the InputTag is used to associate the ability spec with the input:
FGameplayAbilitySpec Spec(AbilityClass, Level, INDEX_NONE, this);
// If using the input tag approach, the ability's CDO InputTag
// is read during activation matching
ASC->GiveAbility(Spec);
7. Test¶
- PIE
- Press the bound key (Left Mouse Button)
- Verify the ability activates
- Release the key -- verify the ability handles release if needed (for hold abilities)
- Check
showdebug abilitysystemto see the ability in the active/granted list
Checklist¶
- Input Action created (IA_)
- Input Action added to Input Mapping Context
- InputTag created (e.g.,
InputTag.Attack) - IA mapped to InputTag (data asset or code)
- Ability's InputTag property set
- Ability granted to character
- Tested in PIE
Related¶
- Input Binding -- concepts and architecture
- Add an Ability -- creating and granting abilities
- Net Execution Policies -- how input interacts with networking