Author Topic: DComboBox selection question  (Read 1450 times)

0 Members and 1 Guest are viewing this topic.

Offline Doomed

  • Newbie
  • *
  • Posts: 28
  • Karma: 0
DComboBox selection question
« on: July 18, 2016, 02:52:40 PM »
To start off, i'm quite new to lua.

I'm trying to make it so when i have selected an option (a prop), I want it to print the actual model of it in console. I've put the options into a table, k is the option, and v is the model. The following code prints the option, but I can't figure out how to get it to print the model. If anyone can tell me how to do this i'd greatly appreciate it. Thanks

Code: [Select]
concommand.Add( "propmenu", function()
   
  local propmenu = vgui.Create( "DFrame" )
propmenu:SetPos( ScrW() * .35, ScrH() * .3 )
propmenu:SetSize( 200, 200 )
propmenu:SetTitle( "Select a prop" )
propmenu:SetVisible( true )
propmenu:SetDraggable( true )
propmenu:ShowCloseButton( true )
propmenu:MakePopup()

propList={}
propList["Chair"] = "models/props/cs_office/Chair_office.mdl"
propList["Rollermine"] = "models/Roller.mdl"

local pList = vgui.Create("DComboBox", propmenu );
pList:SetPos( 50, 50 )
pList:SetSize( 100, 20 )
 
for k,v in pairs(propList) do
    pList:AddChoice( k )

local pButton = vgui.Create( "DButton" )
pButton:SetParent(propmenu)
pButton:SetPos( 75, 150 )
pButton:SetText( "OK" )
pButton:SetSize( 60, 30 )
pButton.DoClick = function()

local selected = pList:GetSelected()
print(selected)

propmenu:Close()
end
end
end)

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: DComboBox selection question
« Reply #1 on: July 20, 2016, 12:33:53 AM »
When looping through a table, the two values (set to k and v in your example) are assigned the key and the value. You are correct, k is the option and v is the model. You are adding k, a string, to the propList. k has no connection to the model by itself. It is only when used in conjunction with the table that it references a model. So, use it in conjunction with the table. Instead of printing k, print the model that is indexed by k. (the notation for this is table[key], obviously replacing 'table' and 'key' with your table and key)
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.