Can I store references to objects in a variable?
Posted: 25 Dec 2012 18:47
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.
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:
With the obvious benefit of storing it all in a for loop, giving me the ability to freely add additonal encoders:
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.
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;
}
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;
}
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;
}
}