ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Event Listener Based On Another Event Fire (https://www.esoui.com/forums/showthread.php?t=1828)

Specko 06/22/14 07:33 PM

Event Listener Based On Another Event Fire
 
Hello All,

Sorry if I missed this somewhere else, but here is my question. I have an event listeners for EVENT_COMBAT_EVENT and EVENT_ALLIANCE_POINT_UPDATE. What I'm wanting to do I have a popup only when there is an alliance point gain and the result of a combat event is a kill. I thought that I might be able to just set the label for the kill popup only when I get a killing blow. However, my function is triggering when someone releases or is rezzed. So the bug is getting a killing blow popup twice. Here is the relevant code. All help is very much appreciated.

Code:

  function KillingBlow.CheckForApGain( alliancePoints, playSound, difference)
        if difference == 0 then
                KBC.Label:SetText("")
        end
  end


  function KillingBlow.Combat(eventCode, result, isError, abilityName, abilityGraphic, abilityActionSlotType, sourceName, sourceType, targetName, targetType, hitValue, powerType, damageType, log)

        local playerName = GetUnitName("player") .. "^Mx"

        if hitValue ~= 0  and damageType ~= 1 then
                if sourceType == COMBAT_UNIT_TYPE_PLAYER then
                        if playerName == sourceName then
                                KBC.label:SetText(abilityName .. " Killing Blow!")
                                KillingBlow.CheckForApGain()
                        end
                end
        end

        if sourceType == COMBAT_UNIT_TYPE_PLAYER  then
                if playerName == sourceName then
                        if result == 2260 or result == 2265 then
                                KillingBlow:simpleAnimation(KBC.label)
                        end
                end
        else
                return
        end
  end

        em:RegisterForEvent(self.addonName, EVENT_COMBAT_EVENT, KillingBlow.Combat )
        em:RegisterForEvent(self.addonName, EVENT_ALLIANCE_POINT_UPDATE, KillingBlow.CheckForApGain)


Sasky 06/22/14 07:58 PM

KillingBlow.CheckForApGain() doesn't trigger the event -- none of the parameters are filled in.

You need to actually register it with an event handler to call.

Does the AP gain event always fire before or after the combat event? If so, you can do something like this to check when the last AP gain was:

Lua Code:
  1. local delta = 1 --If events happen within this time, call simultaneous
  2. local lastApGain = nil
  3.  
  4. function KillingBlow.CheckForApGain( alliancePoints, playSound, difference)
  5.     lastApGain = GetTimeStamp() --or a higher precision, maybe GetFrameTimeMilliseconds would work
  6. end
  7.  
  8.  
  9. function KillingBlow.Combat(eventCode, result, isError, abilityName, abilityGraphic, abilityActionSlotType, sourceName, sourceType, targetName, targetType, hitValue, powerType, damageType, log)
  10.      if ... then --Other filtering you were doing here for source damage, etc
  11.  
  12.           if lastApGain and GitDiffBetweenTimestamps(GetTimeStamp(), lastApGain) > delta then
  13.               --Create your popup frame
  14.           end
  15.  
  16.      end
  17. end
  18.  
  19. em:RegisterForEvent(self.addonName, EVENT_COMBAT_EVENT, KillingBlow.Combat )
  20. em:RegisterForEvent(self.addonName, EVENT_ALLIANCE_POINT_UPDATE, KillingBlow.CheckForApGain)


If it's the other way around (combat event always fires first), you can cache the data on a COMBAT event and create the popup on the AP_GAIN event.

If you need values from both, or the events can fire in either order, it's a little bit trickier, but the basic pattern would be:

Code:

--Pseudocode:

EVENT1:
    save parameters
    save timestamp
    call FIRE function

EVENT2:
    save parameters
    save timestamp
    call FIRE function

FIRE:
    if timestamps close enough then
        do whatever with saved parameters
    end


-----------------------------------------

Also, playerName == sourceName will fail for female characters and possibly in the FR or DE clients. Use one of the ZO format functions or a :gsub("%^..$","") to remove from the source name to check.

Specko 06/22/14 08:22 PM

Quote:

Originally Posted by Sasky (Post 9609)
KillingBlow.CheckForApGain() doesn't trigger the event -- none of the parameters are filled in.

You need to actually register it with an event handler to call.

Does the AP gain event always fire before or after the combat event? If so, you can do something like this to check when the last AP gain was:

Lua Code:
  1. local delta = 1 --If events happen within this time, call simultaneous
  2. local lastApGain = nil
  3.  
  4. function KillingBlow.CheckForApGain( alliancePoints, playSound, difference)
  5.     lastApGain = GetTimeStamp() --or a higher precision, maybe GetFrameTimeMilliseconds would work
  6. end
  7.  
  8.  
  9. function KillingBlow.Combat(eventCode, result, isError, abilityName, abilityGraphic, abilityActionSlotType, sourceName, sourceType, targetName, targetType, hitValue, powerType, damageType, log)
  10.      if ... then --Other filtering you were doing here for source damage, etc
  11.  
  12.           if lastApGain and GitDiffBetweenTimestamps(GetTimeStamp(), lastApGain) > delta then
  13.               --Create your popup frame
  14.           end
  15.  
  16.      end
  17. end
  18.  
  19. em:RegisterForEvent(self.addonName, EVENT_COMBAT_EVENT, KillingBlow.Combat )
  20. em:RegisterForEvent(self.addonName, EVENT_ALLIANCE_POINT_UPDATE, KillingBlow.CheckForApGain)


If it's the other way around (combat event always fires first), you can cache the data on a COMBAT event and create the popup on the AP_GAIN event.

If you need values from both, or the events can fire in either order, it's a little bit trickier, but the basic pattern would be:

Code:

--Pseudocode:

EVENT1:
    save parameters
    save timestamp
    call FIRE function

EVENT2:
    save parameters
    save timestamp
    call FIRE function

FIRE:
    if timestamps close enough then
        do whatever with saved parameters
    end


-----------------------------------------

Also, playerName == sourceName will fail for female characters and possibly in the FR or DE clients. Use one of the ZO format functions or a :gsub("%^..$","") to remove from the source name to check.

Thanks for the reply. A bit of the code is still in pseudocode stages. About the playerName didn't know that about female characters.

Sasky 06/23/14 12:00 AM

Oh missed it, but you'd also want to set lastApGain back to NIL after you use it.

Or to change pseudocode:
Code:

FIRE:
    if timestamps close enough then
        do whatever with saved parameters
        reset timestamps to nil or 0
    end



All times are GMT -6. The time now is 07:45 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI