More than one Global variable?
I was successful in passing messages to a global variable as per the example in the JS Tutorial. But when I put in a second Global variable, my code broke (specifically, Max can't execute function msg_int() ). Commenting it out fixes the code.
Can only one Global variable be declared per script?
______
Here is the script in question:
inlets = 2;
outlets = 4;
var vals = [0, 0];//input array
var d1 = 8000, d2 = 8000, d3 = 8000; //durations
var c1 = 1, c2 = 2, c3 = 3; //MIDI channels
var counter1 = 0, counter2 = 0, counter3 = 0; //counts through loop
var note1Area = false, note2Area = false, note3Area = false; //mouse coords that activate notes
//set pitches as variable values from outside of this script
//using Global (Max namespace) variables
glob = new Global("notes");
glob.p1 = 64;
glob.p2 = 80;
glob.p3 = 72;
glob.v1 = 100;
glob.v2 = 80;
glob.v3 = 127;
function msg_int(val) //checks for input, calls other functions
{
if (inlet == 1) {
vals[1] = val;
} else {
vals[0] = val;
noteAreas();
note1(val);
note2(val);
note3(val);
}
}
function noteAreas()
{
if (vals[0] > 100 && vals[0] < 300 && vals[1] > 150 && vals[1] < 350) {
note1Area = true;
} else {
note1Area = false;
}
if (vals[0] > 500 && vals[0] < 800 && vals[1] > 150 && vals[1] < 350) {
note2Area = true;
} else {
note2Area = false;
}
if (vals[0] > 1100 && vals[0] < 1400 && vals[1] > 150 && vals[1] < 350) {
note3Area = true;
} else {
note3Area = false;
}
}
function note1(val)
{
//post("gotit " + val);
//post();
if (counter1 == 0 && note1Area == true) {
outlet(3, c1); //outputs MIDI channel on right outlet
outlet(2, d1); //outputs duration on mid-right outlet
outlet(1, glob.v1); //outputs vel on mid-left outlet
outlet(0, glob.p1); //outputs pitch on left outlet - plays note
counter1++;
} else if (counter1 != 0 && note1Area == false) {
outlet(1, 0); //outputs 0 vel on middle outlet - turns note off
outlet(0, glob.p1); //outputs pitch on right outlet
counter1 = 0;
}
}
function note2(val)
{
if (counter2 == 0 && note2Area == true) {
outlet(3, c2);
outlet(2, d2);
outlet(1, glob.v2);
outlet(0, glob.p2);
counter2++;
} else if (counter2 != 0 && note2Area == false) {
outlet(1, 0);
outlet(0, glob.p2);
counter2 = 0;
}
}
function note3(val)
{
if (counter3 == 0 && note3Area == true) {
outlet(3, c3);
outlet(2, d3);
outlet(1, glob.v3);
outlet(0, glob.p3);
counter3++;
} else if (counter3 != 0 && note3Area == false) {
outlet(1, 0);
outlet(0, glob.p3);
counter3 = 0;
}
}
You can use more then one global variable and nothing is breaking here. Can you create a stripped down example to illustrate your problem?