• Drawing Images from table
    7 replies, posted
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 [IMG]http://gyazo.com/33643847858ecf847d5e6055a76f29e9.png[/IMG] Of course ill have other guns in the table not just the ak seen there. Any help is appreciated thanks :)
[QUOTE=_Jacob;44199082]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 [IMG_THUMB]http://gyazo.com/33643847858ecf847d5e6055a76f29e9.png[/IMG_THUMB] Of course ill have other guns in the table not just the ak seen there. Any help is appreciated thanks :)[/QUOTE] Is your question how to get the even spacing or how to work with the table? You can use DIconLayout ([URL="http://wiki.garrysmod.com/page/VGUI/Elements/DIconLayout"]wiki[/URL], [URL="https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/vgui/diconlayout.lua"]source[/URL]) 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 [CODE] 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[i][1]) --LINE 154 primcontslist.DoClick = function() end [/CODE] line 154 is nil obviously. But how would i then set the icon for each item in the table [CODE]ffa_ak47 = {"ffa/ffa_ak47"},[/CODE] assuming that code is copy/pasted for 5 lines
Send here full table. (Guntable)
Currently its only 1 image copy/pasted for testing [CODE]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"} }[/CODE]
You can't use an numeric index of the table ( guntable[i] ) 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: [code] for k, v in pairs( guntable ) do ... primcontslist.SetIcon( v[1] ) [/Code] 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: [url]http://lua-users.org/wiki/TablesTutorial[/url]
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[i][1])" in console it prints correctly but the picture isnt setting correctly. Heres the code: [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[i][1]) primcontslist:SetImage(primaries[i][1]) primcontslist.DoClick = function() print(primaries[i][2]) end end [/CODE] and the output. [IMG]http://gyazo.com/b8c0b26bc4e40f2f31e862c47db44f7c.png[/IMG]
Fixed everything. Was using the wrong file path like an idiot :D
Sorry, you need to Log In to post a reply to this thread.