Can we share variables between js instances and patches ?

Jan 23, 2012 at 11:07pm

Can we share variables between js instances and patches ?

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

#61418
Jan 23, 2012 at 11:22pm

aint that what jsextensions is for?

#221545
Jan 23, 2012 at 11:41pm

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

#221546
Jan 23, 2012 at 11:41pm

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.

#221547
Jan 23, 2012 at 11:45pm

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.

#221548
Jan 24, 2012 at 11:42am

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 ?

#221549
Jan 31, 2012 at 11:55pm

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

}
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

Attachments:
  1. EventExample.zip
#221550

You must be logged in to reply to this topic.