What is the best practice in order to set up global Variables?

Hi there,

So things are getting complex , but I'm almost on there and quite happy about it :smiley:

Basically I got this device that has a functionality that upon pressing some buttons it will change the MIDI channel which the device is setted into , so all the widgets will have to be switched to this newly selected Channel accordingly.

To switch those channels you have to press a modifierButton + stateButton , (there are 2 modifier buttons , but that is not important) . If the stateButton is pressed by its own it sends a MIDI message as well
modifierButton

"type": "button",
(...)
"id": "user",
(...)
"mode": "push",

stateButton

"type": "button",
(...)
"id": "trackControl01",
(...)
"mode": "push",
(...)
"script":
if((get('user') == 1)) {
   setVar( 'globalVariables', 'deviceMode' , 1);
} else if ((get('factory') == 1)) {
   setVar( 'globalVariables', 'deviceMode' , 9);
} else {
  send('midi:virtual-launchcontrolxl', '/note', getVar('globalVariables' , 'deviceMode') , 73 , 127);
  send('midi:virtual-launchcontrolxl', '/note', getVar('globalVariables' , 'deviceMode') , 73 , 0);
}

This above works fine if I define a variable in my initScript (which runs once upon initiation of the device, with all globals. definintion

setVar( 'globalVariables', 'deviceMode' , 9);

Ok so this work great for when I have to retrieve the variable in an script widget , the issue is that now I have knobs that need to parse that variable channel on their preArgs parameter, so I'm trying

knobAA

"preArgs": "[
JS {{ getVar('globalVariables' . 'deviceMode') }} ,
  13
]"

And that is not working.

My question is , how would you ideally Set a global variable? (with a default value) , How would you change it and how would you call it within your widgets?

Thanks

In this example there should be a return statement before getVar (or use #{} that adds the return statement under the hood). Also the dot should be a coma. Otherwise the method seems fine.

getVar can only be used in scripts, so this can't work.

The simplest way to set up global variable is a variable widget (set its value from scripts with set and retrieve it with @{} in properties or get() in scripts).

1 Like

Ok so I got your point . Still don't know why is not fully working. I have created a
(note json may not be perfectly correct as I'm just typing from the GUI

deviceMode

  "type":"variable",
  "id": "deviceMode",
 (...)
  "script": "set('this' , 9);",

then a button (with the modifiers on other side of the project non-shown in here for simplicity)
button_6

   "type": "button",
   "id": "button_6",
   (...)
   "script" : "
if((get('user') == 1)) {
   set('deviceMode' , 1);
} else if ((get('factory') == 1)) {
   set('deviceMode' , 9);
} else {
  send('midi:messing', '/note', get('deviceMode') , 6 , 20);
} ",

While the variable is getting parsed properly with the get('deviceMode') , the set('deviceMode' , 9); and other de-like statements are not working.

The variable's script resets its value to 9 every time it receives a value, I guess you just wanted to define the variable's default value in which case use the default property and leave script empty.

1 Like

That is amazing . Thanks a lot!!