DEV Community

Cover image for GAS Input Tags: Ability Activation Without Hardcoded Bindings
Marko Petrić
Marko Petrić

Posted on • Originally published at marko-ue.hashnode.dev

GAS Input Tags: Ability Activation Without Hardcoded Bindings


What are input tags in GAS and why you should use them

Input tags are one way of activating gameplay abilities in GAS. They allow you to easily tie any gameplay ability's activation input to a specific gameplay tag, instead of hardcoding bindings. If you're new to gameplay tags, my earlier post covers them.

Input tags also let you rebind or assign abilities at runtime without touching input code.


How to add an input tag to an ability

The way input tags are added to abilities is by accessing that ability's spec, and adding your input tag to its dynamic spec source tags, which are just an FGameplayTagContainer . They are carried over with that ability's spec, and can always be accessed if you have that spec.

In my case, for adding input tags, I have an FAbilitySet struct which lets me choose to add an input tag to any abilities I give, and lets me access the relevant spec more easily. I give these abilities with their relevant input tags when the game starts, but you can choose to add them in many places and at runtime. The tag you give can be either an editor or native gameplay tag, and you are not required to use a struct.

// Maps abilities to input tags
USTRUCT()
struct FAbilitySet
{
    GENERATED_BODY()

    UPROPERTY(EditDefaultsOnly)
    TSubclassOf<UGameplayAbility> AbilityClass;

    UPROPERTY(EditDefaultsOnly)
    FGameplayTag InputTag;
};
Enter fullscreen mode Exit fullscreen mode
for (const FAbilitySet& Set : StartupAbilities)
{
    FGameplayAbilitySpec AbilitySpec(Set.AbilityClass);
    AbilitySpec.GetDynamicSpecSourceTags().AddTag(Set.InputTag);
    GetAbilitySystemComponent()->GiveAbility(AbilitySpec);
}
Enter fullscreen mode Exit fullscreen mode

How to activate an ability using an input tag

Now that your ability has an input tag given to its dynamic spec source tags, you can check for it when using input.

To check for it, in your input function, you can access the ability's spec, then use GetDynamicSpecSourceTags on that spec, and check if it has the specific tag(s) you choose to pass in. In my case, I'm checking for the input tag for primary actions.

void AComplyPlayerCharacter::PrimaryActionPressed()
{
    for (FGameplayAbilitySpec& Spec : GetAbilitySystemComponent()->GetActivatableAbilities())
    {

        if (Spec.GetDynamicSpecSourceTags().HasTagExact(
        ComplyTags::ComplyAbilities::InputTags::Input_Primary))
        {
            GetAbilitySystemComponent()->TryActivateAbility(Spec.Handle);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

So in the above example, when pressing the primary input, we loop over all activatable abilities on the AbilitySystemComponent, and if an activatable ability has the Input_Primary input tag, it will be activated.


Conclusion

Input tags in GAS are a simple but highly useful and extendable system for handling input for activating abilities. What I've shown is just the basic template, but it's enough to set you up for creating your own advanced input system with things like holding input to activate abilities, or multi-tag checks.

If you have any questions or feedback, feel free to contact me on LinkedIn, or email me: petric.marko04@gmail.com

Top comments (0)