How do you INIT your global variables
Hi all,
How do you init your JS global variables, please? Do you use any kind of init function, which you trigger by the loadbang? Like this..
var globalVariable;
function init() {
globalVariable = 1;
}
Or is the [compile] Message for this? So you send it by loadbang?
Thanks in advice.
Hello Martin,
if you declare your variable in the global scope, as you just did there, you can assign the variable to a value immediately.
var globalVariable = 1;
When your [js] compiles it declares the variables to the assigned values in the global scope, no need for other functions.
-Kristof
related question: what if I want to change the global var? I tried using jsarguments and then changing them via the jsargs message but it doesn't seem to work...
var var0 = jsarguments[0]; // name of object
var var1 = 1; // default value
var var2 = 2; // default value
if (jsarguments.length > 1) {
var1 = jsarguments[1];
if (jsarguments.length > 2) {
var2 = jsarguments[2];
if (jsarguments.length > 3) {
post( "Warning: Too many arguments for js object", var0 );
}
}
}
Hi Michelez,
javascript basically compiles, runs from the top of the code, line after line. If you declared your global variable on the top of your code, simply later in a function or later in your code you reassign it to a new value and you should be fine.
Also remember to use the post(); function to debug, check the changes.
Hi,
Thank you for the answer. But the question is when is it compiled? Do I have to trigger it (by sending the [complie] message)? Or is it compiled also when I open the application?
Thanks
Both.
When you reload the patcher every js object is recompiled. When you save your edited code, it also compiles. Ofcourse you can trigger it to recompile while your patch is up n running.
The global code (outside of any function) runs when the patch is loaded.
A full answer to your question (mentioning some limitations) is on page 12 of
the manual.
Thanks!
I think it is worth to keep this thread going a little longer...
I was trying to update a global variable to then use it into a constructor class included in the code
As Kristof points out it is possible to update a global variable within a function in the code. So if I write
function updateGlobal(newVal){
myGlobal = newVal;
object = new myClass(myGlobal);
}
whenever I call that function myGlobal is updated. And "object" is re-instantiated with the new myGlobal variable.
So far so good. But if I try to update an array instead of a single variable this solution won't work. I guess js function only accept single agruments from max?
(In general I find it hard to send arrays from max to JS unluess you set the as jsarguments and then use the arrayfromargs().)
The solution I found is to use the Global object which accepts arrays as explained on pages 32 and 33 of the manual. Creating a Global object lets me update it with any kind of variable (and method) through max messages (not inlets I believe). After the variable is updated, I can have an update funcion that doesn't take any arguments but calls the updated global variable and rebuilds the constructor:
myGlobal = new Global('global');
// now I can send a max message with arrays ; global 1 2 3
function updateGlobal(){
object = new myClass(myGlobal);
}
// this function is called anytime the myGlobal is updated to a new array
Here I share a simple approach how I usually handle lists, single variables and so on. The idea is basically that, I create different inlets for different trigger situations.
//define number of inlets
inlets = 4;
outlets = 0;
//global variables
var a = "";
var b = "";
var array = [];
//functions
function anything(){
arg = arrayfromargs(messagename, arguments);
if(inlet==0){
a = arg[0];
}
else if(inlet==1){
//if you send the message "a" to the second inlet, it is a single
variable
if(arg[1]==null){
b = arg[0];
}
//otherwise get value from the list
else{
b = arg[1];
}
}
else if(inlet==2){
array = arg;
}
}
//print to console
function test(){
post("var a: ", a,"\n");
post("var b: ", b,"\n")
post("array: ", array,"\n")
}
//reset variables, arrays
function reset(){
a = "";
b = "";
array = [];
}