Forums > MaxMSP

Ordered [loadbang]

February 4, 2010 | 12:27 am

I’ve seen a few ways of doing this, one is a custom java external and one is a abstraction plus javascript that plays off the firing order of [patcherargs], [loadbang] and [loadmess] if I remember correctly.

Here’s a fairly simple javascript that uses a task and a global object to achieve much the same thing. You can create instances of the object and they will fire in the order of the numeric arguments you give to them. These must be in order starting from 1, no missed numbers or everything after will not fire and the task function will continue forever (which is bad). You can disable them like regular [loadbang]s with shift + command when you open a patch. I hope someone finds it useful. It would be fairly easy to implement a [loadmess] equivalent. Post any comments or questions here!

lh


// lh.loadbang.js

var loader = new Global("loader");
loader.num = 1;

var count = jsarguments[1];

var t = new Task(poll);
t.interval = 10;

function poll() {
	if (loader.num == count) {
		outlet(0,"bang");
		loader.num++;
		arguments.callee.task.cancel();
	}
}

function loadbang() {
	if (!max.loadbangdisabled) {
		t.repeat();
	}
}

autowatch = 1;

// EOF

February 5, 2010 | 2:27 am

New version! It still relies on crazy polling at the beggining but will now accept the order arguments being non-consecutive, negative and fractional. Any non-numeric arguments will be ignored, any extra arguments will be sent out as a list instead of a "bang" so you can use it as a [loadmess] substitute too. If you include more than one instance with the same argument they are triggered in order of creation (as far as I can tell).

I’m looking into ways of doing away with the short interval task object use as this could cause issues if you have many instances of the object in your patch.

lh


// lh.loadbang.js

var loader = new Global("loader");
loader.num = 0;
loader.all = [];

var inst = jsarguments[1];

var t = new Task(poll);
t.interval = 10;

function poll() {
	if (loader.all[loader.num] == inst) {
		if (jsarguments.length > 2) {
			outlet(0,jsarguments.slice(2));
		} else {
			outlet(0,"bang");
		}
		loader.num++;
		arguments.callee.task.cancel();
	}
}

function loadbang() {
	if (!max.loadbangdisabled) {
		if (typeof(inst) == "number") {
			loader.all.push(inst);
		}
		loader.all.sort(up);
		t.repeat();
	}
}

function up(a,b) {
	return a-b;
}

autowatch = 1;

// EOF

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