Thread Tools Display Modes
10-24-13, 09:57 AM   #1
gamerfreak
A Murloc Raider
Join Date: Oct 2013
Posts: 4
Issue with COMBAT_LOG_EVENT sub-event.

Hey there. First time addon programmer here, and I'm having an issue pulling the sub-event argument from COMBAT_LOG_EVENT_UNFILTERED.

Here is my code:

Code:
function events:COMBAT_LOG_EVENT_UNFILTERED(self, event, ...)
    local eventType = select(2,...);
    local destGUID = select(7,...);
    print (eventType); -- for debugging
    if (eventType == "UNIT_DIED") then
        print("UNIT_DIED");
    end
end


-----------------
--EVENT HANDLER--
-----------------
frame:SetScript("OnEvent", function(self, event, ...)
events[event](self, event, ...);
end);
for k, v in pairs(events) do
    frame:RegisterEvent(k);
end
eventType is coming back with a true/false value, not the sub-event string.

Any ideas what I'm doing wrong? Thanks.
  Reply With Quote
10-24-13, 10:32 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
I can't get in-game now but try print(tostringall(...)) to see what's coming in your vararg.
  Reply With Quote
10-24-13, 11:37 AM   #3
gamerfreak
A Murloc Raider
Join Date: Oct 2013
Posts: 4
Originally Posted by Dridzt View Post
I can't get in-game now but try print(tostringall(...)) to see what's coming in your vararg.
SPELL_CAST_SUCCESS false 0x07000000054E9410 Gamerfreak 1297 0 0x07000000054E9410 Gamerfreak 1297 0 1126 Mark of the Wild 8
  Reply With Quote
10-24-13, 11:42 AM   #4
gamerfreak
A Murloc Raider
Join Date: Oct 2013
Posts: 4
Originally Posted by gamerfreak View Post
SPELL_CAST_SUCCESS false 0x07000000054E9410 Gamerfreak 1297 0 0x07000000054E9410 Gamerfreak 1297 0 1126 Mark of the Wild 8
Changed local eventType = select(2,...); to local eventType = select(1,...); and its working! Thanks. All the examples I saw had the sub-event string as the second arg.
  Reply With Quote
10-24-13, 03:12 PM   #5
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
It may be working, but it's not correct. The first argument for COMBAT_LOG_EVENT is the timestamp, and since you have the sub-event first it means your arguments are all off by one.

This is because "self" is implied when you use a colon to define a function. You could change it to events:COMBAT_LOG_EVENT_UNFILTERED(event, ...) but the entire point of writing an event handler like this is so you can define your variables directly in the function arguments.

I would personally remove the event name from the arguments since it's already the name of the function itself and I can't think of any reason you would need it. Something like this..

Lua Code:
  1. function events:COMBAT_LOG_EVENT_UNFILTERED(timestamp, event, _, srcGUID, srcName, srcFlags, srcRaidFlags, dstGUID, dstName, dstFlags, dstRaidFlags, ...)
  2.     print(event) -- for debugging
  3.     if (event == "UNIT_DIED") then
  4.         print("UNIT_DIED")
  5.     end
  6. end
  7.  
  8. -----------------
  9. --EVENT HANDLER--
  10. -----------------
  11. frame:SetScript("OnEvent", function(self, event, ...)
  12.     events[event](self, ...)
  13. end)
  14.  
  15. for k, v in pairs(events) do
  16.     frame:RegisterEvent(k)
  17. end

Also semi-colons are essentially just for looks, so unless you really like them the only time I would bother is to prevent ambiguity.

One more thing, NEVER do var = select(1, ...), it's literally the same thing as var = ... except with the overhead of a function call.
  Reply With Quote
10-24-13, 06:57 PM   #6
gamerfreak
A Murloc Raider
Join Date: Oct 2013
Posts: 4
Originally Posted by semlar View Post
It may be working, but it's not correct. The first argument for COMBAT_LOG_EVENT is the timestamp, and since you have the sub-event first it means your arguments are all off by one.

This is because "self" is implied when you use a colon to define a function. You could change it to events:COMBAT_LOG_EVENT_UNFILTERED(event, ...) but the entire point of writing an event handler like this is so you can define your variables directly in the function arguments.

I would personally remove the event name from the arguments since it's already the name of the function itself and I can't think of any reason you would need it. Something like this..
Thanks, super helpful post. Like I said I'm new to addons/lua, so these kind of mistakes are inevitable. Great explanation.

Originally Posted by semlar View Post
Also semi-colons are essentially just for looks, so unless you really like them the only time I would bother is to prevent ambiguity.
I'm coming from a C++/Java background, so they're really just habit.
  Reply With Quote
10-25-13, 10:15 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Also, do not do this:

Code:
function events:COMBAT_LOG_EVENT_UNFILTERED(self, event, ...)
    local eventType = select(2,...);
    local destGUID = select(7,...);
Calling functions is really, really slow (relatively speaking) and should be avoided whenever possible. In this case, you're doing two calls to select, which is almost always a completely useless function, this being no exception. Instead, assign all the variables (or at least the 1st-nth, where n is the highest number you care about) and just ignore the ones you're not using. A common convention in Lua is to use an underscore as the name of any variable you don't care about:

Code:
function events:COMBAT_LOG_EVENT_UNFILTERED(self, event, ...)
    local _, eventType, _, _, _, _, destGUID = ...
Alternatively, you can just name the arguments in the function definition itself:

Code:
function events:COMBAT_LOG_EVENT_UNFILTERED(self, event, _, eventType, _, _, _, _, destGUID, ...)
^ Here I've left the vararg at the end to contain all the unnamed variables, but if you know your code will never use anything past destGUID you can omit it.

Finally, a common practice for handling CLEU is to name all the variables that are the same for all sub-events, and use the vararg only for the event-specific variables:

Code:
function events:COMBAT_LOG_EVENT_UNFILTERED(self, event, timestamp, eventType, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, ...)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Issue with COMBAT_LOG_EVENT sub-event.


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