Page 1 of 1

Can I store references to objects in a variable?

Posted: 25 Dec 2012 18:47
by Traxus
Here is a sample of code I'm working with, it deals with a set of 3 encoders that i would like to all be set to the same value whenever one of them is changed...
This works as intended (and the new Z value on the knob helps a ton as to prevent conflicts) however it is feasiable to assume that I might add several more encoders to this setup, and being from a PHP background I immediately want to abstract and Loop.

Code: Select all

//Script setAll(l,v)

if (l != 1 && Platter.z != 1) {
		Outer.x = v;
}
if (l != 2 && Platter.z != 2) {
		Middle.x = v;
}
if (l != 3 && Platter.z != 3) {
		Inner.x = v;
}

Instead of hand coding each instance, I would like to, if possible, store a reference to a list of objects in an array, however this does not work:

Code: Select all

decl encoders;
encoders[1] = Outer;
//also tried the absolute path with no luck: encoders[1] = Platter.Outer;
encoders[2] = Middle;
encoders[3] = Inner;

if (l != 1 && Platter.z != 1) {
		encoders[1].x = v;
}
if (l != 2 && Platter.z != 2) {
		encoders[2].x = v;
}
if (l != 3 && Platter.z != 3) {
		encoders[3].x = v;
}
With the obvious benefit of storing it all in a for loop, giving me the ability to freely add additonal encoders:

Code: Select all

decl encoders;
		encoders[1] =Outer;
		encoders[2] = Middle;
		encoders[3] = Inner;
decl i;
decl size = sizeof(encoders);
for (i=1;1<=size;i++) {
		if (l != i && Platter.z != i) {
				encoders[i].x = v;
		}
}
However, the line ' encoders[1].x = v; ' does not validate. Is there any way to store a reference to an object, rather than accessing the direct ID of the object? Thanks.

Re: Can I store references to objects in a variable?

Posted: 25 Dec 2012 19:29
by nick_liine
Have a look at the getobject(), getfirst(), getnext() functions. You can create a loop and get references to objects in a single hierarchy level (ex: Faders in a Container). Once you have a reference, use getexpression() and setexpression() to get/set values.

Re: Can I store references to objects in a variable?

Posted: 25 Dec 2012 20:10
by Traxus
Ah, of course, this should help a bit, and per usual I was over complicating the work to begin with...

I'm a bit confused as to how I should get the total amount of objects within a container? Using sizeof(containername) only returns 1...

Code: Select all

//encoders as a global
//fire this onload to set the global array

encoders[1] = getFirst(platter);

decl current = encoders[1];
decl size; //???

for (i=2; i<=size; i++) {
encoders[i] = getNext(current);
current = encoders[i];
}
**edit: this question is a much less relevant to my initial intent following some other discoveries but I'm still curious as I can see using it in the future

Re: Can I store references to objects in a variable?

Posted: 26 Dec 2012 07:43
by lABl
Hi Traxus,

sizeof() works ofor arrays as far as I know, pads, multisliders, multiball, etc.

I have created a small script to return the amount of objects inside of a container, currently it's executed onload, so, it updates every time you add/remove an object, but you could change the execution mode for what you need.

Hope that helps,

Cheers,

getfirst() combined with getnext() are very powerful functions to simplify code for some tasks.

Cheers,
AB

edit: updated the file (just tweaked the monitor size)

Re: Can I store references to objects in a variable?

Posted: 26 Dec 2012 13:13
by lABl
here as an user function

Code: Select all

numobjects(<container_name>)
if somebody need it

cheers,

Re: Can I store references to objects in a variable?

Posted: 26 Dec 2012 21:50
by lABl
Hi,

I have tried an example with 3 knobs, not checked it too much, but hope it helps in some way.

Just I guess you are going not to use mass-spring mode for the knobs right? Just asking because using Z conditions will break the mass-spring behaviour, it would need other work around.

Here the file and code:

Cheers!

Code: Select all

decl i = 0;
decl touched;
decl contName = getattribute(getobject(),'name');
decl id = getfirst(findobject(contName));
while(id){

	touched[i] = getexpression(id,'z');
	
	if(touched[i])
	{
		index = i;
	}
	if(!touched[i])
	{
		setexpression(id,'x',{Knob1.x,Knob2.x,Knob3.x}[index]);
	}
	i++;
	id = getnext(id);
}

Re: Can I store references to objects in a variable?

Posted: 28 Dec 2012 15:34
by Traxus
lABl wrote:here as an user function

Code: Select all

numobjects(<container_name>)
if somebody need it

cheers,
Hey, thanks this is great, I was worried that using getNext() when there was not a 'next' would cause error but I guess not. I'm going to throw the meat of the code into the post for easier reference:

Code: Select all

//Returns the number of objects inside of a container. Antonio Blanca
decl objects = -1;
decl i = 0;
decl id = getfirst(container_name);
while(id){
	id = getnext(id);
	i++;
}
if(i < 1) objects = 'empty';//optional
else objects = i;
return objects;
I had not tried mass spring mode, nor was I aware that it broke the Z variable. If that's the case I'm certainly going to have to avoid it as the Z is critical for DJ platter behavior.