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 gray the button out before the player presses it.
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).
3. Gray 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.
4. Make a cost augment-modifiable
By default the shipped UCrimsonAbilityCost_PlayerTagStack charges its authored Quantity. To let augments change the price, have the ability's contract publish the Ability.Property.Cost property - the cost object reads that instead whenever it is present.
This is the same mechanism cooldowns use with Ability.Property.Cooldown, and it is preferred over reacting to a cost event: a published property flows through EvaluatePropertyCache, so augment tooltips can show the player the before-and-after number automatically. A cost changed by some other route is invisible to that preview.
Ability publishes Ability.Property.Cost | What the cost charges |
|---|---|
| No (default) | The authored Quantity FScalableFloat at the ability's level. |
| Yes | The published value, including a published 0 - 'this cast is free' is a legitimate augment. |
CheckCost and ApplyCost both go through one ResolveStackCount. If you write your own cost object, do the same - a check that passes at one price and a deduction at another is a bug that only shows up once an augment makes the two disagree.Ability.Property.Cost and hover the ability. The tooltip shows the modified cost, and spending the ability deducts the new amount rather than the authored one. See How-To: Declare an Ability Contract for publishing the property and How-To: Write Augment Tooltips for the preview.See also
- Concept: Gameplay Ability