Director FAQ || Notes ~ Short Index ~ Full Index ~ Search || Prev ~ Next ||

[10] Lingo


Director FAQ [10] Lingo

[10.1] What's the difference between score, movie and cast scripts?

The principal difference between script types is scope - that is, whereabouts in your movie they can be invoked, what events typically are sent to them, and the order in which events are received.

Score scripts can be attached to any cells or frames in the script. They appear in the pop-up Script menu at the top left of the score window (when the score is set to show the script menu, that is). Handlers contained within them are only accessible to ordinary events while the frames in which they appear are playing.

Movie scripts and the handlers they contain are globally accessible - they can be called from anywhere in the movie, by score script handlers, cast scripts, other movie scripts or events.

Both score and movie scripts are elements in the cast in their own right. You can choose whether a script is a score or movie script from the pop-up menu in the script info dialog box.

Cast scripts do not appear as separate cast members in themselves, but only as adjuncts to the bitmap, button or whatever they are attached to. They appear in the script window as "Script of cast member x". In general they respond only to events passed to the cast member they are attached to.

Messages normally pass through scripts in the order specified in the "Lingo Messaging Hierarchy" diagram on the back cover of the Lingo Dictionary, stopping as soon as they find a script with a handler that can use them. They can be made to continue down the hierarchy by using the pass command:

-- this is a sprite script
  
on mouseUp
  -- do some sprite things first here
  mySpriteStuff
  -- then pass it on
  pass
end mouseUp

This will send the mouseUp message along to the cast, frame and movie scripts once the sprite is done with it.

Although messages will only normally be passed in the order specified in the messaging hierarchy, it is possible to send messages directly to a specific script via lingo, even if that script would not normally receive such a message, using the script keyword:

  message(script scriptName [, params...])

For example, suppose you wish to invoke the mouseUp handler in a castmember that isn't presently onstage. If the cast's name is "blah", then:

  mouseUp(script "blah")

will do just that. This kind of thing should be done with care, however. In general, it's sensible to place your scripts in the appropriate places in the messaging hierarchy for the things you want them to do.

[top]


Director FAQ [10] Lingo

[10.2] So where should I put this handler?

Depends on what it's doing and how you want it to behave. The relevant guidelines are succinctly given under the heading "Strategies for placing handlers" in chapter 3 of "Using Lingo" (pp.84-85).

The rule of thumb is, place a handler in a script that's just far enough down the message-passing hierarchy to catch all the messages you want it to catch and none of those you don't. In practice, this usually means defining default catch-all responses in movie scripts, default button behaviours in cast scripts, and frame-specific actions in frame and sprite scripts.

Only a few cases are set in stone: startMovie & stopMovie handlers must be in movie scripts, because when they are executed there is no current frame. Parent scripts should usually be in score or cast scripts to hide their methods to calls that don't use their offspring. And all the generic handlers you want to be able to call from anywhere in your movie need to go in movie scripts.

[top]


Director FAQ [10] Lingo

[10.3] Should I use "pause" or "go to the frame" to stay in one place?

[top]


Director FAQ [10] Lingo

[10.4] Is it better to use the visible property or move sprites offstage?

[top]


Director FAQ [10] Lingo

[10.5] I declared this variable global, but now I can't access it. Why?

Global variables must be declared in every script that uses them. For instance, if your movie script contains the following:

global helpFrame

on startMovie
  set helpFrame = 100
end startMovie

and you have a button in frame 20 that needs to use the value of helpFrame,

on mouseUp
  go to helpFrame
end mouseUp

is not sufficient in the sprite script (you'll probably get a warning like "Variable used before assigned a value" when you compile the script). You need to add

  global helpFrame

at the top of this script as well before it will work.

[top]


Director FAQ [10] Lingo

[10.6] These sprites share the same script, how do I know which one got clicked?

In lieu of a sensible "me" keyword, Director provides the clickOn function to tell you which sprite has been clicked. Suppose you have a bunch of buttons which change colour when clicked. They could all share the same script, something like this:

on mouseUp
  put the clickOn into me
  set newColor = (the foreColor of sprite me + 1) mod 256 
  set the foreColor of sprite me = newColor end mouseUp

The first thing the script does is determine which sprite it has been called for, and then it acts on that sprite.

[top]


Director FAQ [10] Lingo

[10.7] This frame has no script, why does tracing show up an exitFrame handler?

Messages such as enterFrame and exitFrame are sent through the messaging hierarchy just like mouseClicks are. If your frame doesn't have a handler for them, the messages will be passed on to the movie script(s). Any stray exitFrame handler in a movie script will therefore be executed by default for any frame that doesn't have a handler of its own.

Tracing should show up what script the handler occurs in. You can either modify that script to stop the default behaviour (either removing the handler altogether or changing the script type to "score"), or, if you want to retain the default behaviour in other cases, put a handler such as:

on exitFrame
  dontPassEventf17
end exitFramef17

in the script channel for that frame to intercept the exitFrame message.

[top]


Director FAQ [10] Lingo

[10.8] I need to stop mouseclicks getting to my sprites for a while. How?

[top]


Director FAQ [10] Lingo

[10.9] Can I send events to a specific object onstage?

Events can be sent as messages to any script using the syntax:

  message(script scriptName, any, required, parameters...)

For example, if you had a button called "help button" with a mouseUp handler in its cast script, you could cause its handler to be called by using:

  mouseUp(script "help button")

(Note that you can also use cast numbers instead of names to specify the script, but in most cases it's safer to use names.)

While this is not strictly the same thing as sending an event to an object onstage (for instance, a button won't hilite on receiving this message), it nevertheless allows for most of the things you would want to do this way, since the actions of objects interacted with will usually be controlled by scripts.

[top]


Director FAQ [10] Lingo

[10.10] How do I assign functions to specific keys or key-combinations?

[top]


Director FAQ [10] Lingo

[10.11] How can I simulate a switch/case statement in Lingo?

Instead of using huge rafts of nested if/then statements, many languages provide a structure that allows multiple branching based on the value of a variable (eg, "switch" in C or "case" in Pascal). Lingo, unfortunately, does not provide such a structure, but you can simulate it using lists.

Eg:

-- to switch between a series of actions depending on the value of 
-- our variable switchVar. This example assumes switchVar will have a 
-- numeric value, but you can use other values instead

  set actionsList = [ 0: "zeroAction", 1: "oneAction", 2: "twoAction",  ]
  do string(getAProp(actionsList, switchVar)) 

Note the outer "string" call. This is because, if switchVar contains a value that isn't one of the list's properties, getAProp returns . String converts this to "" (an empty string), which can be safely passed to the do command without causing an error.

An analogous structure can be used to do conditional assignments. Rather than use a bunch of separate checks:

  if x = 1 then set y = "one"
  else if x = 2 then set y = "two"
  else if x = 3 then set y = "three"
  else if x = 4 then set y = "four"

Combine them all into a single list assignment:

  set y = getAProp( [ 1: "one", 2: "two", 3: "three", 4: "four" ], x )

For more on the uses of lists, see section 12.

[top]


Director Web: Director FAQ: [10] Lingo

Maricopa Center for Learning and Instruction (MCLI)
Maricopa County Community College District

HTML by Zac Belado, zac@wimsey.com

The Internet Connection at MCLI is Alan Levine --}
Comments to levine@maricopa.edu

URL: http://www.mcli.dist.maricopa.edu/director/faq/faq10.html