Small Script for a 7 way Switch

I wrote this script because I needed a 3 way selector switch, then wondered how it would scale if I wanted more positions. So this script is salable to any number of positions (although I think 10 or so would be quite enough :slight_smile:).

The light were just for my testing (it was a pain to keep switching back to the plugin view to see if it worked).
Code:-

Var
Switch1 : Widget

//All Plugins in switch need to be declared here
Plugin1 : PluginBlock
Plugin2 : PluginBlock
Plugin3 : PluginBlock
Plugin4 : PluginBlock
Plugin5 : PluginBlock
Plugin6 : PluginBlock
Plugin7 : PluginBlock

   // Global flags
   Switch1Snaped : Boolean

   // We'll actually keep these in an array and just index them
   // That will allow us to use some functions that will work for all widgets
   position : PluginBlock Array // Dynamic array
   is_active : Boolean Array // Dynamic array
   no_plugins : integer
   j : integer
   
Initialization 
   
   // The other 2 lines that need to be set for Switch
   no_plugins = 7 //Set Number of Plugins
   
   position = [Plugin1, Plugin2, Plugin3, Plugin4, Plugin5, Plugin6, Plugin7]; // Add all names of plugins here
   
   // ***** Nothing more needs to be changed *****
   
   // Global flags initialization
   Switch1Snaped = False;
   for j = 0; j < no_plugins; j = j + 1 do 
   is_active[j] = True
end
End


// The array here indicates the value to which each Plugin should be set
Function UpdateAllplugins (vs : Boolean Array)    
var i : integer

for i = 0; i < no_plugins; i = i + 1 do 
    SetPluginBypassed(position[i], vs[i])
    is_active[i] = vs[i] == False
end    

end

// Called when  widget value has changed to do the actual work 
Function Process(widgetIndex : Integer)
var vs : Boolean array
i : integer

for i = 0; i < no_plugins; i = i + 1 do 
    vs[i] = True
end  

if is_active[widgetIndex] 
   then
      vs[widgetIndex] = True // This is the one we need to deactivate
      UpdateAllplugins (vs)
   else
      if not is_active[widgetIndex]
        then
          vs[widgetIndex] = False // This is the one we need to activate
          UpdateAllplugins (vs)
       end
   end
   
End

// Convert Step Dent Position to slider value
Function Switchposition(widgetValue : Double) returns Double
   
var
stepValue : Integer

stepValue = Round(widgetValue * (no_plugins-1));    
result = Scale(stepValue,0, (no_plugins-1), 0, 1); 

End

// So make one of these for each MidiIn block you want to control
On WidgetValueChanged(newValue : double) from Switch1
var pos : Double

pos = Switchposition(newValue)

   If Switch1Snaped
   Then
 Switch1Snaped = False;
   Else
 Switch1.SetWidgetValue(pos);
 Switch1Snaped = True;
 If IsPluginBypassed( position[Round(newValue*(no_plugins-1))]) //Sort Debounce Issue
 then
    Process(Round(newValue*(no_plugins-1)))
 end   
End

End

And a Gig file to show it:-
7 pos Switch.gig (35.5 KB)

8 Likes

Welcome to the GPScript club :wink::+1:

Thanks for sharing this. One suggestion…I think you can initialize the variable
no_plugins

automatically by writing

 no_plugins = Size(position)

after you have initialized the position array.

Cool, Works great.

Now all you have to Adjust is the names of the plugin’s and match that in the ‘position’ array to have a ‘clicky’ position switch of any size :slight_smile:

Thanks for sharing!
(I edited your post, so that the code appears now in one coherent block.)

1 Like

Ok quick question - How do you post code in a block like that?

Ah… it can be a bit tricky, but it’s this button of the editor:

code-editor

Just paste the whole code into your posting and make sure that there are at least two empty lines (CRLF) at the beginning and end of the text block (i guess that those double empty-lines must be avoided within the text block to keep it in one piece) - then mark the block and press this “code-button”.

Thank you.

There needs to be a small bit of code extra in my script. As i tested there was always only 1 Plugin ever active, but if you some how get all Plugins active (i.e when you are first assembling the patch-board or you were doing something weird at a gig (hey we’re musicians :slight_smile: ) and saved on shutdown) the script for the ‘switch’ stops working. So i have added an initialization routine that only runs once at start and sets the switch to ‘Position 1’

Var
    Switch1 : Widget
    
    //All Plugins in switch need to be declared here.
    Plugin1 : PluginBlock
    Plugin2 : PluginBlock
    Plugin3 : PluginBlock
    Plugin4 : PluginBlock
    Plugin5 : PluginBlock
    Plugin6 : PluginBlock
    Plugin7 : PluginBlock

   // Global flags
   Switch1Snaped : Boolean

   // We'll actually keep these in an array and just index them
   // That will allow us to use some functions that will work for all widgets
   position : PluginBlock Array // Dynamic array for plugin's Switch Position
   is_active : Boolean Array // Dynamic array that tracks Plugin is 'Active'
   no_plugins : integer // Used to Know how many 'Positions'/ Plugins Sitch has 
   j : integer // (Not really used globally but needed in initialization :) )
   start : integer
   
Initialization 
   
   // The other line that need to be set for Switch
   
   position = [Plugin1, Plugin2, Plugin3, Plugin4, Plugin5, Plugin6, Plugin7]; // Add all names of plugins here
   
   // ***** Nothing more needs to be changed *****
   
   // Global flags initialization
   
   no_plugins = Size(position)
   Switch1Snaped = False;
   start = 0
   for j = 0; j < no_plugins; j = j + 1 do 
       is_active[j] = True
   end

End


// The array here indicates the value to which each Plugin should be set/ or do some function at that position
Function UpdateAllplugins (vs : Boolean Array)    
    var i : integer
    
    for i = 0; i < no_plugins; i = i + 1 do 
        SetPluginBypassed(position[i], vs[i]) // if you want switch 'Dent' to some other function this is the line to change/sort
        is_active[i] = vs[i] == True // this is set to 'True' if by passing plugins
    end    

end

// Called when  widget value has changed to do the actual work
Function Process(widgetIndex : Integer)
    var vs : Boolean array
    i : integer
    
    for i = 0; i < no_plugins; i = i + 1 do 
        vs[i] = True
    end  
    
    if is_active[widgetIndex] 
       then
          vs[widgetIndex] = False // This is the one we need to deactivate (default - False)
          UpdateAllplugins (vs)
       else
          if not is_active[widgetIndex]
            then
              vs[widgetIndex] = True // This is the one we need to activate (default - True)
              UpdateAllplugins (vs)
           end
       end
   
End

// Convert Step Dent Position to slider value
Function Switchposition(widgetValue : Double) returns Double
   
var
    stepValue : Integer
    
    stepValue = Round(widgetValue * (no_plugins-1));    
    result = Scale(stepValue,0, (no_plugins-1), 0, 1); 
    
End


// So make one of these for each Plugin block you want to control
On WidgetValueChanged(newValue : double) from Switch1
var pos : Double

pos = Switchposition(newValue)

   If start==0
   Then
     Switch1.SetWidgetValue(0);
     Switch1Snaped = True;
     Process(0)
     start=1
   End
   
   If Switch1Snaped
   Then
     Switch1Snaped = False;
   Else
     Switch1.SetWidgetValue(pos);
     Switch1Snaped = True;
     If IsPluginBypassed( position[Round(newValue*(no_plugins-1))]) //Sort Debounce Issue
     then
        Process(Round(newValue*(no_plugins-1)))
     end   
    End

End

I have also integrated dhj’s tweak (thanks again), cleaned the code up a bit and added more comments to let others futz with it if the need :slight_smile:

6 Likes

Great script! I wanted to adopt it to visually do the same thing, but not based on plugin states, simply to turn the button widgets On/Off. I spent a lot of time editing your script, but couldn’t get it. I’m still learning (understatement of the year) scripting. So all I would like is the same slider (like those steps!!! :)) and setting the widget states o to 1 accordingly. How would you set that up @fr33sp1r1t? Thanks!

In this case, it helps to show the error messages that are produced by the GP Script compiler!

Yeah, I know…but it got to be too much of a mess and I need to start clean. For example, this script is looking for plugins and plugin states and I wanted it to look at widget states so I changed from plugins to widgets and pluginblock array to widget array and I think that was a hack trial and error and it just became a mess.

Great stuff!
I wonder how could it be adapted using radio buttons with this extension GitHub - gp-rank13/gp-radiobuttons: Radio Button Extension for Gig Performer

1 Like