最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

world of warcraft - Why is this Lua code sending increasingly more messages per execution of the code? - Stack Overflow

programmeradmin0浏览0评论

This is for the game World of Warcraft Classic. I want to send a message to the chat on successful spell cast from my pet. This reads the combat log for success spell cast, gets my pet's name, the target of the spell, and sends a message. However, each successful running of the code, an additional message will be sent. For example, first time one message sends, second time two messages send, and so forth. Here is the code below:

_G.petTarget = "their target" -- Default value for pet's target

-- Create a frame to listen for combat log events
local frame = CreateFrame("Frame")
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
frame:SetScript("OnEvent", function()
        local _, subEvent, _, sourceGUID, _, _, _, _, destName = CombatLogGetCurrentEventInfo()
    
        -- If the event is a successful spell cast by your pet, store the target's name
        if subEvent == "SPELL_CAST_SUCCESS" and sourceGUID == UnitGUID("pet") then
            if destName then -- Prevent overwriting with nil
                _G.petTarget = destName
            end
        
            -- Delay calling the emote slightly to ensure the pet’s target updates in-game
            C_Timer.After(0.1, PetCommandEmote)
        end
end)

function PetCommandEmote()
    local petName = UnitName("pet") or "my pet" -- Get pet's name
    local petTarget = _G.petTarget -- Fetch the most recent target

    local phrases = {
        "commands " .. petName .. " to devour magic from " .. petTarget .. ". Good puppy!",
        "orders " .. petName .. " to feast on the magic of " .. petTarget .. ". Well done!",
        "directs " .. petName .. " to absorb magical energy from " .. petTarget .. ". Good job!",
        "commands " .. petName .. " to siphon power from " .. petTarget .. ". Atta boy!"
    }

    SendChatMessage(phrases[math.random(#phrases)], "EMOTE") -- Sends the emote to chat
end

I am not too familiar with Lua coding so I was not able to pinpoint what the issue may be since nothing stopped the duplicate messages from sending. At this point the code works almost exactly how I want except the duplicate messages are too much of a hurdle for me to overcome.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论