Hi!
I’m a new user of O-S-C, coming from TouchOSC, and it’s a massive improvement!
I did not find a way to define a utiliy JS function globally, so I could use it in any value/script property
- custom module cannot export arbitrary function
- “variable” types apparently cannot be used as functions library.
e.g. { “foo”: function (v) {console.log(“bar”); } }
Did anyone already solved this somehow?
Regards,
Guillaume
1 Like
You can use the globals
object to share things between scripts:
JS{{
globals.foo = function(){/*...*/}
}}
2 Likes
Hi, sorry to pollute this thread, but I'm not really an expert on JS.
I want to declare a function globally (for the template) so it can be called by any widget at anytime.
I'm doing a simple example to check if this works, 1 template with 2 Widgets:
Widget 1) script_1
In this one I define a global function
"type": "script",
"id": "script_1",
"value": "globals.launchpadLedColorsA = function launchpadLedColorsA (velocityReceived) {
var colorsArrayA = {
\"1\": \"#990000\",
\"2\": \"#dd0000\",
\"3\": \"#ff0000\",
}
return colorsArrayA[velocityReceived] || \"#E0E0E0\";
}",
Widget 2) Text Widget
Thisone will call up the function of the script just to test it
"type": "text",
"value": "JS {{\n launchpadLedColorsA(2);}}",
Unfortunatelly seems that the function has not been parsed.
Any suggestions? Thanks
That script in the script's value has no reason to be executed, it's not in a JS{} block, either put it in a JS{} or in the script property (with event set to "once"). Make sure the script widget is parsed before any other widget that uses this function.
1 Like
Thank you, I have just placed the code in script
property , and set event
to once
.
I have also placed the scriptWidget before the textWidget (so it get parsed the first?)
It won't work.
global-js-function.json (3.9 KB)
Thanks
There's a space between "JS" and "{", and you need to call globals.launchpadLedColorsA, not just launchpadLedColorsA. And you need a return statement in that block or else it won't return anything.
1 Like
Brilliance is just one word, but it could be changed with your name Thanks so much
Strange behaviour in these 2 different examples.
f you work with this example, the call to the global function is not been recognised.
css-osc-listener.json (18.9 KB)
While in the nextone it looks like its the same behaviour, but as asoon as you change the fader's value it will kind of bang
the function and will start working.
svg.json (11.8 KB)
Any ideas? Thanks
css-osc-listener.json (18.9 KB)
functionDefinitions.event should be set to "once" so that the script is executed when the session loads.
1 Like