WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Change Button Text on Click Event (https://www.wowinterface.com/forums/showthread.php?t=59059)

Firesong 03-03-22 07:35 AM

Change Button Text on Click Event
 
Hi all,

I have a button that I want to say "Start" when loaded and then say "Stop" when clicked.

Here is the lua creating the Fontstring and event:
BtnStopGo:SetScript("OnClick", StopStart)
local fs = BtnStopGo:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
fs:SetText("Start")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs

And here is the code I tried but it has errors:
function StopStart()
if boolStartStop == true
then
CommandStartStop.texture:SetColorTexture(255,0,0)
boolStartStop = false
fs:SetText("stop")
fs:SetText("Stop")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs
else
CommandStartStop.texture:SetColorTexture(0,255,0)
boolStartStop = true
fs:SetText("stop")
fs:SetText("Stop")
fs:SetPoint("CENTER")
BtnStopGo:SetFontString(fs)
BtnStopGo.text = fs
end
end

Here is the Bugsack error message:
"2x Grid\Grid.lua:641: attempt to index global 'fs' (a nil value)"

I've googled a lot and can't find the correct way to have text of a button change on clicking the button. Can anyone help please?

Dridzt 03-03-22 10:25 AM

Code:

local button = CreateFrame("Button", nil, UIParent, "UIPanelButtonTemplate")
button:SetSize(100,20)
button:SetPoint("CENTER")
button:RegisterForClicks("AnyUp")
button:SetText("Start")
button:SetScript("OnClick", function(self,btn)
  if self:GetText()=="Start" then
    self:SetText("Stop")
  else
    self:SetText("Start")
  end
end)
button:SetMovable(true)
button:RegisterForDrag("LeftButton")
button:SetScript("OnDragStart", button.StartMoving)
button:SetScript("OnDragStop", button.StopMovingOrSizing)
button:SetScript("OnHide", button.StopMovingOrSizing)
button:Show()

This should make a minimal button for testing but your error mentions line 641, meaning there's a ton of code you're not showing.

The most probable cause is that fs does not exist inside StopStart because it is defined as a local in another scope.

Firesong 03-04-22 12:09 AM

Thanks. That is so much more elegant than my effort as well as working!


All times are GMT -6. The time now is 11:38 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI