Defining global functions with TouchOSC

The new version of TouchOSC looks like it might be a decent competitor to Lemur but I’m struggling due to a lack of documentation (and they don’t seem to have a support forum either)

Did anybody here get the new TouchOSC app? If so, did you figure out how to define a global function that will be available to all callbacks?

Available for all global callbacks, or those at a widget/control level?
The only way I’ve found to communicate between the global script and widgets/controls is using the notify function. Everything I’ve tried to reference a custom global function in the widget/control script ends up with: No such property or function.

Some examples based on what I’ve figured out…

This is a button script (named ‘button2’):

function onValueChanged(key)
  if  key == 'touch' and self.values[key] == true then
    self.notify(self.parent,, "Hello World")

This is in the global script:

function onReceiveNotify(source, string)
  print(string.." from "..source)

Console message, after pressing button2.

You can use the notify function to go back the other way, passing parameters from a global function back to particular child controls.

When sticking within the global script, this part is within the onReceiveOSC callback:

local myNumber = 10
print("Start with: "..myNumber)
print("End with: "..multiplyByTen(myNumber))

This is the multiplyByTen function:

function multiplyByTen(number)
  return (number * 10)

This is the console message:

First of all, where did you put a global script?

I want to just put regular functions in there that can be called from multiple widget/control callbacks

Accessed by clicking the background. Then the script box in the right-pane is the global script.

Oh yeah, I had tried that first. I put a trivial function there and tried to call it from a control and got some message about a nil something or other.

After looking at their document, it seems that every control has its own Lua context which is probably why this doesn’t work. Sounds like a bad approach…they should at least have provided some shared area somewhere. I wonder if there is an “include” mechanism that could be used, but even if there is, it seems like there might be a problem controlling all the local controls.

For example, if you have ten faders, how would you write a function that would set them all to zero?

You can easily set the controls’ parameters from the global script.

Have a look at the very last part of the example I posted here:

The key would be to put all the faders in a group. That way you can refer to them by index value.

Yes, I saw that self.children but that seems to be only available inside the OnReceiveOSC callback — how would one have “normal” global functions that can refer to those things?

As for putting faders in a group (which I already did), I probably should have asked my question differently…i.e. how do I set all faders except the one I’m passing as a parameter to 0?

In other words, implement radio button behavior

function onReceiveOSC(message, connections)
  RadioButton('faders', 3, 1.0)

function RadioButton(group, index, value)
  for i = 1, #self.children[group].children do
      if i == index then
        self.children[group].children[i].values.x = value
        self.children[group].children[i].values.x = 0.0

Right. But I want to be able to call that RadioButton from the script in a control.

Otherwise it looks like one would have to put all the code in that global document and never add code to the individual controls