access data from a different subpatcher?

dagfooyo's icon

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!

Jesse's icon

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

dagfooyo's icon

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?

Jesse's icon

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];

dagfooyo's icon

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

Emmanuel Jourdan's icon

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

Hans Höglund's icon

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.

Jesse's icon

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

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.

Emmanuel Jourdan's icon

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

Hans Höglund's icon

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
https://cycling74.com/docs/max5/vignettes/js/jsglobal.html instead of https://cycling74.com/docs/max5/vignettes/js/jsglobalobject.html.

That explains why I never heard about it ;)