User Guide
Chapter 16402
About variables
Director uses variables to store and update values. As the name implies, a variable contains a value
that can be changed or updated as the movie plays. By changing the value of a variable as the
movie plays, you can do things such as store a URL, track the number of times a user takes part in
an online chat session, or record whether a network operation is complete.
It’s a good idea always to assign a variable a known value the first time you define the variable.
This is known as initializing a variable. Initializing variables makes it easier to track and compare
the variable’s value as the movie plays.
Variables can be global or local. A global variable can exist and retain its value as long as Director
is running, including when a movie branches to another movie. A local variable exists only as long
as the handler in which it is defined is running. See “Using global variables” on page 403 and
“Using local variables” on page 404.
Storing and updating values in variables
Variables can hold any of the types of information found in Director: numbers, strings,
TRUE or
FALSE values, symbols, lists, or the result of a calculation. To store and retrieve the values of
properties and variables, Lingo uses the equals (
=) operator and the set and put commands.
Also, a variable in Lingo can contain different types of data at different times. (The ability to
change a variable’s type distinguishes Lingo from other languages such as Java, in which a
variable’s type cannot be changed.)
For example, the statement
x = 1 creates the variable x, which is an integer variable because you
assigned the variable an integer. If you subsequently use the statement x = "one", the variable x
becomes a string variable, because the variable now contains a string.
You can convert a string to a number or a number to a string with the
value() and string()
functions. For example, the statement
x = value("1") stores the number 1 in the variable x.
The statement
x = string(1) stores the string “1” in the variable x.
Some properties cannot be set, but can only be tested. Often these are properties that describe
some condition that exists outside the control of Director. For example, you cannot assign a value
to the
numChannels cast member property, which indicates the number of channels within a
Shockwave movie. However, you can retrieve the number of channels by referring to the
numChannels property of a cast member.
To assign a value to a variable:
• Use the equals (=) operator. For improved readability, you can use the optional set command
at the beginning of the statement.
For example, any of these statements will change the cast member assigned to sprite 2 by setting
the sprite’s
member property to a different cast member. The last two statements use dot syntax
(see “Dot syntax” on page 393):
set the member of sprite 2 = member "Big Flash"
set sprite (2).member = member ("Big Flash")
sprite (2).member = member ("Big Flash")
As another example, each of these statements assigns a URL to the variable placesToGo:
placesToGo = "http://www.macromedia.com"
set placesToGo = "http://www.macromedia.com"