Thread Tools Display Modes
05/25/14, 08:42 PM   #1
hulksmash
 
hulksmash's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 30
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")
  Reply With Quote
05/25/14, 09:19 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
What you're doing is fine. Are you having issues?
  Reply With Quote
05/25/14, 10:17 PM   #3
hulksmash
 
hulksmash's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 30
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
  Reply With Quote
05/26/14, 01:13 AM   #4
Kentarii
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 42
Originally Posted by hulksmash View Post
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
  Reply With Quote
05/26/14, 10:39 AM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Add local in front of each thisnode assignment.
  Reply With Quote
05/26/14, 12:30 PM   #6
hulksmash
 
hulksmash's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 30
Thanks that really pointed me in the right direction for what I needed
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » dynamic buttons


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