Structure Definition

You can create your own compound values in MAXScript using Structure definitions.

Structure definitions let you define the layout of new 'classes' of values that you can then create and work with in your code.

The syntax for a structure definition is:

struct <struct_name> ( <member> { , <member> } )

where each <member> can be one of:

<name> [ = <expr> ] --name and optional initial value
<function_def>

EXAMPLE

   struct person (name, height, age, sex)

The above example defines a new 'person' class. You can create values of this class using the 'person' constructor:

EXAMPLE

   bill = person name: "Bill" height:72 age:34 sex:#male

This creates an instance of the person class, storing the instance in variable bill . Member name is initialized to the string value "Bill", height to the integer value 72, age to the integer value 34, and sex to the name value #male.

EXAMPLE

   joe = person name: "Joseph" sex:#male

This creates an instance of the person class, storing the instance in variable joe . Member name is initialized to the string value "Joseph" and sex to the name value #male. As the height and age members are not assigned values and do not have optional initial values supplied in the structure definition, they default to a value of undefined .

You can access structured values using the standard property accessing syntax in MAXScript.

EXAMPLE

   bill.age  --returns 34
   joe.age  --returns undefined
   joe.age = bill.age-4 --assigns Bill's age less 4 to Joe's age.

Structure definitions are useful as an alternative to arrays when you have a fixed, usually small number of independent components and the code to work with them is much clearer when they can be referenced by property name, rather than by index number.

As with function definitions, a structure definition actually creates a value to represent the definition and stores it in a variable of the same name as the structure. You can store the structure definitions in other compound objects or pass them as function arguments. The classOf() function returns this structure definition value when applied to these values, so you can use it to test the definition of structure instances at runtime.

EXAMPLE

   classOf bill --returns person

The struct value constructors are just like function calls and take positional argument initializers as well as keyword initializers. The elements of the new struct are filled in order from any positional arguments and then by name from any keyword arguments.

FOR EXAMPLE,

given the following struct definition:

   struct foo (name, age, height, hair="brown")

you can create instances in several ways:

   f1 = foo "bill" 23 72 --fills in name, age, height in order
   f2 = foo age:45 name: "sam" --random named initializers
   f3 = foo "mary" hair: "red" --first name, then keywords

There are several methods associated with struct values:

getProperty <struct> <propname>

Allows you to access properties of the structure.

getPropNames <struct>

Returns the properties of the structure as an array.

copy <struct>

Creates a copy of the structure. The copy made is called a shallow copy. Only a copy of the upper-level value itself (that is, the struct value) is created. Copies are not made of compound values stored in the structure, rather the same compound value is stored in both the struct values. This can be seen in the following example, where changing a component value of a compound value in a copy of a struct value also changes the value in the original struct value:

SCRIPT:

   Struct test (pos1, pos2) --define a structure
   testval = test [1,2,3] [4,5,6] --create struct value
   testval_copy=copy testval --copy the struct value
   testval_copy.pos1.x=10 --change component value of a compound
   --value in the copy
   testval --look at original struct value

OUTPUT:

   #Struct:test( --result line 1
     pos1:<data>; Public,
     pos2:<data>; Public)
   test(pos1:[1,2,3] pos2:[4,5,6]) --result line 2
   test(pos1:[1,2,3] pos2:[4,5,6]) --result line 3
   10 --result line 4
   (test pos1:[10,2,3] pos2:[4,5,6]) --result line 6