LInking DNumSliders

I am working a new tool where I require three different sliders to affect each other when there values are changed. For instance, when one slider is increased, the value of the other 2 sliders decreases so that the total values of all three sliders equal a set overall value. I have some partial success with making this work, but I have run into a snag and need help figuring it out. Here is the code in question:



 local EmFocus = false
 local KinFocus = false
 local ThermFocus = false
 
 local EmSlider = vgui.Create( "DNumSlider", panel )
 local KinSlider = vgui.Create( "DNumSlider", panel )
 local ThermSlider = vgui.Create( "DNumSlider", panel )
 
  EmSlider:SetWide( 150 )
  EmSlider:SetText( "Electro-Magnetic Resistance" )
  EmSlider:SetMinMax( 0, 90 )
  EmSlider:SetDecimals( 0 )
  EmSlider:SetValue( LocalPlayer():GetInfo( "ac_coretool_em" ) )
 
  KinSlider:SetWide( 150 )
  KinSlider:SetText( "Kinetic Resistance" )
  KinSlider:SetMinMax( 0, 90 )
  KinSlider:SetDecimals( 0 )
  KinSlider:SetValue( LocalPlayer():GetInfo( "ac_coretool_kin" ) )
  ThermSlider:SetWide( 150 )
  ThermSlider:SetText( "Thermal Resistance" )
  ThermSlider:SetMinMax( 0, 90 )
  ThermSlider:SetDecimals( 0 )
  ThermSlider:SetValue( LocalPlayer():GetInfo( "ac_coretool_therm" ) )
 
  EmSlider.ValueChanged = function( self, value )
   if not KinFocus and not ThermFocus then
    EmFocus = true
    local kval = ( 90 - value ) * 0.5
    local tval = ( 90 - value ) * 0.5
    KinSlider:SetValue( math.Clamp( kval, 0, 90 ) )
    ThermSlider:SetValue( math.Clamp( tval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_em", value )
    RunConsoleCommand( "ac_coretool_kin", math.Clamp( kval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_therm", math.Clamp( tval, 0, 90 ) )
    EmFocus = false
   end
  end
 
  KinSlider.ValueChanged = function( self, value )
   if not EmFocus and not ThermFocus then
    KinFocus = true
    local eval = ( 90 - value ) * 0.5
    local tval = ( 90 - value ) * 0.5
 
    EmSlider:SetValue( math.Clamp( eval, 0, 90 ) )
    ThermSlider:SetValue( math.Clamp( tval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_em", math.Clamp( eval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_kin", value )
    RunConsoleCommand( "ac_coretool_therm", math.Clamp( tval, 0, 90 ) )
    KinFocus = false
   end
  end
 
  ThermSlider.ValueChanged = function( self, value )
   if not EmFocus and not KinFocus then
    ThermFocus = true
    local eval = ( 90 - value ) * 0.5
    local kval = ( 90 - value ) * 0.5
 
    EmSlider:SetValue( math.Clamp( eval, 0, 90 ) )
    KinSlider:SetValue( math.Clamp( kval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_em", math.Clamp( eval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_kin", math.Clamp( kval, 0, 90 ) )
    RunConsoleCommand( "ac_coretool_therm", value )
    ThermFocus = false
   end
  end
 panel:AddItem( EmSlider )
 panel:AddItem( KinSlider )
 panel:AddItem( ThermSlider )


With this code I have managed to make the actual values change, but it seems that if a slider has a ValueChanged in the code, the actual “bullet” on the slider does not move when it’s value is changed indirectly. I have tested changing values indirectly when a ValueChanged is not present for the slider and it works perfectly, but without a ValueChanged, that slider can not affect the others.

Also, you may notice I have had to set up my own Focus handling to prevent infinite loops and buffer overflows. DNumSliders don’t seem to work with HasFocus and I haven’t been able to find any other function that is equivalent.

If there are any coders or derma people out there that may have a solution or theory as to how I can make this work, your help would be appreciated.

Wow, 12 hours and 44 views later and still no reply? Did I ask a stupid question or is this just something that is difficult enough to not bother with?

Well, I’ll continue to mess around with it and see what I can figure out on my own, but again, ANY suggestions that could help me would be appreciated.

Okey, finally figured out the solution. For those that are cusrious and may have this problem in the future, the way to make it work is to use OnValueChnaged instead ValueChanged. OnValueChanged is completly undocumented on the wiki, but ti seems to work exactly the same as ValueChanged, with the added functionality of updating the sliders bullet on the derma panel.

Update it on the wiki,

Truth be told, if you ever discover how an un-documented function works, help others by editing it on the wiki.