View Full Version : Problem with Variables

08 August 2005, 04:49 PM
I just stumbled across this and cant seem to solve it...

float $a1 = 1;
float $b = 0;

$i = 1;

$b = ("$a" + $i);

How would I do it so that ("$a" + $i) actually equals the value of $a1? At the moment I just get:

// Warning: line 8: Converting string "$a1" to a float value of 0. //
// Result: 0 //

Any help would be appreciated!
Cheers, Dave

08 August 2005, 05:19 PM
I don't really understand what it is you want to do.

the line

$b = ("$a" + $i);

just tells maya to assign the string "$a" plus the integer $i (which has the value 0) to the variable $b. Since $b and $i are numbers, the string "$a" (note that "$a" is a string, i.e. a piece of text and not a variable) must also be converted to a number - you can't (mathematically) add a number to a text, that's meaningless. Since maya has no clue what number the string "$a" is supposed to mean, it automatically converts it to the number 0.0.

If you explain more clearly what you want to do we'll probably be able to help you.

08 August 2005, 05:33 PM
i wanted to use the variable $i to iterate through a bunch of variables (eg $a1, $a2, $a3, $a4,...)
so that if I say ("$a" + $i) (where $i = 1, ,2 ,3, 4....) it returns the actual value of the variable $a1 that I had declared previously...

so in my example (where it says
$b = ("$a" + $i);
) I would like $b to equal 1 (from my original declaration
float $a1 = 1;
near the top)

so basically I want ("$a" + $i) to equal the variable $a1
hmmm, its quite hard to explain, i hope this makes it a bit more clear?
Cheers Dave

08 August 2005, 05:34 PM
I think I know what you're trying to do. To make this work, you could use the following:

eval ("$b = $a" + $i);

eval will take the created string ("$b = $a1") and evaluate it as MEL. This avoids the early interpretation that you're facing.

Does that help?

08 August 2005, 05:45 PM
yeah, exactly what I wanted...
sorry for explaining it so badly...
Thanks alot!
Cheers, Dave

CGTalk Moderation
08 August 2005, 05:45 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.