How can I make my code execute something exactly in the moment the player attacks something?
I tried the COMBAT_LOG_EVENT_UNFILTERED event, but this appears to happen a noticeabe time before the in-game model actually swings at the target and the damage is displayed in-game.
Lua Code:
playerGuid = UnitGUID("player")
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
eventFrame:SetScript("OnEvent", function(_, ...)
timestamp, subevent, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags = CombatLogGetCurrentEventInfo()
if sourceGUID == playerGuid then
local amount, critical
if subevent == "SWING_DAMAGE" then
amount, _, _, _, _, _, critical = select(12, CombatLogGetCurrentEventInfo())
elseif subevent == "SPELL_DAMAGE" then
_, _, _, amount, _, _, _, _, _, critical = select(12, CombatLogGetCurrentEventInfo())
else
return
end
-- This comes noticeably earlier than the in-game action takes place.
-- But I want it to happen exactly at the same time as the in-game action.
print(amount, critical)
end
end)