Drawing Images from table

So basically im trying to draw guns images from a table called “guntable” guntable consists of paths to images “ffa/weapons/ffa_ak47” and i want to take these and place them evenly like so

Of course ill have other guns in the table not just the ak seen there. Any help is appreciated thanks :slight_smile:

Is your question how to get the even spacing or how to work with the table? You can use DIconLayout (wiki, source) to get the even spacing.

Oh wow i didn’t even know that existed thanks ill try it out

[editline]11th March 2014[/editline]

ok so im a little stumped



for i = 1, 5 do 
		local primcontslist = primconts:Add( "DImageButton" )
		primcontslist:SetSize(primary:GetWide()/2 - 20, primary:GetTall()/2 - 20)
		primcontslist.Paint = function(s,w,h)
			outlinedbox(0,0,0,w,h,Color(20,20,20,240),Color(200,200,200,255))
		end
		primcontslist.SetIcon(guntable*[1]) --LINE 154
		primcontslist.DoClick = function()

		end


line 154 is nil obviously. But how would i then set the icon for each item in the table


ffa_ak47 = {"ffa/ffa_ak47"},

assuming that code is copy/pasted for 5 lines

Send here full table. (Guntable)

Currently its only 1 image copy/pasted for testing


guntable = {
	ffa_ak47 = {"ffa/ffa_ak47"},
        ffa_ak47 = {"ffa/ffa_ak47"},
        ffa_ak47 = {"ffa/ffa_ak47"},
        ffa_ak47 = {"ffa/ffa_ak47"},
        ffa_ak47 = {"ffa/ffa_ak47"}
}

You can’t use an numeric index of the table ( guntable* ) because you give names to the keys of the table. You never set the value of guntable[1] etc, but you set the value of guntable[“ffa_ak47”].

Use a loop such as this:



for k, v in pairs( guntable ) do
    ...
    primcontslist.SetIcon( v[1] )


Beware, the order of the loop is undefined, mean your icons will not be sorted the same as how you define them in the table. Do you really need to give names to the keys ? Can’t you use an array instead ?

Learn more about Lua tables: http://lua-users.org/wiki/TablesTutorial

Ok this is a bit of a bump / update. So ive got it working but the materials are errors, i even tried using default gmod materials (didnt work) and even when i print “print(primaries*[1])” in console it prints correctly but the picture isnt setting correctly. Heres the code:



primaries = {
[1] = {"vgui/entities/acr", "ACR"},
[2] = {"vgui/entities/ak47", "AK-47"},
[3] = {"vgui/entities/amd65", "AMD-65"},
[4] = {"vgui/entities/an94", "AN-94"},
[5] = {"vgui/entities/auga3", "AUG-A3"},
}

primconts = vgui.Create("DIconLayout", primary)
primconts:SetSize(primary:GetWide() - 10, primary:GetTall() - 10)
primconts:SetPos(5,5)
primconts:SetSpaceX(5)
primconts:SetSpaceY(10)
primconts.Paint = function() end
for i = 1, #primaries do 
	local primcontslist = primconts:Add( "DImageButton" )
	primcontslist:SetSize(primary:GetWide()/2 - 20, primary:GetTall()/3 - 20)
	primcontslist.Paint = function(s,w,h)
		outlinedbox(0,0,0,w,h,Color(20,20,20,240),Color(200,200,200,255))
	end
	print(primaries*[1])
	primcontslist:SetImage(primaries*[1])
	primcontslist.DoClick = function()
	print(primaries*[2])
	end
end


and the output.

Fixed everything. Was using the wrong file path like an idiot :smiley: