Scripting multiple arguments to (eg) route

Dec 29, 2009 at 1:02pm

Scripting multiple arguments to (eg) route

This is probably another dumb question, but I haven’t managed to find a solution in the tutorials or by futzing with the script …

The first thing I want my little script to do is to create a route object with 6 outputs, which are numbered according to an int sent in the first outlet of js

(the input message is called (offset) )

{
x = (jsarguments[3]);
z = (jsarguments[4]);

//args 3 & 4 are the patcher location for the [route]. args 1 & 2 are the objects that the route will connect to

list = (1 + offset);
obj = this.patcher.newdefault(x,z,”route”,list);
}

So far, this creates a route object with an argument of the input value plus 1. Based on this I could script separate [route]s for each of the range of input values, but what I want is a single [route 1 2 3 4 5 6] (or whatever). I guess the question is – how do I get an array of values into the newdefault part of the script.

thanks

David

#47465
Dec 29, 2009 at 1:11pm

Ah. I found a way to do it. Not very elegant though – is there a better way?

{
x = (jsarguments[3]);
z = (jsarguments[4]);
list = (offset);
obj = this.patcher.newdefault(x,z,”route”,list,list+1, list+2,list+3,list+4,list+5);
}

#170797
Dec 29, 2009 at 2:13pm

Hmm, turns out the output connections are harder! I have 6 named objects (smooth1-6) that I want to connect the outputs of the scripted [route] to. Currently the only way I can get it to work is to have the script name of one of the destination objects as argument[2] to the js object. That allows me to connect outlet 0 of route to inlet 0 of smooth1 (where I’ve used smooth1 as the argument to js).

I’ve tried creating the destination in the script – target_c = (jsarguments[2]) + 1; and then using target_c in the connection script – but it doesn’t do anything. (I figure if I can get that working, then I can replace the + 1 with + i, and increment the i in a loop).

It seems that an argument[2] (eg smooth1) isn’t the same (as far as the connection script is concerned) as argument[2] + 1 (ie smooth + 1 … smooth1).

What am I missing?

function msg_int(offset)
{
target_a = this.patcher.getnamed(jsarguments[1]);
target_b = this.patcher.getnamed(jsarguments[2]);
var x = (jsarguments[3]);
var z = (jsarguments[4]);

{
x = (jsarguments[3]);
z = (jsarguments[4]);
list = (offset);
target_c = (jsarguments[2]) + 1;

post(target_c);

obj = this.patcher.newdefault(x,z,”route”,list,list+1, list+2,list+3,list+4,list+5);
this.patcher.connect(target_a,0,obj,0);

this.patcher.connect(obj,0,target_c ,0);
}
}

#170798
Dec 29, 2009 at 4:37pm

Well, it turned out simpler than I thought, after a bit of digging in the help files. I’d still be interested to know if there’s an iterative way of doing this.

function msg_int(start)
{
i = start
p = this.patcher;
a = p.newdefault ( 260, 190,”route”, i, i+1, i+2, i+3, i+4, i+5);
b = p.getnamed(“smooth”);
c = p.getnamed(“smooth1″);
d = p.getnamed(“smooth2″);
e = p.getnamed(“smooth3″);
f = p.getnamed(“smooth4″);
g = p.getnamed(“smooth5″);
z = p.getnamed(“zlrev1″);

p.connect (z,0,a,0);
p.connect (a,0,b,0);
p.connect (a,1,c,0);
p.connect (a,2,d,0);
p.connect (a,3,e,0);
p.connect (a,4,f,0);
p.connect (a,5,g,0);

}

#170799
Jan 1, 2010 at 9:48pm

Here’s one way of going about it using for() loops. It assumes you’ve named the objects you want to connect to dest[0] to dest[5] but you could always change the formatting. It uses another loop to create an array of the arguments you want rather than declaring all the additions seperately.

lh


function msg_int(num) {
    p = this.patcher;
    steps = [];
    for (i=0; i<5; i++) {
        steps.push(num+i);
    }
    obj = p.newdefault(260,190,"route",steps);
    for (i=0; i<6; i++) {
        target = p.getnamed("dest["+i+"]");
        p.connect(obj,i,target,0)
    }
}
#170800

You must be logged in to reply to this topic.