coll and javascript?
I'm doing my first javascript attempt in max.
I wonder if it's possible to make an array ina javascript, and connect it to coll (or some other data storage object), so that I can write, read and change data in both in max world and javascript?
klif
as far as I can tell (I'd love to be wrong!!), you can't access coll contents directly in javascript. It would be cool if you had a coll named "mycoll" and then in javascript treat the coll like an array:
var myvar = mycoll[0];
to get the coll contents at index 0.
However, you can create a Global object to substitute for named coll. In someways, it's a bit more convenient.
Here's an kinda odd example that demonstrates creating a couple of different Arrays from incoming lists, then recalling that data elsewhere.
----makearrays1.js-----
/*
creates 2 different arrays from an incoming list. one is recalled by name, the other by symbol
*/
myglobal = new Global("data");
myglobal.array1 = new Array();
myglobal.array2 = new Array();
//initializes a couple of arrays in this Global object.
var index = 0;
function anything(){ //add items to arrays using a list input
var a = arrayfromargs(arguments);
myglobal.array1[messagename] = a;
myglobal.array2[index] = messagename;
post("list:",myglobal.array1[messagename],"n")
post("list header:",myglobal.array2[index],"n")
index++;
}
------------end makearrays1.js------------
------makearrays2.js----------
/*
recall array items that are made in makearrays1.js
*/
function anything(){
outlet(0,myglobal.array1[messagename]);
}
function msg_int(v){
outlet(0,myglobal.array2[v]);
}
------------end makearrays2.js---------
------patch!------
-----end patch------
Thank you for the reply Peter,
So basically I can build my own coll in javascript. It seems it will not be so difficult.
My head is still 100% in max, and I have to get used to different style now.
Klif
yeah, you can kind of make a coll. the one disadvantage that I've come across is that coll is nicer when you store data using a symbol, such as:
mammal,hair livebirth warmblood;
reptile,scale egg coldblood;
bird,feather egg warmblood
with coll, it's easy to query all the contents at once with the "dump" message. with a js Array, i'm not sure it's so easy! Normally, to get all the contents you could do something like:
for (var i=0;i
outlet(0,myarray[i]);
}
That's sort of the point in the example in my pvs. message. You can create an array referenced by literal strings, but to dump it, you need a parallel array that has the names indexed by integers, so you can use a for loop:
for (var i=0;i
outlet(0,myarray[myarraynames[i]]);
}
. Also, it takes an extra step of cognition to realize that you will need arrays of arrays to store lists of data!
P
On Feb 3, 2007, at 9:11 AM, pnyboer wrote:
>
> That's sort of the point in the example in my pvs. message. You
> can create an array referenced by literal strings, but to dump it,
> you need a parallel array that has the names indexed by integers,
> so you can use a for loop:
>
> for (var i=0;i
> outlet(0,myarray[myarraynames[i]]);
> }
Hey Peter,
Don't forget the for...in statement in JS.
http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Reference:Statements:for...in
for (var name in myarray) {
outlet(0,myarray[name);
}
Here's a great article I'd recommend to anyone building Coll
workalikes in JS ("asociative arrays" or "hash tables"):
Btw, as the above article mentions, it's recommended to use Object in
place of Array for this sort of associative array.
-Joshua
>Don't forget the for...in statement in JS.
hmm...I can't forget things I don't know! Thanks for the tip. Yes, using Object DOES make more sense than arrays. That link is a good explanation, as it is written by a hack like me :)