Thread Tools Display Modes
10/07/20, 05:26 AM   #1
nightdreaming
Join Date: Feb 2018
Posts: 8
AddFilterForEvent causing additional callbacks

Here's a piece of understanding that you guys probably already know but I thought I would put it up for those like me who didn't realise, and a question:

How do I filter to only get 1 event when any of source==player, target==player or both==player?

Here's the detail:

I'm trying to reduce the load my AddOn causes by reducing the number of callbacks made to my event handler. AddFilterForEvent does the job as the C code filters the events for me and only sends me the events I am interested in. All well and good.

The problem I was getting was that I got the same event sent multiple times because of my filters, e.g. if I wanted an event callback to occur if the player was the SOURCE combat unit type OR the player was the TARGET combat unit type then I was registering two filters:

Lua Code:
  1. -- SOURCE_COMBAT_UNIT_TYPE==Player filter
  2. EVENT_MANAGER:RegisterForEvent ("myCombatEvent1", EVENT_COMBAT_EVENT, myCombatEventHandler)
  3. EVENT_MANAGER:AddFilterForEvent ("myCombatEvent1", EVENT_COMBAT_EVENT, REGISTER_FILTER_SOURCE_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER)
  4.  
  5. -- TARGET_COMBAT_UNIT_TYPE==Player filter
  6. EVENT_MANAGER:RegisterForEvent ("myCombatEvent2", EVENT_COMBAT_EVENT, myCombatEventHandler)
  7. EVENT_MANAGER:AddFilterForEvent ("myCombatEvent2", EVENT_COMBAT_EVENT, REGISTER_FILTER_TARGET_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER)

This worked, but there are many combat events where both the SOURCE and the TARGET combat unit type is the player in the event. For these events I was getting two callbacks, one for each case or filter.

I then discovered that if the same filters are applied in one AddFilterForEvent call I only get one event, e.g. for the above case:

Lua Code:
  1. EVENT_MANAGER:RegisterForEvent ("myCombatEvent1", EVENT_COMBAT_EVENT, myCombatEventHandler)
  2.  
  3. EVENT_MANAGER:AddFilterForEvent ("myCombatEvent1", EVENT_COMBAT_EVENT,
  4.                 REGISTER_FILTER_SOURCE_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER,
  5.                 REGISTER_FILTER_TARGET_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER)

This only results in one event rather than two. But this will only trigger an event if BOTH source and target are the Player. It will not trigger an event when only one of source or target is the Player.

So how do I filter to only get one event when any of source, target or both are the player?

Last edited by nightdreaming : 10/07/20 at 07:15 AM.
  Reply With Quote
10/07/20, 07:13 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Does anyone know how to solve this?
I doubt it's possible within 1 event and filters.

Either register an event + filter on REGISTER_FILTER_TARGET_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER or REGISTER_FILTER_SOURCE_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER.
And within your event's callback function then check the other case by using the callback function parameters.
e.g. if you got the filter on REGISTER_FILTER_TARGET_COMBAT_UNIT_TYPE, COMBAT_UNIT_TYPE_PLAYER -> check if parameter sourceUnitType
== "player" or different.

Depending on the amount of events fired you would need to check which combination would be the best to prefilter at C code, and the rest of the incoming events need to be filtered in your lua code then.
  Reply With Quote
10/07/20, 07:41 AM   #3
nightdreaming
Join Date: Feb 2018
Posts: 8
Thanks Baertram for the reply,

The problem with that solution is that it misses a case out.

If I register for source==player events then I will get those and can check if target==player at the same time. But that means for this example if only target==player I will get no events.

I doubt there is a solution but I thought I would ask.
  Reply With Quote
10/07/20, 09:23 AM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
You need to register 2 events with the appropriate filters then and in your callback functions handle both cases.
e.g. do some pre-checks in your event callback function and then call the same "further stuff" function from it.

Code:
event_combat_event, filter source == player, callback = callback1
event_combat_event, filter target == player, callback = callback2

function callbackEventCombat(caseType, a, b, c, ...)
 if caseType == 1 then
 elseif caseType == 2 then
 end
end

function callback1(a, b, c, source, target, ...)
 if target ~= "player" then return end
 callbackEventCombat(1, a, b, c, source, target, ...)
end

function callback2(a, b, c, source, target, ...)
 if source~= "player" then return end
 callbackEventCombat(2, a, b, c, source, target, ...)
end
Something like that perhaps.
  Reply With Quote
10/08/20, 07:10 AM   #5
nightdreaming
Join Date: Feb 2018
Posts: 8
Thanks and I will have a play with it.


If I get anywhere useful I'll post an update
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » AddFilterForEvent causing additional callbacks

Thread Tools
Display Modes

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