Forums > Javascript

Can we share variables between js instances and patches ?

January 23, 2012 | 11:07 pm

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… :)

January 23, 2012 | 11:22 pm

aint that what jsextensions is for?

January 23, 2012 | 11:41 pm

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

January 23, 2012 | 11:41 pm

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() {

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.

January 23, 2012 | 11:45 pm

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.

January 24, 2012 | 11:42 am

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 ?

January 31, 2012 | 11:55 pm

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] = [];
	var fire = function(eventInfo) {
		if(_events[eventInfo.eventName] != undefined ) {
		for(i=0; i<_events [eventInfo.eventName].length; i++) {
				_events[eventInfo.eventName][i] (
					eventName : eventInfo.eventName,
					eventData : eventInfo.eventData
	var remomeListeners = function(eventName) {
		if(_events[eventInfo.eventName] != undefined ) {
			_events[eventName] = [];
		} else {
			_events = {};
	return {
	addListener : addListener,
	fire : fire,
	remomeListeners : remomeListeners
} ();

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");

	eventName: "myEvent",
	eventListener : firstListener

var fire = function (newVal) {
	post("fire called");
		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).

January 23, 2015 | 9:01 am

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.

January 23, 2015 | 9:36 am

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

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