If you use Director 3.1.3 or earlier you obviously have no alternative but to use factories, but for users of Director 4 there is no reason to use them, and parents offer the advantage of automatic disposal. Also, some people have reported that factories' memory management can be unreliable. In short, ditch those factories now!
The syntax for factories is somewhat different, but their functionality is very similar to parent scripts, so it may be useful to read the previous sections for some background. I'm afraid this is gonna be pretty cursory.
A factory is declared in your movie script as follows :
factory someFactoryName
method mNew
instance blah, whatever, etc
-- initialize the object as you wish
end mNew
method mSomeOtherMethod someArgument
-- do whatever it is you want to do
end mSomeOtherMethod
The factory declaration identifies the subsequent list of methods as
belonging to the factory called someFactoryName. This is equivalent to
naming a parent script.
The mNew method tells the factory how to initialize a newly created object. This is equivalent to a parent script's birth handler. Note that mNew neither takes "me" as a parameter nor returns it. Factories do, however, support a slightly different usage of a special keyword "me", described below.
Any further methods equate to the other handlers in a parent script. They implement whatever functions you wish your objects to perform.
The "instance" statement declares blah, whatever and etc as instance variables, which are the equivalent of properties: variables which have independent values for each object created.
A method may call other methods within the same object by using the keyword "me" to refer to itself :
method mYetAnotherMethod
-- do a few things first
me(mSomeOtherMethod)
-- etc
end mYetAnotherMethod
Objects are created from a factory by using a statement like :
put someFactoryName(mNew) into myObjectand their methods are called by statements like
myObject(mSomeOtherMethod, "this value is passed in someArgument")The most important difference between factories and parents is that objects spawned from factories must be explicitly disposed of using the built-in mDispose method:
myObject(mDispose)Factories also provide two other built-in methods, mPut and mGet, which support arrays, the precursor to Director 4 lists. All factory objects automatically have associated with them an array of unspecified size which can contain values of any type, including other objects. Values are placed into the array by using :
myObject(mPut, index, value)and retrieved using :
put myObject(mGet, index) into someThingOrOtherFactories and arrays provide a useful way to implement data structures like stacks and tables in Director 3.1.3 and earlier, but with Director 4 it is invariably more convenient to use lists and parent scripts.