Hi there,
Apologies this may well be simple! I have a situation on my template whereby I need to access an array of variables. This is normally quite simple within a onvalue script, doing something like this (after declaring the variables):
const SearchSettings = ["Empty", Inst1, Inst2]
However, this only would get written to within that widget's script, I am trying to create a 'preset' system that would be recalled from many different places.
Therefore I thought the best method would be a sort of global array of variables that could be written and recalled anywhere when needed. The only way I can think to achieve it across the whole template would be in a dedicated variable widget, but how to do write and recall into an array of variables there?
I have tried putting something like this into the values field of the variable widget, but I realise this is over simplistic (variables not yet declared etc!):
[
Test1,
Test2,
Test3,
Test4
]
Any help greatly appreciated thanks!
To keep it as javascript-ish as possible you could create custom variables in some widget's onCreate function:
setVar(this, 'data', [1, 2]
and then use them in other scripts:
var data = getVar('widget_id', 'data')
Or maybe you simply wanted to write
[
"Test1",
"Test2",
"Test3",
"Test4"
]
1 Like
Ah, thanks for pointing me in the right direction Jean-Emmanuel, I think using your oncreate solution looks to be what I am after! Is it right then you could just recall and modify certain aspects of this array from any other widget just using something like:
setVar('widget_id', 'data', somevariable[i])
where somevariable is previously declared and 'i' is the array position?
And similarly could you could recall a particular value from the array with something like this?
var Particularvalue = getVar('widget_id', 'data', somevariable[i])
If this is the case, then I am good to go! So flexibility with Open Stage Control, still impresses me daily with new things I find out about using it!
Is it right then you could just recall and modify certain aspects of this array from any other widget just using something like:
setVar('widget_id', 'data', somevariable[i])
Not exactly, should be something like:
var data = getVar('widget_id', 'data')
data[i] = somethingElse
setVar('widget_id', 'data', data)
And similarly could you could recall a particular value from the array with something like this:
var Particularvalue = getVar('widget_id', 'data', somevariable[i])
Should be something like
var Particularvalue = getVar('widget_id', 'data')[i]
Note that setVar
and getVar
are only available in scripting properties, you can't use these in JS{} or #{} blocks.
1 Like
Thanks very much for these explanations, I had a feeling my syntax was some way off! I will get working on testing this out, incredibly helpful examples, top support as always!
Oh, I just had one final (and hopefully not too silly!) question to ask concerning arrays in open stage control. Is there such a thing as too big an array (and will large arrays impact performance too much?). I have been doing some rough calculations and for this 'preset' idea, the array may end up holding in excess of 1000 values!
If you're going to manipulate this array often it may indeed perform a bit slowly since getVar()
makes a copy of the variable before returning it. Another approach to avoid that (didn't think of it at first) would be to simply use global variables:
// some widget's onCreate
globals.data = [1,2,3]
// anywhere else
globals.data[1] // returns 2
globals.data[1] = -2 // assign a value
1 Like
That sounds a perfect solution, thanks for the suggestion, didn't realise this was possible, very cool!