Movie Script, Parent Scripts and Behaviours

Script cast members

All Director cast members have a "Type" property. Director has built in support for a range of cast member types: #bitmap, #shape, #field and so on. Some of these cast member types, such as #quicktimeMedia and #Flash, are enabled by Xtras. Some third party Xtras allow for additional cast member types. For example, the OSControls Xtra creates cast members with type #OSBox and #OSpopupMenu.

Movie scripts, parent scripts and behaviours are all cast members with the type #script. You can create a reference to a script cast member like by using the "member" key word:

 ScriptCastmemberReference = member("ScriptCastMemberName")

put ScriptCastmemberReference

-- (member 3 of castLib 1)

put ScriptCastmemberReference.type

-- #script

Script cast members have have a 'scriptType' property. Movie script cast members have the scriptType #movie, behaviour cast members have the scriptType #Score and parent script cast members have the scriptType #Parent. This scriptType property is important for two reasons:

(1) It determines the implicit 'scope' of the script object created from the script cast member (movie scripts make script objects with an implicit global scope; behaviours and parent scripts create script objects with a limited scope)

(2) In authoring mode, it determines whether or not the script cast member can be added to a sprite (behaviours can be dragged onto sprites to trigger the automatic 'add behaviour to sprite scriptList and generate MUI dialog to set specified properties' process; the other script types cannot be dragged on to sprites, though they can be added to the scriptList via lingo).

Script cast members also have a scriptText property. The scriptText is the lines of lingo code that are executed when the movie is run (or you execute a command in the message window). In order for the lingo code to be executed, a 'script object' is created from the script cast member. This is done automatically for movie scripts when the movie is run but must be done explicitly for parent scripts and behaviours using the "script" keyword.

Script Objects

All three script cast member types create 'script objects'. To create a reference to a script object, you use the "Script" key word like this:

ScriptObject = script("ScriptCastMemberName")

put ScriptObject

-- (script "ScriptCastMemberName")

This script object is a fully fledged object that can respond to messages that are sent to it (events such as #mouseDown and custom messages such as #DoThisHandler). Once the script object is created, there is no difference between the script objects created from the different scriptTypes. They can all have properties and methods to respond to messages. There is, however, a signicant difference how Director sends messages to script objects and how they are created.

Director creates its own special reference to movie script objects. If you do not specify the script object you want to respond to the message, Director will assume the message is intended for a script object made from a movie script and will send all the message to all the movie scripts objects it has created until it finds one that can respond to the message. When it sends the message to the movie script object in this fashion, it does not include any special parameters. However, if you create an explicit reference to the script object and send a message to the object using this reference, Director will include the object reference.

To illustrate this, create a movie script cast member called "aScriptName" and give it the following scriptText:

on HandlerName scriptObj

  put #HandlerName, scriptObj

end

If you type movieHandler into the message window, you will get this:

HandlerName

-- #HandlerName <Void>

Now, if you create an explicit reference to the script object and send a message to the object, you will get this

scriptObject = script("aScriptName")

scriptObject.HandlerName()

-- #HandlerName (script "aScriptName")

In the first example, we did not specify the script object containing the handler we wanted to call - so Director assumes it was intended for one of the script objects it created from a movie cast member and automatically sent the message to that object. In this case, it does not pass the object reference as a parameter. In the second example, we have specified which script object to send the message to. In this case, Director includes a reference to the target object as a parameter.

As an experiment, change the movie script member's script text to this:

on HandlerName scriptObj

  put "nothing here, move along", scriptObj

end

Click 'recompile', and then in the message window, type "HandlerName" and hit return. You will see this

HandlerName

-- "nothing here, move along" <Void>

Now, the scriptText has changed and when we called the handler without reference to the script object, Director sent the message to new movie script object it created when we hit "compile". But if we send a message to the script object we created before, we will see this:

scriptObject.HandlerName()

-- #HandlerName (script "aScriptName")

In this example, the script object which is actually executing the code hasn't changed. It exists independantly of the script cast member (even though the 'scriptText' of the cast member has changed, the object hasn't changed).

Now this time, change the scriptType of the movie script to #frame (behaviour) or #parent and change the script text to this:

on HandlerName scriptObj

  put "I am a script with limited scope"

end

In the message window, type "HandlerName" again. You will see this

HandlerName

-- #HandlerName<Void>

How can this be? Your movie has no movie script cast members, so who is responding to this message? The answer is the movie script object you created earlier is answering. Any variables you create in the message window are assumed to be global variables, so the reference to the movie script object we created earlier with the line "scriptObject = script("aScriptName")" is still alive and well. And because this object was created from a movie script object, Director will use it to respond to the "HandlerName" message we sent from the message window.

In the message window, type this:

scriptObject = VOID

HandlerName

This time you will get a "handler not defined error" because there are no movie script objects available to respond to the "HandlerName" message. There is a script cast member with a "HandlerName" handler defined in its script text, but since this script cast member is not a movie script, Director does not automatically create a script object from the cast member. Even if we had explicitly created an script object from this parent script or behaviour cast member, this script object still would not respond to the "HandlerName" message because we have not explicitly sent the message to this script object. To get the script object created from the parent script/behaviour cast member, we need to specify that script object as the target for the message:

scriptObject = script("aScriptName")

scriptObject.HandlerName()

-- #HandlerName (script "aScriptName")

To summarise, the difference between script objects created from movie script cast members and script objects made from parent scripts or behaviour cast members is (1) Director will only automatically create script objects from Movie script cast members; and (2) Director will automatically send 'un-targeted' messages to script objects made from movie scripts, not to script objects made from the other script types (though it will try to send the message to the object that originated the message as well).

Ok, cast members have scriptText. The cast member is used to create a script object which can execute the code defined by the cast member's script text. Script objects execute code by responding to messages. Director automatically creates script objects from movie script cast members and will send any 'untargeted messages' to script objects created from movie script cast members. Script objects created from parent scripts and behaviours need to be manually created and they will only respond to messages explicitly sent to them... Confuse me some more, please. Whats the difference between script objects and 'child objects' (script instances)?

Often you will want many 'versions' of a script object with each version having some minor differences. For example, one script object might be working with sprite 1, changing its properties (such as the cast member associated with the sprite), and another version of the script object is working with sprite 2.

Instances of script objects are created using the "new" command. Each new instance of a script object is a completely new object and can have its data stored in its properties (and can inherit different methods from a different 'ancestor' object).

The following illustrates the difference between creating a reference to a 'script cast member', a 'script object' and a new 'instance' of that script object:

scriptmember = member("aScriptName")

scriptObject = script("aScriptName")

instanceObject = script("aScriptName").new()

Each new instance object is created from the script cast member, but once it is created, the new instance object is completely independant of the the script object and the original script cast member.

First published 07/06/2005