Multiple Buttons without loads of code

I know it’s possible, just not sure how to go about it.

Question really is how would I create multiple buttons with only one chunk of code, not multiples like:
[lua]local Button01 = vgui.Create(“DButton”, lolparent)
local Button02 = vgui.Create(“DButton”, lolparent)
local Button03 = vgui.Create(“DButton”, lolparent)
local Button04 = vgui.Create(“DButton”, lolparent)
local Button05 = vgui.Create(“DButton”, lolparent)
local Button06 = vgui.Create(“DButton”, lolparent)[/lua]

But instead using maybe a for loop? I’m not sure how to go about doing this, that is why I’m asking.

Thanks for any help!

This is how you create a for loop…

[lua]
for i, number_of_calls do

-- create your button

end
[/lua]

You’d also want to keep the buttons in a table if you want to access them outside of the loop.

Thanks nevec, but I’m having problem understanding.

Here is what I have in mind currently:
[lua]local Buttons = {}
Buttons[1] = {“Button1”, “Title1”}
Buttons[2] = {“Button2”, “Title2”}
Buttons[3] = {“Button3”, “Title3”}

function drawButtons( )
for i, Button in ipairs(Buttons) do
local Button[1] = vgui.Create(“DButton”, parent)
Button[1]:SetTitle( Button[2] )
end
end[/lua]

I think this is what you want:

[lua]local Buttons = {
{“Title1”,{10,10},{50,25},function() print(“You pressed the first button!”) end},
{“Title2”,{20,20},{50,25},function() print(“You pressed the second button!”) end},
{ “Title3”,{30,30},{50,25},function() print(“You pressed the third button!”) end},
}

function drawButtons()
for _,v in ipairs(Buttons) do
local btn = vgui.Create(“DButton”, parent)
btn:SetTitle( v[1] )
btn:SetPos(v[2][1],v[2][1])
btn:SetSize(v[3][1],v[3][2])
btn.DoClick = v[4]
end
end[/lua]

Entoros, don’t overcomplicate it for him by not commenting on what you do.

Ok, basically, your ‘Buttons’ table is full of sub-tables that have two values in them. I’m guessing that you want the first value to be the variable name. You can’t/don’t need to do that unless you want the buttons to be global variables.

If you want to keep track of your buttons, you need to create a separate table to store the buttons in. Then you loop through the info table, create the buttons and store the actual button objects in a separate table.

Here’s how it works…

[lua]
– button info
local button_info =
{

-- this way is the same as..
-- button_info[ 1 ] = { ... }
-- button_info[ 2 ] = ...
-- this table has the index of one
{ "Title1", { 50, 50 } },

-- the first value( Title2 ) has the index
-- of 1 and the second value( the table )
-- has the index of 2
-- this table has the index of two
{ "Title2", { 50, 150 } }

--[[

button_info = { };

button_info[ 1 ] =
{
	
	[ 1 ] = "Title2",
	[ 2 ] =
	{
		
		[ 1 ] = 50,
		[ 2 ] = 150
		
	}
	
}

]]

};

– table that keeps the actual buttons
local actual_buttons = { };

function CreateButtons( )

-- loop through the info table
-- i is the current index( 1, 2, ... )
-- info is the table with that index
for i, info in pairs( button_info ) do
	
	-- create a new button( it can be local here )
	-- you can also do 'actual_buttons[ i ] = ... '
	-- but then every line would get a little longer
	-- since you'd have to replace 'button' with that
	-- code
	local button = vgui.Create( "DButton", parent );
	
	-- set the button's text to the first value
	-- we get from the loop
	button:SetText( info[ 1 ] );
	
	-- set the pos to the first and second values
	-- we have in the second value we get from the
	-- loop
	-- 'info[ 2 ]' is the table after the title
	-- 'info[ 2 ][ 1 ]' is the first value in that table
	button:SetPos( info[ 2 ][ 1 ], info[ 2 ][ 2 ] );
	
	-- and finally store the button in your button table
	actual_buttons[ i ] = button;
	
end

end
[/lua]

Tables don’t work, returns the next { as an error after local TableName = {