• Dear Guest,

    You're browsing our forum as a Guest meaning you can only see a portion of the forum in read-only mode.
    To view all forum nodes and be able to create threads/posts please register or log-in with your existing account.

    TwinStar team

Small useful LUA functions

Psychi

Authorized
Joined
May 24, 2010
Hi, I really enjoy coding in LUA and since I'm beginner (started two days ago) I'm writing some easy short scripts for usefull in-game stuff.


My first work is this piece of code which allows you track proc of spells you choose. (Dagtrinactt request)

2dcbd34f27


You can put this to LUA code of any addon or use SuperMacro (recommended).
Code:
if not procCheck then
     procCheck = CreateFrame("Frame")
end

start = 0
procs = {}

function TrackProc(...)
    if start == 0 then 
        spells = arg
        for k,v in spells do 
            if k ~= "n" then
                procs[k] = 0
            end
        end
        start = 1 
        timeStart = GetTime()
        procCheck:RegisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE")
        DEFAULT_CHAT_FRAME:AddMessage("Proc log STARTED")
    else
        procCheck:UnregisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE")
        start = 0
        timeEnd = GetTime()
        timeShit = timeEnd - timeStart
        timeShit = math.ceil(timeShit-0.5)
        DEFAULT_CHAT_FRAME:AddMessage("Proc log ENDED after "..timeShit.." seconds.")
        for k,v in spells do 
            if k ~= "n" then
                DEFAULT_CHAT_FRAME:AddMessage(v.." proc'd: "..procs[k].." times.")
        end
    end end
end


procCheck:SetScript("OnEvent", function ()
    if arg1 ~= nil then 
        for k,v in spells do 
            if k ~= "n" then
                if string.find(arg1, v) then
                       procs[k] = procs[k] + 1
                end
            end
        end
    end
end)

You just call function in example below in macro and add name of spells as arguments in same format.
Code:
/script TrackProc("Hand of Justice","Seal of Command","Crusader's Wrath","Fatal Wound");

(Calling script twice will end tracking)

___________________________________________

If you are looking for something, you can post your request here and I can try to create something just for you!
 
Last edited:
Got new one. This one allows you filter error messages you don't like. For me it's "Spell is not ready yet." message that is so much annoying.

Code:
UIErrorsFrame:Show();
blacklist = {"Spell is not ready yet.","Nothing to dispel", "Can't do that while moving", "Another action is in progress"}
r = 1.0; g = 0.0; b = 0.0

UIErrorsFrame:SetScript("OnEvent", function()
    block = false
    for k,v in blacklist do
        if blacklist[k] == arg1 then 
           block = true
        end
    end
    if block then
        -- SendChatMessage(v.." - Message blocked")  
        return 
    else     
        UIErrorsFrame:AddMessage(arg1, r,g,b, nil) 
    end
end)

1. Add messages to blacklist = {"Spell is not ready yet.", "Message1", "Message2"} separated by comma. (Be careful with dot in the end, if it doesn't work then try it both with and without dot but most messages are without dot in the end)

2. You can also edit colour of unfiltered messages by editing r = 0.0;g = 0.0;b = 1.0; values. I like blue most. :) (Optional)


Gonna create some addon soon but for those who are trying to get rid of things like that this can be easy option without downloading of any addon except SuperMacro.
 
Last edited:
Created function that will report to chat (raid>party>say) who is being ressed by you. (Works for both target and mouseover)

Code:
if not ressAnnounce then
     ressAnnounce = CreateFrame("Frame")
end

spells = {"Resurrection","Rebirth","Redemption","Ancestral Spirit"}
ressAnnounce:RegisterEvent("SPELLCAST_INTERRUPTED")
ressAnnounce:RegisterEvent("SPELLCAST_START")
ressAnnounce:SetScript("OnEvent",function()
if event == "SPELLCAST_START" then
    spell = arg1
    ress = false
    for v,k in spells do
        if spell == k then 
            ress = true
        end
    end


    if ress then
        if GetNumRaidMembers() > 0 then
            channel = "RAID"
        else if GetNumPartyMembers() > 0 then
            channel = "PARTY"
        else
            channel = "SAY"
        end end
        
        if UnitIsFriend("player","target") and UnitIsDead("target") then
            nameOfTarget = UnitName("target")
        else if UnitIsFriend("player","mouseover") and UnitIsDead("mouseover")  then
            nameOfTarget = UnitName("mouseover")
        else if GameTooltipTextLeft1:IsVisible() then
            local _, _, name = string.find(GameTooltipTextLeft1:GetText(), "^Corpse of (.+)$")
            if ( name ) then
                nameOfTarget = name
            end
        else
            nameOfTarget = UnitName("player")
        end end end


        castTime = arg2/1000
        startTime = GetTime()
        SendChatMessage("Casting "..spell.." on "..nameOfTarget..". (Duration: "..castTime.." sec)",channel)
    end
else
    if ress then
        endTime = GetTime()
        remaining = castTime - (endTime - startTime)
        remaining = math.ceil(remaining-0.5)
        SendChatMessage("Casting of "..spell.." on "..nameOfTarget.." was interrupted. (Remained: "..remaining.." sec)",channel)   
    end
end
end)

1. Just put it somewhere to SuperMacro or to any LUA file in Addons folder.
 
Last edited:
Some useful function for easier addon developing.

Code:
print = function(...)
     local s="";
    for x=1,table.getn(arg) do
        s=s.. ( type(arg[x]) == "string" and arg[x] or type(arg[x]) == "number" and arg[x] or type(arg[x]) ) .. "  ";
    end
    DEFAULT_CHAT_FRAME:AddMessage(s)
end

Code:
function FrameStack(x)
if MyGameTooltip:IsShown() then MyGameTooltip:Hide() else MyGameTooltip:Show() end
if x then AllFrames = 1 else AllFrames = nil end
end



MyGameTooltip = CreateFrame("Frame")
MyGameTooltip:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", 
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 32, edgeSize = 16,
    insets = { left = 5, right = 5, top = 5, bottom = 5 }});
MyGameTooltip:SetPoint("TOPLEFT",UIParent,"TOPLEFT", 10, -10)
MyGameTooltip:SetWidth(225)
MyGameTooltip:SetHeight(160)
MyGameTooltip:SetFrameLevel(100)
MyGameTooltip:Hide()
MyGameTooltip:EnableMouse(1)
MyGameTooltip:SetMovable(1)
MyGameTooltip:RegisterForDrag("LeftButton", "RightButton")
MyGameTooltip:SetScript("OnMouseUp", function() MyGameTooltip:Hide() end)
MyGameTooltip:SetScript("OnDragStart", function() MyGameTooltip:StartMoving() end)
MyGameTooltip:SetScript("OnDragStop", function() MyGameTooltip:StopMovingOrSizing() end)
fntString = MyGameTooltip:CreateFontString("fntString")
fntString:SetFontObject("GameFontNormal")
fntString:SetAllPoints(MyGameTooltip)
fntString:SetText("")
MyGameTooltip:SetScript("OnUpdate",function(self)
    local text = ""
    local frame = EnumerateFrames()
    while frame do
        if (frame:IsVisible() or AllFrames) and MouseIsOver(frame) then
            if frame:GetName() then text=text.."<"..frame:GetFrameLevel().."> ".. frame:GetName().."\n"; end
        end
        frame = EnumerateFrames(frame)
    end
    fntString:SetText(text)
    this:SetWidth(fntString:GetWidth()+35)
    this:SetHeight(fntString:GetHeight()+35)
end)


SLASH_FRAMESTACK1,SLASH_FRAMESTACK2="/framestack","/fstack"
SlashCmdList["FRAMESTACK"] =  function(x) if tonumber(x) then FrameStack(1) else FrameStack() end end;
 
In last days I get quite a lot annoying whispers by gold sellers, so I created function that blocks all chinese letters + blocks all whispers under X level (defined by variable local ignoreUntilLvl = 5)

1. Download SuperMacro and move it to Addons folder (Ace addons are optional)
2. Create Macro in SuperMacro
3. Copy this code to Extended LUA code
4. ???
5. Profit

Code:
local ignoreUntilLvl = 5
local backup  = ChatFrame_OnEvent;
local hooked = false
local hookedhook = false
local found = false
remove = 0
function ChatFilter(event)
    if event == "CHAT_MSG_SYSTEM" then
        if string.find(arg1,"friend") ~=nil and hooked or hookedhook then
            hookedhook = false
            if string.find(arg1,"already") ~=nil or string.find(arg1,"yourself") ~=nil then
                -- DEFAULT_CHAT_FRAME:AddMessage("BACKUP EVENT"..prev)
                arg1 = prevArg1
                arg2 = prevArg2
                arg3 = prevArg3
                backup(prev)
                hooked = false
            else if string.find(arg1,"added to friend") ~= nil and hooked then
                local nameb
                for nameb in string.gfind(arg1, "[^%s]+") do
                    for i=1,GetNumFriends() do
                        local name, level, class, loc, connected, status = GetFriendInfo(i);
                        if level == 0 then
                            break
                        end
                        -- DEFAULT_CHAT_FRAME:AddMessage("LEVEL: "..name.." "..level)
                        if nameb == name then
                            -- DEFAULT_CHAT_FRAME:AddMessage("LEVEL: "..name.." "..level)
                            if level >= ignoreUntilLvl then        
                                -- DEFAULT_CHAT_FRAME:AddMessage("BACKUP EVENT"..prev)
                                arg1 = prevArg1
                                arg2 = prevArg2
                                arg3 = prevArg3
                                backup(prev)
                            end
                            break
                        end
                    end
                    RemoveFriend(nameb)
                    hooked = false
                    hookedhook = true
                    break
                end        
            end end
            return
        end
    end
    if event == "CHAT_MSG_WHISPER" then
        for i=1,GetNumFriends() do
            local name, level, class, loc, connected, status = GetFriendInfo(i);
            if arg2 == name then
                backup(event)
                return
            end
        end
        -- DEFAULT_CHAT_FRAME:AddMessage(event.." "..arg1.." "..arg2)
        if string.find(arg1,"[\228-\233]") ~=nil then
            -- DEFAULT_CHAT_FRAME:AddMessage("found")
            return
        else 
            AddFriend(arg2) 
            prev = event
            prevArg1 = arg1
            prevArg2 = arg2
            prevArg3 = arg3
            setglobal(prev)
            setglobal(prevArg2)
            setglobal(prevArg3)
            hooked = true


        end
    else
        backup(event)
    end
end
ChatFrame_OnEvent = ChatFilter

Scroll up to find how to use these functions. There is also download link for SuperMacro.

Works with Prat without any problems so I guess it could work with other chat addons too.
It is quite complicated function so there can be some bugs but I tried my best and didn't find any. Feel free to report them to PM or here.

+1 appreciated :)
 
Last edited:
Now write exactly how to use it. Point after point. Now is totally useless for 90% of population here.
 
Now write exactly how to use it. Point after point. Now is totally useless for 90% of population here.
1. Download SuperMacro and move it to Addons folder (Ace addons are optional)
2. Create Macro in SuperMacro
3. Copy this code to Extended LUA code
4. ???
5. Profit

How to use it:
Players in friend list can't be blocked. Level doesn't matter.
You must have at least one free slot in friend list to make this function work.
 
Last edited:
Like that?


fd9bba6b8e.jpg


Because if I do it like that after the pushing the macro button there is red thing on the screen 'dont have target' or something like that. And my alt on lvl 1 still can whisp me.
 
I found huge bug :D fixed and tested with my guild. So copy paste new one and press Save and type /reload and it should work. You don't have to click anything. Just have it there.
 
In last days I get quite a lot annoying whispers by gold sellers, so I created function that blocks all chinese letters + blocks all whispers under X level (defined by variable local ignoreUntilLvl = 5)

1. Download SuperMacro and move it to Addons folder (Ace addons are optional)
2. Create Macro in SuperMacro
3. Copy this code to Extended LUA code
4. ???
5. Profit

Code:
local ignoreUntilLvl = 5
local backup  = ChatFrame_OnEvent;
local hooked = false
local hookedhook = false
local found = false
remove = 0
function ChatFilter(event)
    if event == "CHAT_MSG_SYSTEM" then
        if string.find(arg1,"friend") ~=nil and hooked or hookedhook then
            hookedhook = false
            if string.find(arg1,"already") ~=nil or string.find(arg1,"yourself") ~=nil then
                -- DEFAULT_CHAT_FRAME:AddMessage("BACKUP EVENT"..prev)
                arg1 = prevArg1
                arg2 = prevArg2
                arg3 = prevArg3
                backup(prev)
                hooked = false
            else if string.find(arg1,"added to friend") ~= nil and hooked then
                local nameb
                for nameb in string.gfind(arg1, "[^%s]+") do
                    for i=1,GetNumFriends() do
                        local name, level, class, loc, connected, status = GetFriendInfo(i);
                        if level == 0 then
                            break
                        end
                        -- DEFAULT_CHAT_FRAME:AddMessage("LEVEL: "..name.." "..level)
                        if nameb == name then
                            -- DEFAULT_CHAT_FRAME:AddMessage("LEVEL: "..name.." "..level)
                            if level >= ignoreUntilLvl then        
                                -- DEFAULT_CHAT_FRAME:AddMessage("BACKUP EVENT"..prev)
                                arg1 = prevArg1
                                arg2 = prevArg2
                                arg3 = prevArg3
                                backup(prev)
                            end
                            break
                        end
                    end
                    RemoveFriend(nameb)
                    hooked = false
                    hookedhook = true
                    break
                end        
            end end
            return
        end
    end
    if event == "CHAT_MSG_WHISPER" then
        for i=1,GetNumFriends() do
            local name, level, class, loc, connected, status = GetFriendInfo(i);
            if arg2 == name then
                backup(event)
                return
            end
        end
        -- DEFAULT_CHAT_FRAME:AddMessage(event.." "..arg1.." "..arg2)
        if string.find(arg1,"[\228-\233]") ~=nil then
            -- DEFAULT_CHAT_FRAME:AddMessage("found")
            return
        else 
            AddFriend(arg2) 
            prev = event
            prevArg1 = arg1
            prevArg2 = arg2
            prevArg3 = arg3
            setglobal(prev)
            setglobal(prevArg2)
            setglobal(prevArg3)
            hooked = true


        end
    else
        backup(event)
    end
end
ChatFrame_OnEvent = ChatFilter

Scroll up to find how to use these functions. There is also download link for SuperMacro.

Works with Prat without any problems so I guess it could work with other chat addons too.
It is quite complicated function so there can be some bugs but I tried my best and didn't find any. Feel free to report them to PM or here.

+1 appreciated :)

BIG KUDOS MAN! Great thread overall and this function in particular is super useful, thank you so much for sharing and good luck with your LUA coding :smile:

*** MAKE IT STICKY! ***

Off-topic!

Too bad I can't restart WoW right now to install SuperMacro
asd.gif

LOL REKT! You better get to 60 for no-queue! :wink:
 
Top Bottom