Store reference to object in variable

Send us your feedback or ask for features.
Post Reply
Traxus
Regular
Posts:216
Joined:30 Nov 2012 06:19
Location:Detroit
Contact:
Store reference to object in variable

Post by Traxus » 21 Mar 2013 01:37

Alright, well, we've batted this idea around in enough threads to warrant its own. This seems like a hefty request but it would be absolutely huge in helping us abstract and refactor our code.

In short, we need to be able to do this:

Code: Select all

decl o = getobject();
Let me elaborate.

This is not the best example, as this implementation could be simplified by using switches, but bear with me, the principals are here.

Lets say I make an array of pads. I want to use those pads as select buttons. Hitting a pad will cause it to light up, as well as turn off whichever other pad was currently lit.

So we have our object 'Pads', and inside 'Pads' we make an expression, 'selected=0' for tracking which pad was last pressed, by which we can setup a script elsewhere in the template that will fire when 'selected' is changed.

We also make a script to control which pad is lit, 'setGlow(p)' within 'Pads' that fires manually:

Code: Select all

decl i;
for (i=0;i<=sizeof(x);i++) {
		if (i == p) {
				light[i] = 0.325;
		} else {
				light[i] = 0;
		}
}
Now we need another function, 'setSelected()' which will execute on any change of x. This will, as the name implies set which pad is selected, both by changing the 'selected' variable and lighting up that pad:

Code: Select all

decl i;
for (i=0;i<=sizeof(x);i++) {
		if (x[i] == 1) {
				selected = i;
				setGlow(i);
		}
}
So, simple enough. But what if I have this 50 or some times in my template, why should that be necessarily?


Why can't 'setGlow(p)' become a global script with 2 paramaters, 'setGlow(p,o)' ?

Code: Select all

decl i;
for (i=0;i<=sizeof(o.x);i++) {
		if (i == p) {
				o.light[i] = 0.325;
		} else {
				o.light[i] = 0;
		}
}
And then simply change 'setSelected()' to pass the current object into the global script?:

Code: Select all

decl i;
decl o = getobject()
for (i=0;i<=sizeof(x);i++) {
		if (x[i] == 1) {
				selected = i;
				setGlow(i,o);
		}
}
I understand this example is trivial and could possibly be worked out another way, but imagine the resources we could save by doing that! Forget(maybe) having to increase the max template size at the loss of FPS, let us code like adults!

dBlicious
Newbie
Posts:26
Joined:12 Jan 2013 06:49

Re: Store reference to object in variable

Post by dBlicious » 21 Mar 2013 04:28

It's true, you can't do this:

Code: Select all

decl obj = SomeObject;
obj.light = val;
but you can do this:

Code: Select all

decl obj = SomeObject;
setexpression(obj, 'light', val);
The same also applies for attributes. I attached a simple template that doesn't do exactly what you describe, just essential stuff.

Also, maybe you know this, but you often don't need to iterate through an array to pull out the useful information. Often you can pass the array as-is, or use firstof() and nonnull() to get at the useful bits. For the second set of pads in the attached template, I just assigned light to x--no function necessary. I know this wasn't what you were asking, so apologies if I you know this stuff. Just trying to help.

-A
Attachments
Object Reference Test.jzml
using a reference to an object in a script
(5.05KiB)Downloaded 125 times

Traxus
Regular
Posts:216
Joined:30 Nov 2012 06:19
Location:Detroit
Contact:

Re: Store reference to object in variable

Post by Traxus » 21 Mar 2013 05:23

ooooooooh :o

I knew there had to be some way to do this

Macciza
Regular
Posts:1325
Joined:07 Dec 2011 04:57
Location:Sydney, Australia.

Re: Store reference to object in variable

Post by Macciza » 21 Mar 2013 10:56

Hi
Thought I posted this earlier but it seems I didn't . . .
Slightly different approach and a couple of possible version taking different args,
Slightly wordier but with some other programming options made possible
Have a look
MM
ObjRef.jzml
(19.59KiB)Downloaded 134 times
iMac 2.8G i7 12G 10.6.8/10.7.2, Legacy Dexter/Lemur, Liine Lemur/iPad2, KMI SoftStep, 12Step & QuNeo , B-Controls, Mackie C4 etc
MaxMSP, Live Suite, Native Instrument stuff, etc Modified Virtual Guitar System etc All Projects/Modules © CC-BY-NC-SA[*][/b]

Traxus
Regular
Posts:216
Joined:30 Nov 2012 06:19
Location:Detroit
Contact:

Re: Store reference to object in variable

Post by Traxus » 21 Mar 2013 23:09

ah, findobject()

how did i forget that? I must have skimmed over it in the function reference list a dozen times.

Also looking forward to getting home and verifying whether set expression works for custom expressions...

Post Reply