Programming for Interactive Multimedia
Notes from Week Six

Variables: local & global

Definition

A variable is a container for data--specifically data that is expected to change and/or data that will not be known until runtime (playback). Variables are the fleeting complements to constants, which also store data--but that data is known ahead of time and that data can never change.

Variables store many different kinds of data--numbers, strings, lists/arrays, objects, and other variables. In most programming languages you have to tell the variable what type of data is is to hold, and it can't hold any other type of data. To create a variable, you just give it a name and a value.

Examples:
userName = "Jewell" (string)
myNumber = 7 (numerical value)
myList = ["thisThing", "thatThing", "theOtherThing"] (list/array)
myNumber = yourNumber (another variable)

In Lingo, it's easy to change the data type of the variable: you simply put a different kind of data into the variable. For example, I could use my same variables from above and change the values:

Examples:
userName = 7
myNumber = ["thisThing", "thatThing", "theOtherThing"]
myList = yourNumber
myNumber = "Jewell"

Yikes! That makes it really easy to confuse your scripts further down the line. This chameleon nature of Lingo variables makes it very important to use condition statements to test for the proper data type before trying to execute some scripts.

Scope

Variables are fleeting--they have a limited "life span" and a relatedly limited range of influence called scope. In terms of processing, a variable in scope is being stored in RAM. When it's out of scope, it disappears from RAM. Variables can have local scope or global scope.

Local: A scope of local variable in Lingo is the handler it is instatiated (created) in. So if you create a variable on mouseUp on a particular sprite, you'd probably guess that it's not available to any other script on any other cast member or frame or sprite or the movie, but you also need to know it's also not available on mouseDown or mouseOver of that *same sprite*.
Example:
on mouseEnter me
buttonHere = sprite(the rollOver).memberNum
buttonHere = buttonHere + 1
sprite(the rollOver).memberNum = buttonHere
  end mouseEnter

If I then tried to access "buttonHere" on mouseLeave, I'd get an error.

Global:A global variable can be accessed in any script on that sprite or any movie, cast member, frame or sprite script. Any of those scripts can use that variable or set that variable if you give them the permission to. You give them access to the variable by including the line global myVariableName either before or within any handlers that created or modify the variable.

Example:
global buttonHere
on mouseEnter me
buttonHere = sprite(the rollOver).memberNum
buttonHere = buttonHere + 1
sprite(the rollOver).memberNum = buttonHere
  end mouseEnter

global buttonHere
on mouseLeave me
buttonHere = sprite(the rollOver).memberNum
buttonHere = buttonHere - 1
sprite(the rollOver).memberNum = buttonHere
  end mouseLeave

It may seem like you should always use global variables, but there are two reasons not to do this. First, all global variables are held in RAM throughout the movie and take up valuable memory space. Director is a processor and memory hog, so you don't want to keep anything around you don't need. Second, sometimes the values will change often in related handlers, and another handler only needs it's own value, not the other handler's value, and it will compute things wrong. So in all possible cases, "act local."

There are examples of variable usage in several of the Director files in the "examples" folder on the server.

Puppeting: the Punch & Judy show

Puppeting means putting Lingo, not the score, in charge of actions during playback. If you puppet a sprite, and Lingo tells it to put one member in that sprite channel, and the score puts another member in that channel, Lingo rules. As soon as puppeting is turned off, the score is back in charge. Puppeting gives you many more choices than the score allows you, so it's a good thing to know. Most importantly, though, you will puppet things in this class because you need to show me you can write Lingo, not let Director do things for you!

The things you will puppet most are sprites, sounds, and transitions, and possibly the tempo.

Here's the syntax

.puppet
sprite(x).puppet = TRUE or =FALSE

puppetTransition [ID code (a number)][,duration(in seconds)][,size(in pixels of chunks)][,area(TRUE for changing area only, FALSE for entire stage)]

Example:
puppetTransition 11, 1.5, 10, FALSE
--uses wipe left transition over 1.5 seconds moving 10 pixels at a time over the entire stage

puppetSound [channel (a number), memberRef]
To return control to the score
puppetSound [channel], 0

Example: --plays sound file of "Taps" in sound channel 5
puppetSound 5, "Taps"
updateStage

puppetTempo [speed]

Example: --changes the tempo to 15 frames per second
puppetTempo 15

All of these built in functions are described in detail with examples in the Lingo Lexicon at the back of your book, so READ IT! (I'm not here to rewrite the book).

References

Chapter 9 and "Appendix C: A Lingo Lexicon" of Director 8 Demystified.

Programming for Interactive Mulitmedia I Home