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]
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)
[/CODE]
wouldnt v print the model?
[QUOTE=Invule;50735244]wouldnt v print the model?[/QUOTE]
I'm trying to make it so in the drop down list it says "Chair", but at the same time it will print the model. So far it will only print "chair" or "rollermine" in console, but it cannot get the model
Instead of
[CODE]
local selected = pList:GetSelected()
print(selected)
[/CODE]
Try
[CODE]
local selected = propList[pList:GetSelected()]
print(selected)
[/CODE]
[QUOTE=MPan1;50735314]Instead of
[CODE]
local selected = pList:GetSelected()
print(selected)
[/CODE]
Try
[CODE]
local selected = propList[pList:GetSelected()]
print(selected)
[/CODE][/QUOTE]
You're awesome, thanks :smile:
[lua]print(v)[/lua]
should also work since you are still inside the for loop. Any function you create or set (like DoClick) within that loop will be able to use k and v, even if called after the initial looping.
[QUOTE=Zet0r;50736044][lua]print(v)[/lua]
should also work since you are still inside the for loop. Any function you create or set (like DoClick) within that loop will be able to use k and v, even if called after the initial looping.[/QUOTE]
But he's checking the currently selected option at the time of the user clicking the button. That option can change, since the user can alter it before clicking the button.
You are creating a bunch of buttons on top of each other with that loop. You should create the button outside of the loop to avoid that.
[editline]![/editline]
And mpan is right, use the code in his first post after moving the button.
Yeah, got confused as he was creating all the buttons in the loop, I assumed every model had a button each that would print that model. MPan is right for a single button that checks the selected one.
Sorry, you need to Log In to post a reply to this thread.