Thread Tools Display Modes
09-09-10, 09:48 AM   #1
bennasi
A Kobold Labourer
Join Date: Sep 2010
Posts: 1
Need help with EventHandling

I am new to addon developpment and am trying to make an addon to play some sounds on some events. I coded this piece of code but only the crit heal and lifesaver work when I delete the rest. Does anyone see mistake(s) I made and help me out?

thnx a lot.

Code:
local frame = CreateFrame("FRAME", "FooAddonFrame");
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
frame:RegisterEvent("PLAYER_ENTERING_WORLD");
frame:RegisterEvent("PLAYER_ENTERING_BATTLEGROUND");
local guid = UnitGUID("player");
local waar = false;
local function eventHandler(self, event, ...)
if (event == "COMBAT_LOG_EVENT_UNFILTERED") then
 local lol = select(2, ...)
 local guidlol = select(3, ...)
 if guid == guidlol then
 if lol == "SPELL_HEAL" then
     local lol2 = select(15, ...)
     if lol2 == 1 then
     PlaySoundFile("Interface\\AddOns\\WillemSound\\megaheal.wav")
     end
     if waar == true then
      PlaySoundFile("Interface\\AddOns\\WillemSound\\lifesaver.wav");
      waar = false;
     end
 end
 if lol == "SPELL_CAST_START" then
 local leven = UnitHealth("mouseover");
 local maxleven = UnitHealthMax("mouseover");
 local ondervijf = ((maxleven / 100) * 20);
 if leven < ondervijf and leven ~= 0 then
 waar = true;
 end
 end
end
end

if (event == "PLAYER_ENTERING_WORLD") then
PlaySoundFile("Interface\\AddOns\\WillemSound\\outofgum.wav")
end

if (event == "PLAYER_ENTERING_BATTLEGROUND") then
PlaySoundFile("Interface\\AddOns\\WillemSound\\medieval.wav")
end

end
frame:SetScript("OnEvent", eventHandler);
  Reply With Quote
09-10-10, 03:32 AM   #2
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
If you're not getting Lua errors, the natural assumption is that the code you want isn't being fired.

You might want to try a less generic event handler.
Code:
local frame = CreateFrame('Frame', 'FrameName', UIParent)
frame:SetScript('OnEvent', function (self,event,...)
local f = self[event] if f then
f(self,...)
end)
-- Creating a frame returns a table, so you can add stuff onto it as you please. This event handler calls frame.EVENTNAME with a reference to the frame (self) and the events args (...) when an event occurs. Lots easier to work with than a whole bunch of if/then statements, if you ask me. -- colon = implied 'self' function frame:COMBAT_LOG_UNFILTERED(timestamp, event, srcguid,srcname,srcflags, destguid,destname,destflags, spellid,spellname) -- stuff end -- alternative syntax #1 - does the exact same thing function frame.PLAYER_ENTERING_WORLD(self) -- more stuff end -- alternative syntax #2 - again, same frame.PLAYER_ENTERING_BATTLEGROUND = function (self) -- yet more stuff end -- remember to register your events.
When all else fails, put a print('Hey, it works!') where the code seems not to work. If the message occurs ingame, you know it's at least trying to do something.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 09-10-10 at 01:58 PM. Reason: Posting at early morning hours is bad, mkay?
  Reply With Quote
09-15-10, 05:30 PM   #3
Zwacky
A Defias Bandit
 
Zwacky's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2010
Posts: 2
your local function eventHandler(...) function is just a function and isn't connected with the frame, that has all the events.

Taroven's approach is right.
just do
Code:
frame:SetScript('OnEvent', function (self,event,...)
instead of
Code:
local function eventHandler(self, event, ...)
and you should be fine

(of course closing the frame:SetScript thing with a end) )
  Reply With Quote
09-15-10, 05:38 PM   #4
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Zwacky View Post
your local function eventHandler(...) function is just a function and isn't connected with the frame, that has all the events.

Taroven's approach is right.
just do
Code:
frame:SetScript('OnEvent', function (self,event,...)
instead of
Code:
local function eventHandler(self, event, ...)
and you should be fine

(of course closing the frame:SetScript thing with a end) )
Actually his last line of code sets the event handler correctly.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help with EventHandling


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