Assignment

 

 

Use the assignment operator to set the value for a variable. The assignment operator is represented by an equal sign (=). The variable to the left of the equal sign is assigned the value to the right of the equal sign.

For example:

float $counter = 5.3;

declares a float variable and assigns it a value of 5.3.

 

In MAXScript, the same operator (=) is used to perform the value assignment. The main difference to MEL is that the left-hand side is type-free, so the value type of the right-hand side is enforced over the left-hand side.

 

counter = 5.3

declares a type-free variable and assigns it a value of 5.3, turning it into a float variable .

 

 

Assignment value

 

The result of the assignment operator is the type and value assigned to the variable on the left side of the equal sign. This result can be used as a constant of the same type and value.

 

Example

float $owl;

float $hotdog = ($owl = 5.3) + 6;

 

In the second line of this example, the float $owl is assigned a value of 5.3 and $hotdog is assigned a value of 11.3.

 

 

Same applies to MAXScript - the result of the assignment is the value that has been assigned.

Other than in MEL, there is no need for an explicit variable declaration before the variable can be assigned a value and used in expression:

 

hotdog = (owl = 5.3) + 6

 

In this line, the variable own is first assigned the float value 5.3, then 6 is added to the result of the assignment expression in the parentheses, then the result of the addition is assigned to the variable hotdog, turning it into a float variable.

 

Chained assignment

 

Because an assignment has a value and the operation is assigned the value to the right of the operand (see Operator precedence), you can use chained assignment.

 

Example

int $i1, $i2, $i3, $i4;

$i1 = $i2 = $i3 = $i4 = 6; // All variables assigned 6

 

Same applies to MAXScript - you can perform chained assignments:

 

i1 = i2 = i3 = i4 = 6 -- All variables assigned 6

 

Because MXS is type-free, the actual type of the values in the variables on the left-hand side does not matter - they all will end up with the type and value on the right-hand side:

i1 = 1.5

i2 = "Bobo"

i3 = [10,20,30]

i4 = undefined

i1 = i2 = i3 = i4 = 6 -- All variables assigned 6, old types and values are overwritten