View Single Post
06-04-16, 04:10 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
RegisterCallback

I just wrote myself a function that triggers a callback on any given event. On top of that you have access to two sets of arguments. One which you define on init. One which is passed along on event.

Benefit is that you only need one event frame for any future event callback.

Lua Code:
  1. --rLib:RegisterCallback
  2. function rLib:RegisterCallback(event, callback, ...)
  3.   if not self.eventFrame then
  4.     self.eventFrame = CreateFrame("Frame")
  5.     function self.eventFrame:OnEvent(event, ...)
  6.       for callback, args in next, self.callbacks[event] do
  7.         callback(args, ...)
  8.       end
  9.     end
  10.     self.eventFrame:SetScript("OnEvent", self.eventFrame.OnEvent)
  11.   end
  12.   if not self.eventFrame.callbacks then self.eventFrame.callbacks = {} end
  13.   if not self.eventFrame.callbacks[event] then self.eventFrame.callbacks[event] = {} end
  14.   self.eventFrame.callbacks[event][callback] = {...}
  15.   self.eventFrame:RegisterEvent(event)
  16. end
  17.  
  18. --init
  19. local function OnLogin(...)
  20.   print(...)
  21. end
  22. rLib:RegisterCallback("PLAYER_LOGIN",OnLogin,"hello1","hello2")

Arguments you pass along on init are the first argument in the triggered callback packed as a table.

Any thoughts?
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)