Forums > Javascript

access data from a different subpatcher?

February 5, 2010 | 8:18 pm

I’m writing a javascript function in one of my subpatchers that needs to grab an array from js files in several other subpatchers. Basically I need to route the function call up to the parent patcher and then back down to a different subpatcher. What’s the syntax for doing that? I saw some forum threads with a convoluted way to do so in 4.6 which hinted that there would be a much easier way to do so in 5. Is that true? What’s the best syntax to use to perform this action? Thanks!


February 5, 2010 | 9:21 pm

Just use globals rather than local vars. Javascript globals in Max are truly global – i.e. they can be accessed in any js script.


February 5, 2010 | 9:58 pm

Thanks, I was hoping such a thing was possible. From the tutorial on globals, I got the impression they were only good for sending messages between patchers, not accessing variables. Could you provide an example of how to, for example, declare a global array, and then access it?


February 5, 2010 | 11:02 pm

If I remember correctly you just would declare your array without the var prefix in one script, then access it by name in the other.

foo = new Array[];

—————-

var localVar = foo[x];


February 5, 2010 | 11:50 pm

Wow – well that’s pretty cool. Wish I’d known about that a while ago :-) Thanks for the help Jesse!


February 6, 2010 | 10:38 pm

I would recommend going the Global object route. it’s the cleanest way. Omitting the var keyword is more like a hack.


April 30, 2010 | 8:54 pm

What ej means is of course: create one unique object with the name of your project and use its properties. All globals are dangerous, but one is better than many.


April 30, 2010 | 9:08 pm

This is the most concise and complete discussion of local/global scope in Javascript I’ve found.

http://www.irt.org/script/1321.htm

Agreed that it’s cleaner to create a single global object and set/get its properties in all scripts. But it’s still useful to know the implications of the var keyword within functions.


May 1, 2010 | 7:52 pm

I meant the Global object which is a standard Max / JS way of sharing / setting data between Max and JavaScript.


May 4, 2010 | 3:29 pm

Emmanuel, that the docs of that object is not visible from the Javascript in Max page. I see that the "Global object" link erronously points to
http://www.cycling74.com/docs/max5/vignettes/js/jsglobal.html instead of http://www.cycling74.com/docs/max5/vignettes/js/jsglobalobject.html.

That explains why I never heard about it ;)


Viewing 10 posts - 1 through 10 (of 10 total)