How-To: Add Ability Costs

Goal: charge something other than an attribute - a tag-stack resource, a consumable, a custom rule - and have the UI grey the button out before the player presses it.

Prerequisites
Quick Start.

1. Add a cost object

UCrimsonGameplayAbility holds an array of UCrimsonAbilityCost instances. Every one must pass for the ability to activate, and all of them are applied together on commit.

In the ability's Class Defaults, add an entry to Additional Costs and choose a cost class - CrimsonAbilityCost_PlayerTagStack ships with the plugin.

2. Write a custom cost

Subclass UCrimsonAbilityCost and override two BlueprintNativeEvents.

Right-click -> Blueprint Class -> `CrimsonAbilityCost`, then override K2 Check Cost (return whether the player can pay) and K2 Apply Cost (deduct it).

Verify
Activation fails when the cost cannot be paid, and the resource is deducted when it can.

3. Grey out the UI before the press

Check Ability Cost For UI runs the same checks without activating, so a button can reflect affordability every frame.

Bind the button's Is Enabled to Check Ability Cost For UI on the ability.

Forcing a commit
Force Commit Ability Cost pays the cost outside the normal commit point - useful when a cast-time ability should charge at a moment of your choosing.
Verify
Drain the resource and watch the button disable without any activation attempt.

See also

  • Concept: Gameplay Ability