Making any ability use charges

A guide/snippet which will help you to make any ability use charges like Shrapnel or Stone Caller.

First, add save this file with a name "modifier_charges.lua" to your vscripts folder (or any subfolder inside of it)

Then, add an initialization line to your addon_game_mode.lua:

LinkLuaModifier("modifier_charges", LUA_MODIFIER_MOTION_NONE)

If your file is into a subfolder you can do it like that

LinkLuaModifier("modifier_charges", "subfolder/anothersubfolder/modifier_charges", LUA_MODIFIER_MOTION_NONE)

Gratz, you've successfully installed it!

Now you can add charges to any ability with this code:

unit:AddNewModifier(unit, unit:FindAbilityByName("ability_name"), "modifier_charges",
            max_count = 2,
            start_count = 1,
            replenish_time = 6

The settings in the end are pretty self-explanatory. You can omit the start_count if you want.

That's it, folks.


  • edited December 2015 Posts: 1,670

    That IsServer() usage is great, I didn't know functions could be defined that way but it saves a lot of client trouble.

    Do you need to return 0 on OnAbilityExecuted for any reason in particular?

    I'd add that linking lua modifiers for specific abilities is better done in the abilities themselves than in addon_game_mode or game init.

    Added to the tutorial list. Also nice idea using Gists, we should use more of those (and even have a built-in reader pluging like the pastebin one)

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • Not sure how OnAbilityExecuted return part works honestly, just got that from lina_fiery_soul example.

    You are right about linking modifiers, it's just this modifier can be used in multiple abilities, and I'm not entirely sure that linking a single modifier multiple times is a right thing to do.

    Gists are very nice since they have diffs and edits yeah.

  • edited September 2016 Posts: 15

    I must have messed something up? Charges are correctly stacking up, but are not being spent when the ability is cast. I didn't change any of the code. Just directly copy/pasted per the instructions.

    Perhaps this LUA-based sort of thing isn't playing nice with my spell because mine is DataDriven, using RunScript for it's effect?

  • edited September 2016 Posts: 188

    The instructions for attaching the modifier to ability should only be ran once. If you apply the modifier every time you cast ability then the modifier is applied multiple times.
    Basically what this lua modifier does is notice when your ability is cast(reason we need the unit:FindAbilityByName("ability_name")), then decrements the stack by one. If stack becomes zero it applies cooldown equal to "remaining charge time" to your ability.

  • So it won't work with DataDriven spells that use RunScript, because it would be applying the modifier on every cast?

  • Posts: 74

    Just add an initialization check so that you only apply it the first time

  • I'm not yet familiar enough with LUA to know how to do that.

  • Posts: 74

    just do

    if not hero.initializedAbilityName then --Add modifier hero.initializedAbilityName = true end
  • Looks like it's working. Thanks for the help!

  • this has made me wonder, what does IsServer() do exactly? what does it check

  • IsServer() returns false if script runs on client.