Conditional cooldown based casting in addons

I’m just getting into trying to write my own addon, after digging through the Lua files of an existing addon to submit a bugfix.

I’ve been recently using healbot just in party questing as a priest but thought it would be nice if my “heal button” would cast “Power Word: Shield” on the target if it hasn’t been cast within the last 15 seconds, or “Renew” if it has, and then finally “Heal” if Renew has also been recently cast.

Each button press would only cast one spell, but which spell would depend on the time since the last cast.

I understand that a /castsequence reset=N is based on when the macro was last attempted to be ran, so isn’t much use for this.

I’ve also read that addons don’t have any special ability in this regard either.

However, it seems like the WoW Lua API has the function Date(). Messily, this returns as a string, and only seems to be accurate to a second, but if one stores this somewhere (combined with some sort of target ID) shouldn’t you roughly be able to work out when a spell of a particular type was last cast, and do the sort of logic I’m suggesting above, although perhaps for example, I may have to wait 17 seconds, instead of 15 seconds, between Power Word: Shield casts to be safe?

Can something like this be done? Or, I’ve heard API access is limited during combat (and I certainly want this to work in combat). Will this foil my plans?

I’m also planning on targeting WoW classic if that makes a difference as that’s what I play on.

Time( ) should return a numeric value, i think, (seconds since a certain date/time). still 1 second resolution though.

if you want microsecond resolution then youd have to use debugprofilestop( )

eg;

local tz = debugprofilestop( )
-- your code goes here
tz = debugprofilestop( ) - tz
print( string.format( "that took %0.0fms", tz ) )

just dont call debugprofilestart, it reset the timer and will bugger up any other mod thats using this method for microsecond level timings. theres no real need to anyway.

this definitely works in combat as im using it to ensure my mod doesnt go anywhere near the in-combat script timer limit, which seems to be 200ms

it will also work in classic

Timing functions will work in combat, but casting spells based on conditions definitely won’t. All the CastSpell functions are protected and can only be called from Blizzard’s own code. The only way to cast spells in combat is by using SecureActionButtonTemplate, which restricts you to setting a specific spell and target before the combat starts.

2 Likes

Pins are your friend.

1 Like

Pins are not my friend in this case, because what you have quoted only starts macros cant do things based on cooldown, not addons.

Thanks for this response Ilendur! My further research made me suspect this was the case. I presume there’s no way to hide and show the buttons during combat? (the approach I’m considering around this is to place multiple buttons all in one place but only show one button at a time)

Same rule applies to addons, to prevent botting.