How to realize "radio buttons" with scripting

Because this is a topic that pops up regularly, i decided to upload an example for anyone who is in need of such a useful thing. I made two rows with a diffrent number of buttons, where each row acts “radio-like”.

radiobuttons_example.gig (142.5 KB)

This is the code that is contained in the rackspace:

var
btn_u1, btn_u2, btn_u3, btn_l1, btn_l2, btn_l3, btn_l4, btn_l5 : Widget //used button's names

u_last, l_last : Integer // buffer variables to store the last pressed button

//Declaration of widget arrays for upper and lower preset buttons
u_buttons : Widget Array = [btn_u1, btn_u2, btn_u3]
l_buttons : Widget Array = [btn_l1, btn_l2, btn_l3, btn_l4, btn_l5]


Initialization //Called on first run
var i : Integer
//Check & store state of buttons - if all off, assume the first as "ON"
    For i = 0; i < Size(u_buttons); i = i + 1 Do
        If GetWidgetValue(u_buttons[i]) == 1.0 Then
            u_last = i
        Else
            u_last = 0
        End
    End

    For i = 0; i < Size(l_buttons); i = i + 1 Do
        If GetWidgetValue(l_buttons[i]) == 1.0 Then
            l_last = i
        Else
            l_last = 0
        End
    End

End


// Called when any of several widgets changed -> This is the block for UPPER buttons
On WidgetValueChanged(w : Widget, index: integer, newValue : double) from btn_u1, btn_u2, btn_u3
    var i : integer
    if newValue == 1.0 and index <> u_last then
        // Deselect other buttons
        for i = 0; i < Size(u_buttons); i = i + 1 do
            if i <> index then SetWidgetValue(u_buttons[i],0.0) end
        end
    u_last = index
end

    //Prevent a button to be switched off manually
    If newValue == 0.0 and index == u_last Then
        SetWidgetValue(u_buttons[index],1.0)
    End
End

// Called when any of several widgets changed -> This is the block for LOWER buttons
On WidgetValueChanged(w : Widget, index: integer, newValue : double) from btn_l1, btn_l2, btn_l3, btn_l4, btn_l5
    var i : integer
    if newValue == 1.0 and index <> l_last then
        // Deselect other buttons
        for i = 0; i < Size(l_buttons); i = i + 1 do
            if i <> index then SetWidgetValue(l_buttons[i],0.0) end
        end
    l_last = index
    end

    //Prevent a button to be switched off manually
    If newValue == 0.0 and index == l_last Then
        SetWidgetValue(l_buttons[index],1.0)
    End
End

If you should have any questions, don’t hesitate to ask. :slight_smile:
Cheers
Erik

8 Likes

I really hope that at a time this script will be useless… :innocent:

5 Likes

I am definitely hoping with you! :innocent: …maybe we should use the Force to get what we want?! :alien:
Or even Voodoo? I could pin some needles in the buttons of my controller… :ghost:

2 Likes

ooh sexay! My first thought would be to incorporate the buttons to control plugin bypass state to audition a number of say stomp box distortion types for a guitar amp , fun way to audition each one separately with simply one foot press …