1. Post #1
    BastinkaLive's Avatar
    March 2009
    232 Posts
    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:
    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)

    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!

  2. Post #2
    Gold Member
    Nevec's Avatar
    June 2008
    1,091 Posts
    This is how you create a for loop..

    for i, number_of_calls do
    	
    	-- create your button
    	
    end
    

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

  3. Post #3
    BastinkaLive's Avatar
    March 2009
    232 Posts
    Thanks nevec, but I'm having problem understanding.

    Here is what I have in mind currently:
    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

  4. Post #4
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    I think this is what you want:

    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

  5. Post #5
    Gold Member
    Nevec's Avatar
    June 2008
    1,091 Posts
    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..

    -- 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
    

  6. Post #6
    BastinkaLive's Avatar
    March 2009
    232 Posts
    Tables don't work, returns the next { as an error after local TableName = {