ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   dynamic buttons (https://www.esoui.com/forums/showthread.php?t=1628)

hulksmash 05/25/14 08:42 PM

dynamic buttons
 
I am trying to create a bunch of buttons in a FOR loop. What is the best practice to go about doing this? I need to be able to change the button text outside the loop in another function.


Code:

       
for p=1,4,1 do
        myButton[p] = Chain( wm:CreateControl("button"..p, ss, CT_BUTTON) )
                        :SetDimensions(50,50)
                        :SetText("button"..p)
                        :SetAnchor(TOPLEFT, ss, TOPLEFT, 0,(p*50)-50)
                        :SetFont("ZoFontAlert")
                .__END
end
myButton[p]:SetText("newname")


Seerah 05/25/14 09:19 PM

What you're doing is fine. Are you having issues?

hulksmash 05/25/14 10:17 PM

Well going further I have several objects I create in the loop along with the button. I wanted to keep them together in a structure of some sort. For example:

Code:

local node = {}

for p=1,4,1 do
        node[p].button = Chain( wm:CreateControl("button"..p, ss, CT_BUTTON) )
                        :SetDimensions(50,50)
                        :SetText("button"..p)
                        :SetAnchor(TOPLEFT, ss, TOPLEFT, 0,(p*50)-50)
                        :SetFont("ZoFontAlert")
                .__END
end

function update()
  node[1].button:SetText("newname")
end

Which would not work so I tried something like this but then I cant reference the button text anymore.

Code:

local node = {}

for p=1,4,1 do
        thisNode = node[p]
        thisNode.button = Chain( wm:CreateControl("button"..p, ss, CT_BUTTON) )
                        :SetDimensions(50,50)
                        :SetText("button"..p)
                        :SetAnchor(TOPLEFT, ss, TOPLEFT, 0,(p*50)-50)
                        :SetFont("ZoFontAlert")
                .__END
end

function update()
  thisNode = node[1]
  thisNode.button:SetText("newname")
end


Kentarii 05/26/14 01:13 AM

Quote:

Originally Posted by hulksmash (Post 8478)
Which would not work so I tried something like this but then I cant reference the button text anymore.

Let me guess.. you get a nil error?

If you want to use tables, you need to declare them properly, here's two approaches:
Lua Code:
  1. local node = {} -- define node tables
  2.  
  3. for p=1,4,1 do
  4.   node[p] = {} -- define inner table
  5.   node[p].button = YourButtonFunction(..)
  6. end
  7.  
  8. for i=5,8,1 do
  9.   node[i] = {
  10.     button = YourButtonFunction(..)
  11.   }
  12. end
  13.  
  14. for j=1,8,1 do
  15.   d(node[j])
  16. end

(Disclaimer, code is untested and might contain errors)
http://www.lua.org/pil/2.5.html

Seerah 05/26/14 10:39 AM

Add local in front of each thisnode assignment.

hulksmash 05/26/14 12:30 PM

Thanks that really pointed me in the right direction for what I needed


All times are GMT -6. The time now is 10:20 AM.

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