Making Script More Compact With Array and For Loop

Hello friends,

Would it be possible to shorten this script with an array and append the OSC message with a for loop?

Thanks!
Sam

var
btn_1, btn_2, btn_3, btn_4 : Widget //used button's names

buttons : Widget Array = [btn_1, btn_2, btn_3, btn_4]

mOSC : OSCMessage //OSC Out Message


// Called when widget changed -> 
On WidgetValueChanged(newValue : double) from btn_1
    if newValue == 1.0 then
        OSC_SetAddress(mOSC , "/GigScript/btn_1")
        OSC_SendSpecific(mOSC, "127.0.0.1", 9007) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9001) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9002)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9003)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9004)
    end
End

On WidgetValueChanged(newValue : double) from btn_2
    if newValue == 1.0 then
        OSC_SetAddress(mOSC , "/GigScript/btn_2")
        OSC_SendSpecific(mOSC, "127.0.0.1", 9007) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9001) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9002)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9003)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9004)
    end
End

On WidgetValueChanged(newValue : double) from btn_3
    if newValue == 1.0 then
        OSC_SetAddress(mOSC , "/GigScript/btn_3")
        OSC_SendSpecific(mOSC, "127.0.0.1", 9007) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9001) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9002)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9003)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9004)
    end
End

On WidgetValueChanged(newValue : double) from btn_4
    if newValue == 1.0 then
        OSC_SetAddress(mOSC , "/GigScript/btn_4")
        OSC_SendSpecific(mOSC, "127.0.0.1", 9007) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9001) 
        OSC_SendSpecific(mOSC, "127.0.0.1", 9002)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9003)
        OSC_SendSpecific(mOSC, "127.0.0.1", 9004)
    end
End

var
   btn_1, btn_2, btn_3, btn_4 : Widget //used button's names
   mOSC : OSCMessage //OSC Out Message
   Ports : int array = [9007, 9001, 9002, 9003, 9004]
      
   OSC_ADDRESS : string = "127.0.0.1"
   OSC_GIGSCRIPT_BUTTON : string = "/GigScript/btn_"
   
function SendOscMessages(newValue : double, buttonIndex : integer)
   var portIndex : integer
   if newValue == 1.0 then
      OSC_SetAddress(mOSC , OSC_GIGSCRIPT_BUTTON + IntToString(buttonIndex + 1))
      for portIndex = 0; portIndex < Size(Ports); portIndex = portIndex + 1 do
         OSC_SendSpecific(mOSC, OSC_ADDRESS, Ports[portIndex]) 
      end
   end
end

On WidgetValueChanged( unused: widget, index: integer, newValue : double) from btn_1, btn_2, btn_3, btn_4
   SendOscMessages(newValue, index)
End

Remarks:

  • As naming convention I typically use capital names for global variables (I left btn_x uncapitalized, as you would need to change your rackspace. Also MOsc seemed odd.
  • I always use full capitalized names for constants. Always use constants were fixed strings are used which have a real meaning. You could even consider to replace 1.0 by a constant ‘BUTTON_PRESSED’ for readability.
  • I used the port numbers for an array.
  • With the size function you can find the length of an array.
  • You can easily convert int to strings, or any type to another type, there are plenty functions implemented out of the box.
  • You can concatenate strings with +.
  • On WidgetValueChanged can handle multiple widgets at the same time

In general it is always a good idea to use for loops, to copy duplicated code to a function and the (few) code parts which are different, to put them into parameters.

Disclaimer: I only checked if the code compiles, I did not run it.

6 Likes

Wow, @Michelkeijzers!

This is even better than I thought possible. I can tell this took a lot of work to put together. I really appreciate it!

There’s a lot of information and language that’s new to me. I’m going to spend some time studying/digesting it and get back to you once I’ve implemented it.

Thanks again!
Sam

2 Likes

Thanks, GPScript is very powerful once you get the hang of it.
Good luck implementing!