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

10 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:

3 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 …

Thank you! I’ve been wanting to setup radio buttons controlling patch selection in the global rackspace. Going to give this a try.
I’m also hoping Gig Performer will implement a radio button feature.

2 Likes

May you hope hard enough! :wink:

3 Likes

I have setup an alternative way to realise RadioButtons using a Skriptlet.

The advantage is that you can link the skriptlet directly to other components. You can save / copy / paste a group of components incl the related scriptlet and you have not to care about the rackspace script and there is no need to change / adjust a rackspace script.

The example shows a B3 organ with 7 presets mapped to 7 RadioButtons. You can switch the presets via the RadioButtons or via the BlackKeys if they are enabled on the keyboard.

RadioButton Skriptlet Demo.gig (879.1 KB)

The widget ‘btn_l2’ is declared in a script but does not exist in the rackspace called ‘Radio buttons’
Radio buttons (Rackspace) - GPScript System Function Exception: Attempt to access missing widget

How do I fix this? I’m trying to add more buttons. Initially, I deleted button2 then tried to add it again.

The script knows about the widgets via the name specified in the widget properties Advanced tab.

If you have added new ones or duplicated existing ones, then you will need to update the names in the advanced tab and ensure they are listed the same in the script. Or, if the widgets no longer exist, remove any reference to them in the script.

1 Like