Reg - how many?!
-
@DanH Vars should always be a last resort.
Read this - https://forum.hise.audio/topic/79/scripting-best-practices
And this - https://docs.hise.audio/scripting/scripting-in-hise/hise-script-coding-standards.html#variables (scroll down to the "Types" section).Why don't you use a single array to store your global cable values? Is this something you could use a broadcaster for instead of a timer?
-
@d-healey haven't used broadcasters before, could be a possibility I guess, I've been following @Christoph-Hart 's guide on how to use global cables thus far.
https://docs.hise.audio/scripting/scripting-api/globalroutingmanager/index.html
https://docs.hise.audio/scripting/scripting-api/globalcable/index.html -
@DanH yeah store them in an array.
-
@d-healey @Christoph-Hart Argh arrays!
I might make a small snippet if you fancy helping me out!
-
An array is just like lots of normal variables living in one house. So you only need to go to one address and ask for the person you want, instead of going to multiple addresses :p.
I hope that analogy works...
Let's imagine you are using
reg
variables like thisreg myGlobalCableData0; reg myGlobalCableData1; reg myGlobalCableData2; reg myGlobalCableData3; reg myGlobalCableData4; function myTimerCallback() { myGlobalCableData0 = someValue; myGlobalCableData1 = someValue; myGlobalCableData2 = someValue; myGlobalCableData3 = someValue; myGlobalCableData4 = someValue; }
You can do it with an array like this
const myGlobalCableData = []; // Arrays should pretty much always be const function myTimerCallback() { myGlobalCableData[0] = someValue; myGlobalCableData[1] = someValue; myGlobalCableData[2] = someValue; myGlobalCableData[3] = someValue; myGlobalCableData[4] = someValue; }
You might find this useful too:
https://youtu.be/Y8sraa5ig-M -
-
@iamlamprey I don't think there is any benefit to them being var or reg.
So unless they're in an inline function, in which case they should be local like everything else, const seems to be the best option, unless you can think of a reason I've missed?
Oh and of course inside a regular function they should be var because that's all we have there.
-
@d-healey If there's no performance overhead then I guess it's just good coding practice to make them const since the actual array isn't changing, just what's in it
I guess var / reg would only be used if you're pointing it to another array or something :man_shrugging:
-
@iamlamprey The engine can resolve a dot operator for a const object at compile time, so it improves the performance of function calls.
var slow = [1, 2, 3, 4, 5]; slow.indexOf(2); // this requires two table lookups to get the function to call const var fast = [1, 2, 3, 4, 5]; fast.indexOf(2); // this function call can be resolved at compile time.
-
@DanH
Aconst
array will always be an array whereas areg
could be something else later :reg myArray = [] ; reg myArray = 42; // this will work. const myArray = [] ; const myArray = 42; // this won't
If I'm not mistaken, it has something to do with memory allocation. That's why it's always a good practice to
reserve
the arrays's slots number if you know it.const myArray = [] ; myArray.reserve(4); // push 4 variables into the array later
Edit : ah, didn't see @Christoph-Hart 's answer