1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Solved Changing a UI switch from a script

Discussion in 'Scripting Workshop' started by stefangs, Nov 12, 2021.

  1. stefangs

    stefangs NI Product Owner

    Messages:
    97
    Is it possible to change a UI toggle switch without clicking on it, but rather a script statement?

    For example, I'm trying to turn off switch A when I click on switch B.
     
  2. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Yes, you just set its value to 0 or 1.
     
  3. stefangs

    stefangs NI Product Owner

    Messages:
    97
    Thanks ED, much better, but not quite there. I would like to ignore a click on the switch if it is already selected, so that only one switch can be active and also one will always be active. I tried this to no avail (with the same code reversed for the other button):

    Code:
    on ui_control ($switch_a)
        if ($switch_a = 0)
            $switch_b := 0
            else if ($switch_a = 1)
                exit
            end if
        end if
    end on
    Edit: I forgot to mention there are custom graphics behind the switches. Don't know if that would make a difference, though.
     
    Last edited: Nov 12, 2021
  4. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You cannot ignore clicks in KSP. You can just set values. In your case you want this:

    Code:
    on init
       declare ui_switch $A
       declare ui_switch $B
       $A := 1
    end on
    
    on ui_control ($A)
       $A := 1
       $B := 0
    end on
    
    on ui_control ($B)
       $A := 0
       $B := 1
    end on
    
     
    • Like Like x 1
  5. stefangs

    stefangs NI Product Owner

    Messages:
    97
    Oh yes, I think it's working now. Thanks much!!