Dereferencing Operator

Visible Class For '&' Reference Values

A Function Published dereferencing prefix operator, *, is available in MAXScript since 3ds Max 4.

It can be used in conjunction with the '&' 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 '*' 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.

EXAMPLE:

The following puts a value into ref which is a reference to the .pos property in the scene object $foo.

ref = &$foo.pos

Elsewhere in the code, you could use the * dereferencing operator to say:

$baz.pos = *ref

The * above dereferences the reference value in ref, essentially making the expression equivalent to:

$baz.pos = $foo.pos

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

*ref = [10,0,0]

The above 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,

FOR EXAMPLE

foo (*ref) x y z

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

FOR 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 is allowed in & reference values in MAXScript.

EXAMPLE:

This allows a construct like

ref = &$foo.pos

and later in the code

*r.controller

to mean the same as

$foo.pos.controller

See Also