for loops in derma buttons?




for i1=1,3 do

	local x = 24
	local y = 24*i1
	local pShopBut = vgui.Create('DButton')
	pShopBut:SetText( "Button "..i1 )
	pShopBut:SizeToContents()
	pShopBut:SetTall( 127 )
	pShopBut:SetWide( 127 )
	pShopBut:SetPos(x,y+24)
	
	end


I tried this but it only draws “button 3”

shouldn’t this variable
[lua]
local y = 24[*i1
[/lua]

be?

[lua]
local y = 24*i1
[/lua]

Yeah sorry that was a accident, that wasnt in the code i was testing

Error here: local y = 24[*i1

Remove the [

-didn’t notice replies, were none when I opened thread, read rest below-

Additionally, when using a loop to add buttons, or whatever, I’d recommend setting up a table so you can access the buttons later. Also it’d be good practice to set up a table containing functions for the DoClick for when you create them.

Your buttons are 127 pixels tall but you are only spacing them 24 pixels apart, they are overlapping each other.

Thankyou!

[editline]28th October 2014[/editline]

How would i use tables to set the positions of my 6 buttons?


local x = {
	"x1" = 20,
	"x2" = 187,
	"x3" = 20,
	"x4" = 187,
	"x5" = 20,
	"x6" = 187
	}
	
	local y = {
	"y1" = 50,
	"y2" = 50,
	"y3" = 187,
	"y4" = 187,
	"y5" = 304,
	"y6" = 304
	}
for i1=1,6 do	
local pShopBut = vgui.Create('DButton')
	pShopBut:SetText( "Button "..i1 )
	pShopBut:SizeToContents()
	pShopBut:SetTall( 127 )
	pShopBut:SetWide( 127 )
	pShopBut:SetPos(x,y)-- this is where i would go through the table and insert the numbers
end




for i1=1,6 do	
local x = (i1 % 2 == 0 and 187 or 20)
local y = (i1 <= 2 and 50 or (i1 <= 4 and 187 or 304))
local pShopBut = vgui.Create('DButton')
	pShopBut:SetText( "Button "..i1 )
	pShopBut:SizeToContents()
	pShopBut:SetTall( 127 )
	pShopBut:SetWide( 127 )
	pShopBut:SetPos(x,y)-- this is where i would go through the table and insert the numbers
end


Something like this might work.