WoW:Detecting an instant cast spell

From AddOn Studio
Jump to navigation Jump to search

Using UNIT_SPELLCAST_SUCCEEDED to track instant casts[edit]

The core event here is UNIT_SPELLCAST_SUCCEEDED (hereafter _SUCCEEDED). This event fires whenever a spellcast is completed server end, regardless of whether it is instant or cast. Therefore whenever an instant cast has been used successfully, this event will fire. However, there are times when _SUCCEEDED will fire but the spell is not instant. The events UNIT_SPELLCAST_START, UNIT_SPELLCAST_FAILED, and UNIT_SPELLCAST_INTERRUPTED (hereafter _START, _FAILED, and _INTERRUPTED) allow us to track precisely which spellcasts are instant and which are not.

The key to this code is the relation between these events. Here are the relevant details:

  • All spells eventually trigger _SUCCEEDED if the cast completes, or _FAILED if it doesn't.
  • Only spells with cast times fire _START.
  • Every spell that starts must finish before another can begin; in other words, for every _START, we must hear finishing event (_SUCCEEDED, _FAILED or _INTERRUPTED) before we can hear another _START.

Thus, to detect instant spells, note when someone starts] casting a spell, then watch for a corresponding finishing event. Any finishing event that occurs without an unfinished start event preceding it describes an instant cast spell.

Example code[edit]

-- you should replace this with a better one
local print = function (msg)
  DEFAULT_CHAT_FRAME:AddMessage(msg)
end

is_casting = false
SampleTrackerFunctions = { }

SampleTracker = CreateFrame("Frame", nil, UIParent)
SampleTracker:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
SampleTracker:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
SampleTracker:RegisterEvent("UNIT_SPELLCAST_FAILED")
SampleTracker:RegisterEvent("UNIT_SPELLCAST_START")
SampleTracker:SetScript("OnEvent", function (_,e) SampleTrackerFunctions[e]() end)

----
function SampleTrackerFunctions.UNIT_SPELLCAST_SUCCEEDED()
  if not is_casting then
    print("An instant spell, oh my!")
  end
  is_casting = false
end
----
function SampleTrackerFunctions.UNIT_SPELLCAST_START()
  is_casting = true
end
----
function SampleTrackerFunctions.UNIT_SPELLCAST_FAILED()
  is_casting = false
end

Channeled Spells[edit]

Note that channeled spells are actually considered two casts by the event engine: first, the preceding component spellcast, then channeling component itself. For clarity, consider Mind Control. The preceding component is the 3-second cast before the spell takes effect, and the channeling component is the actual mind control portion of the spell. Note that every channeled spell has a preceding component, though many of them are instant.

In other words, don't be surprised when the sample code (or any code using this strategy) picks up abilities like Mind Vision or Drain Life as instant. Technically, there is an instant cast spell associated with such abilities.