Events Info

From WoWDev

Contents

Events Info

Introduction

Events are messages sent by the WoW client to the scripts. Events are sent to Frame objects.

Registering

In order for your Frame to receive an event, you have to do a few things:

  1. You need to use <myFrame>:RegisterEvent(nameOfEvent). Commonly, this can be this:RegisterEvent(nameOfEvent).
  2. The <myFrame> needs to have an <OnEvent> tag in its <Scripts> tag in the XML file.

It is possible to put the event handling code directly in the <OnEvent> tag in the XML file. Most AddOns, though, put a function in the lua file with a signature like

function YourMod_OnEvent()
  if(event == "PLAYER_AURAS_CHANGED") then
    YourMod_UpdateBuffs();
  end
end

Then, they hook the XML up to the lua with something like

<Frame>
    <Scripts>
        <OnEvent>
            YourMod_OnEvent();
        </OnEvent>
    </Scripts>
</Frame>

At the time that the code in the <OnEvent> tag is executed, a variable is accessible named event which contains the name of the event. This variable is not a global variable; it is local to one of the builtin functions, but we have access to it thanks to Lua's scoping rules (see http://www.lua.org/manual/5.0/manual.html#2.6).

Also available to the code in the <OnEvent> tag are the global variables arg1 to arg9. Each event specifies the values that it will put in these variables.


Variations Seen in the Wild

Though the examples above are one way to hook up event handlers, there are others as well. It is also possible to pass the event's name to the event handling function (rather than getting it through the normal lua scoping).

XML:

<Frame>
    <Scripts>
        <OnEvent>
            YourMod_OnEvent(event);
        </OnEvent>
    </Scripts>
</Frame>

LUA:

function YourMod_OnEvent(event)
  if(event == "PLAYER_AURAS_CHANGED") then
    YourMod_UpdateBuffs();
  end
end


Why you would want to do that, I am not sure. With this method, you are creating a local copy of the event within your lua function. With the method seen in the section above, you are ensured that you're using the proper global "event" variable.


It is also possible to include the args in the signature. This is commonly used when the event handler is only handling one event.

this:RegisterEvent("UNIT_SPELLMISS")
function YourMod_OnEvent(unitName, reason)    -- now we have realistic names for the args
<Frame>
    <Scripts>
        <OnEvent>
            YourMod_OnEvent(arg1, arg2);
        </OnEvent>
    </Scripts>
</Frame>

If you do pass either the event name or the args, be careful with your naming. If your function's signature looks like this

function YourMod_OnEvent(event, arg1, arg2)    -- dangerous

You are hiding the more global variables from your function. This is fine, as long as you remember to pass them in from the XML file. If you are getting a lot of nils that you aren't expecting, it's probably because you forgot to pass your variables into your function.

Unregistering

<myFrame>:UnregisterEvent(nameOfEvent); 

Unregister an event, when it's no longer needed. Useful if the addon can be disabled in game.