Search code examples
luaadd-onworld-of-warcraft

Get damage value and school of magic incoming damage (WoW 1.13)


How do I get damage value and school of incoming damage magic using api World Of WarCraft 1.13 in lua language? Damage caused to me by another player or mob. This is necessary so that I can use

print("You received " .. damageValue .. " " .. damageSchool .. " damage")

So that I can get in the chat:

You received 100 Fire damage

You received 50 Physical damage

etc.


Solution

  • Classic combat log should be almost the same as retail
    See https://wow.gamepedia.com/COMBAT_LOG_EVENT

    local playerGUID = UnitGUID("player")
    local MSG_PLAYER_DAMAGE = "You received %d %s damage"
    
    local damageEvents = {
        SWING_DAMAGE = true,
        SPELL_DAMAGE = true,
    }
    
    local f = CreateFrame("Frame")
    f:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
    f:SetScript("OnEvent", function(self, event)
        self:OnEvent(event, CombatLogGetCurrentEventInfo())
    end)
    
    function f:OnEvent(event, ...)
        local timestamp, subevent, _, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags = ...
        local spellId, spellName, spellSchool
        local amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing, isOffHand
    
        if subevent == "SWING_DAMAGE" then
            amount = select(12, ...)
        elseif subevent == "SPELL_DAMAGE" then
            spellId, spellName, spellSchool, amount = select(12, ...)
        end
    
        if damageEvents[subevent] and destGUID == playerGUID then
            print(MSG_PLAYER_DAMAGE:format(amount, GetSchoolString(spellSchool or 0x1)))
        end
    end