Dereferencing Operator

Topic: version 4 MAXScript Language Improvements/Language Improvements

Visible Class For '&' Reference Values

A Function Published dereferencing prefix operator, *, has been added to MAXScript. This can be used in conjunction with the new '&' reference operator that was added to support pass-by-reference arguments to functions and interface methods.

The '&' operator is used to get a 'reference' to a variable or property or array element slot in MAXScript. You can pass this reference in to a function that accepts by-reference arguments, allowing its code to directly assign values to the referenced variable or property or array element, thus supporting a kind of multiple-value return.

The new '*' operator allows you to work with '&' reference values anywhere in MAXScript code. Therefore 'dereference' the reference and get the value in the slot or variable it is referring to and to assign it to the referenced slot.

Examples:

ref = &$foo.pos

this puts a value into ref which is a reference to the .pos property in the scene object $foo. Elsewhere in the code, you could use the new '*' dereferencing operator to say:

$baz.pos = *ref

the '*' dereferences the reference value in ref, essentially making this equivalent to:

$baz.pos = $foo.pos

you can also use this construct as a destination for an assignment:

*ref = [10,0,0]

first dereferences the reference in 'ref' to $foo.pos and then assigns to that, effectively setting $foo's position.

In order to avoid ambiguity with the '*' multiply operator, the precedence of the '*' dereferencing operator is set at just lower than the function call and just higher than the 'as' coercion operator. This means that if you want to send a dereferenced value into a function as an argument, you must surround the dereferencing with parentheses, as in:

foo (*ref) x y z

Remember that reference values can be generated for variables, properties and array elements, as

Example:

&foo

&$box.position.x

&valArray[23]

The '*' dereferencing operator is a moderately advanced feature in MAXScript that is often used to allow very general purpose code to be written that uses references instead of actual objects so that it can be applied to different kinds of objects and properties at different times.

Nested property access allowed in '&' reference values in MAXScript. This allows constructs like: r = &$foo.pos , and later *r.controller to mean $foo.pos.controller.

See also