Can we share variables between js instances and patches ?

Julien Bayle's icon

Instead of "informing" my all JS instances that a variable just changed in the patch via a couple of objects, I'd like to "send" this value to them and just wanted to figure out if there was a way to do that.

If not, I'll go to my couple of objects, mates... :)

jamesson's icon

aint that what jsextensions is for?

DiGiTaLFX's icon

Depending on what you're doing, a JitterListener might be of some help? But that would be a very specific case I guess.

Luke Hall's icon

If you want to share variables between different [js] objects you can declare a variable in the global scope without using the "var" keyword.

For example, the following code will work a little bit like the [value] object. If you instantiate more than one of them in a patch you can see that the variable sent to one with a [number] box is available to the other when banged. This technique works between different [js] javascript files too.

global_var = 0;

function msg_int(x) {
    global_var= x;
}

function bang() {
      outlet(0,global_var);
}

However I'd recommend looking in to the Global object which is probably a better way to approach this task. It's explained pretty well in the documentation, but if you need to know anything else then ask away.

Julien Bayle's icon

Sorry, I didn't express my question well enough :-/

I have a value inside my patch.
This value changing, I want to inform my all JS instances.

This was the problematic.

Using global variable, I could have 1 specific JS instances ONLY dedicated to a particular global variable change, that ... would inform all the other instances :)

It answers. Testing it right now
Many thanks to all of you.

Julien Bayle's icon

It works fine.

I'd like to have your opinions/feelings about the efficiency of my architecture.

In order to "transmit" some informations/orders to MANY JS instances, I'll use the global object in which I'll "store variables"

Some JS instances require to be updated each time the value change.
Some others would only require one time some values.

I can update my global context realtime, then filtering in my different JS instances (for those which don't require so fast update)

Do you have other ideas ?

Jan M's icon

You can implement an own event system - if you want. The idea is that you can assign any function to an event (== eventListener), and you can fire this event from anywhere. If you fire an event some data that you sent to the event will be passed to the listener-functions and the function will be executed.

here is how i implemented it on my local machine.

1. you need to add a file with this code to the [...]Cycling '74Max 6.0Cycling '74jsextensions folder (to be loaded automatically on Max startup):

var jm = {};
jm.event = jm.event || function() {
    var _events = {};
    var addListener = function(data) {
        if(_events[data.eventName] == undefined) {
            _events[data.eventName] = [];
        }
        _events[data.eventName].push(data.eventListener);
    };
    var fire = function(eventInfo) {
        if(_events[eventInfo.eventName] != undefined ) {
        for(i=0; i

than you can add an eventListeners and fire events from any js object like this:

var firstListener = function (jmEvent) {
    post("firstListener -- ")
    post("eventName is :"+jmEvent.eventName);
    post(" eventData is :"+jmEvent.eventData +"n");

}
jm.event.addListener(
    {
    eventName: "myEvent",
    eventListener : firstListener
    }
);

var fire = function (newVal) {
    post("fire called");
    jm.event.fire(
        {
        eventName : "myEvent",
        eventData : newVal
        }
    );
}

attached is an example patch and the .js files (jm.event.js goes into the jsextensions folder before you start Max).
Jan

3288.EventExample.zip
zip
orange_glass's icon

Hi Does anyone know if this global object is in max for live across multiple devices in the same set? I'm guessing not but thought I'd ask.

Jan M's icon

global objects are shared between all js instances also inside different devices.

Jay Walker's icon

hello is this only for Max 6? I'm having difficulty finding the right folder to put this in, I tried Documents/Max 7/Packages, and ~/Library/Application Support/Cycling '74/ and /Applications/Max.app/Contents/Resources/C74/extensions thx!

Bradley Korth's icon

I found that the Global object does indeed work to share variables between patchers. I found this useful today!