Using Director > Parent Scripts > Creating a child object

 

Creating a child object

Child objects exist entirely in RAM; they are not saved with a movie. Only parent and ancestor scripts exist on disk.

To create a new child object, you use the new function and assign the child object a variable name or position in a list so you can identify and work with it later.

To create a child object and assign it to a variable, use the syntax:

set variableName = new(script "scriptName", argument1, argument2, argument3...)

where scriptName is the name of the parent script and argument1, argument2, argument3... are any arguments you are passing to the child object's on new handler.

The new() function creates a child object whose ancestor is scriptName. It then calls the on new handler in the child object with the specified arguments.

You can issue a new statement from anywhere in a movie. You can customize the child object's initial settings by changing the values of the arguments passed with the new statement.

Each child object requires only enough memory to record the current values of its properties and variables and a reference to the parent script. Because of this, in most cases you can create and maintain as many child objects as you require.

You can produce additional child objects from the same parent script by issuing additional new statements.

To create child objects without immediately initializing their property variables, use the rawNew() function. The rawNew() function does this by creating the child object without calling the parent script's on new handler. In situations where large numbers of child objects are needed, rawNew() allows you to create the objects ahead of time and defer the assignment of property values until each object is needed.

This statement creates a child object from the parent script Car without initializing its property variables and assigns it to the variable car1:

car1 = script("Car").rawNew()

To initialize the properties of one of these child objects, call its on new handler:

car1.new