FORUMS: list search recent posts

Expression question

VIEW ALL
 Expression question on Sep 8, 2010 at 2:57:55 pm

Hi everyone,

I picked up a new book recently with a bunch of great info on expressions..however I'm not sure I understand the advantages of defining variables vs. just assigning values....for example....

\$rand = rand(0.4, 1.5);
flyScaler = << \$rand, \$rand, \$rand >>

My question is, why not just say...

flyScaler=rand (0.4,1.5);

Thank you very much.

Best,
Craig

 Re: Expression questionon Sep 8, 2010 at 4:16:05 pm

There would be no point to assigning static values in an expressions. If that's what you're going to do, you don't need expressions, because you could just as well set an attribute's value directly. You use expressions you want to change the value of an attribute over time.

You could just use the functions directly, like this:

pCube1.translateX=rand(0, 1);
pCube1.translateY=rand(1, 2);
pCube1.translateZ=rand(2, 3);

Variables allow temporarily storing a value, perhaps for reuse later, but most importantly, they allow us to write cleaner, easier to maintain code. Where you can say; assign a value to an attribute, and figure out what that value is in some other part of the code, so that I can separate the two actions of finding and assigning the values.
So then we get

float \$tx = rand(0,1);
float \$ty = rand(1,2);
float \$tz = rand(2,3);

pCube1.translateX=\$tx;
pCube1.translateY=\$ty;
pCube1.translateZ=\$tz;

Now, I can change my expression to make my object move in circles rather than moving completely randomly by changing ONLY the part of the code where I find the translation values and leave the part where I assign them the same.

Like this

float \$tx = sin(time)*10;
float \$ty = rand(-0.2,0.2);
float \$tz = cos(time)*10;

pCube1.translateX=\$tx;
pCube1.translateY=\$ty;
pCube1.translateZ=\$tz;

This way way, you create code that is reusable, clean and easy to maintain.

Michiel
http://www.thnkr.com