I have a DComboBox that is able to change a certain command in the way shown in the example below.
[CODE]
local ComboBox = vgui.Create( "DComboBox" )
if GetConVarNumber( "example_command" ) == 1 then
ComboBox:SetValue( "Command is true" )
else
ComboBox:SetValue( "Command is false" )
end
ComboBox:AddChoice( "Make command true" )
ComboBox:AddChoice( "Make command false" )
ComboBox.OnSelect = function( ind, val, data )
if val == 1 then
RunConsoleCommand( "example_command", "1" )
else
RunConsoleCommand( "example_command", "0" )
end
end
[/CODE]
The only problem is that the ConVarNumber checking part only runs once, meaning if you change the convar while the ComboBox is open, the value of it will not change. I could put the checking statement in the paint function of this box, but that sounds a bit expensive to me. Any ideas?
This isn't directly related to what you're asking, but you appear to be missing the implicit "[B]self[/B]" parameter in your OnSelect function.
You should either modify it to [code]ComboBox.OnSelect = function( self, ind, val, data )[/code] or
[code]function ComboBox:OnSelect( ind, val, data )[/code]
To your actual question, you can do something like the following using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/cvars/AddChangeCallback]cvars.AddChangeCallback[/url]:
[code]
ComboBox = ...
...
cvars.AddChangeCallback( "example_cvar", function( cvar, old, new )
if ( !IsValid( ComboBox ) ) then return end
ComboBox:SetValue( "Command is " .. ( tobool( new ) and "true" or "false" ) )
end, "something_unique" )
[/code]
Sorry, you need to Log In to post a reply to this thread.