Thread Tools Display Modes
02-08-20, 01:16 PM   #1
save-disk
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 9
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);
  Reply With Quote
02-08-20, 01:49 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,857
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);
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-08-20 at 03:26 PM.
  Reply With Quote
02-08-20, 01:52 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Fizzlemizz View Post
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.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
02-08-20, 03:00 PM   #4
save-disk
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 9
Ah, of course! I feel silly for not spotting that now in all my attempts to fix it! Thank you for your help
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Newbie LUA help with fontstrings

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off