Thread Tools Display Modes
12-05-10, 06:32 PM   #1
Waky
A Cobalt Mageweaver
 
Waky's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2010
Posts: 200
Event arguments

I've never really been able to understand how to work in arguments into my "OnEvent" frames, and now I need to use it. I'm trying to make it so my addon does a certain function when the auras of my target change, so I went about it like this:

Code:
local OnEvent = CreateFrame("Frame")
OnEvent:SetScript("OnEvent", function(self, event, addon, ...)
if (event=="UNIT_AURA") then
   if (arg1=="target") then
      myFunction()
   end
end
end)
OnEvent:RegisterEvent("UNIT_AURA")
I have no idea if I'm doing this right, I'm going to guess no since it's not working :P If anyone could help I'd be very appreciative!

Thanks!!
  Reply With Quote
12-05-10, 07:10 PM   #2
acapela
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 241
with UNIT_AURA as an example, and assuming the rest of your code is 100% correct (i use Ace3, so what i am used to would be slightly different than what you have here):

Code:
local OnEvent = CreateFrame("Frame")
OnEvent:SetScript("OnEvent", function(self, event, addon, ...)
if (event=="UNIT_AURA") then
   local unitid = select(1,...)
   if (unitid=="target") then
      myFunction(unitid)
   end
end
end)
OnEvent:RegisterEvent("UNIT_AURA")
this would pass the unitid argument (the only "argument") from the UNIT_AURA event down to your "myFunction".

presumably from LUA (as opposed to from XML), you could also do:

Code:
local OnEvent = CreateFrame("Frame")
OnEvent:SetScript("OnEvent", function(self, event, addon, arg1, arg2, arg3, arg4, arg5)
if (event=="UNIT_AURA") then
   if (arg1=="target") then
      myFunction(arg1)
   end
end
end)
OnEvent:RegisterEvent("UNIT_AURA")
where you added extra "argN" parameters to your handler, and determined which were valid based on what event you received, and passed them down selectively as needed.

i don't know which approach is officially considered more kosher in LUA, added extra (potentially bogus) parameters to a handler, or demultiplexing varargs using "select". from LUA, i would probably opt for the former (adding extra potentially bogus parameters to the handler). from XML you would probably have to use "select" in some form, sooner or later.

hope that helps.
__________________
Retired author/maintainer of Aloft (the nameplate addon)
http://www.wowinterface.com/download...AloftBeta.html
-----
Zippy said it best: "All life is a BLUR of Republicans and Meat!"

Last edited by acapela : 12-05-10 at 07:12 PM.
  Reply With Quote
12-05-10, 07:15 PM   #3
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
3rd argument in every OnEvent script is arg1, so "addon" is actually arg1.
  Reply With Quote
12-05-10, 07:17 PM   #4
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
self and event are always passed with the event, the rest varies from event to event.

Take UNIT_AURA for example, it passes "unitid" as well as those mentioned above [1]. But you have to catch it yourself nowadays.

So your example would either be (only showing the relevant bits):
Code:
OnEvent:SetScript("OnEvent", function(self, event, ...)
if event == "UNIT_AURA" then
   local unit = select(1, ...)
   if unit == "target" then 
      <snip>
or

Code:
OnEvent:SetScript("OnEvent", function(self, event, unit, ...)
   if unit == "target" then
      <snip>
Either way is fine, but the latter basically assumes you've only registered for the UNIT_AURA event (or any other event with that particular signature) while the first can be used for other events as well.

[1] http://wowprogramming.com/docs/events/UNIT_AURA
__________________
Oh, the simulated horror!

Last edited by Ailae : 12-05-10 at 07:20 PM. Reason: Clarification..
  Reply With Quote
12-05-10, 07:23 PM   #5
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
One other possible way of doing this is the following.

Code:
local addon = CreateFrame('Frame')
addon:SetScript('OnEvent', function(self, event, ...) self[event](self, ...) end)

addon:RegisterEvent('UNIT_AURA')
function addon:UNIT_AURA(...)
    -- magic
end
Its a tad more advanced, but it is easier to work with, especially if you have multiple events.
  Reply With Quote
12-05-10, 07:33 PM   #6
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Definately, I prefer that way as soon as I am listening for more than one event.
__________________
Oh, the simulated horror!
  Reply With Quote
12-05-10, 07:59 PM   #7
Waky
A Cobalt Mageweaver
 
Waky's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2010
Posts: 200
Originally Posted by p3lim View Post
3rd argument in every OnEvent script is arg1, so "addon" is actually arg1.
THIS was my problem! Thank you so much!

Thanks to everyone else who posted to help me out. Acapela, you did show the right way, my problem was the third argument thing lol!
  Reply With Quote
12-05-10, 08:03 PM   #8
acapela
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 241
i like p3lim's final/refactored solution. demux the event with a table lookup... and it is probably more kosher to stick to varargs as long as possible, but once you finally know what you are doing you can skip the "select" (i.e. the best of both worlds).
__________________
Retired author/maintainer of Aloft (the nameplate addon)
http://www.wowinterface.com/download...AloftBeta.html
-----
Zippy said it best: "All life is a BLUR of Republicans and Meat!"
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Event arguments


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off