View Single Post
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