WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Newbie LUA help with fontstrings (https://www.wowinterface.com/forums/showthread.php?t=57811)

save-disk 02-08-20 01:16 PM

Newbie LUA help with fontstrings
 
Hi there! I'm not a programmer and don't really know any languages off by heart but have a basic grasp of how coding works, so excuse any silly oversights on my part. I just couldn't find the answer to my question after a week of combing through the wow API online so I thought I'd ask. I'm making an RP addon for myself that displays, after the character has stopped moving, whether you had run or walk toggled on (as other games have this and it's useful when you're roleplaying. At least, it's useful for me!)

I've managed to make it all work, but my text is appearing piled on top of each other instead of changing each time.

I assumed this was because I was accidentally creating a new font string each time instead of editing the old, but I can't find a 'delete text' function anywhere. Is this a thing? Sorry again for my dumb questions and thanks for your help!

Here is my full code:
Code:

frame = CreateFrame("Button", "frame", UIParent)


frame:RegisterEvent("PLAYER_STOPPED_MOVING");
local function eventHandler(self, event, ...)


            frame:SetWidth(120)
            frame:SetHeight(32)
            frame:SetToplevel(1)
            frame:SetFrameStrata("HIGH")
            frame:SetMovable(true)
            frame:EnableMouse(true)
            frame:RegisterForDrag("LeftButton")
            frame:RegisterForClicks("RightButtonUp")
            frame:SetPoint("TOP", Minimap, "BOTTOM", -150, 20)

            frame.Text = frame:CreateFontString("text", "OVERLAY", "GameFontNormal")

       
            frame.Text:SetJustifyH("CENTER")
            frame.Text:SetPoint("CENTER", 0, 0)

            frame:SetBackdrop({
                bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
                edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
                edgeSize = 16,
                insets = {left = 4, right = 4, top = 4, bottom = 4},
            })
            frame:SetBackdropColor(0,0,0,0.6)
            frame:SetBackdropBorderColor(1,0.8,0,0.8) 

local speed = GetUnitSpeed("player")
    if speed <= 6 then
        frame.Text:SetText("Walking")
    elseif speed > 6 then
        frame.Text:SetText("Running")
    end
end
frame:SetScript("OnEvent", eventHandler);


Fizzlemizz 02-08-20 01:49 PM

You're creating a new fontstring on top of previous ones each time the OnEvent function is called. Just create one and update its text.

Code:

local frame = CreateFrame("Button", "save-disk_frame", UIParent)
frame:SetWidth(120)
frame:SetHeight(32)
frame:SetToplevel(1)
frame:SetFrameStrata("HIGH")
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:RegisterForClicks("RightButtonUp")
frame:SetPoint("TOP", Minimap, "BOTTOM", -150, 20)
frame.Text = frame:CreateFontString("text", "OVERLAY", "GameFontNormal")
frame.Text:SetJustifyH("CENTER")
frame.Text:SetPoint("CENTER", 0, 0)
frame:SetBackdrop({
    bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    edgeSize = 16,
    insets = {left = 4, right = 4, top = 4, bottom = 4},
})
frame:SetBackdropColor(0,0,0,0.6)
frame:SetBackdropBorderColor(1,0.8,0,0.8) 

frame:RegisterEvent("PLAYER_STOPPED_MOVING");
local function eventHandler(self, event, ...)
        local speed = GetUnitSpeed("player")
        if speed <= 6 then
                self.Text:SetText("Walking")
        else
                self.Text:SetText("Running")
        end
end
frame:SetScript("OnEvent", eventHandler);


Seerah 02-08-20 01:52 PM

Quote:

Originally Posted by Fizzlemizz (Post 335098)
You're creating a new fontstring on top of previous ones each time the OnEvent function is called. Just create one and update it's text.

And you were doing the same with the frame itself.

save-disk 02-08-20 03:00 PM

Ah, of course! I feel silly for not spotting that now in all my attempts to fix it! Thank you for your help :)


All times are GMT -6. The time now is 02:21 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI