User Guide
Variables 21
In JavaScript syntax, you cannot compare symbols of the same name to determine whether they
refer to the same symbol. To compare symbols of the same name, you must first convert them to
strings by using the
toString() method, and then perform the comparison.
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 a 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, record whether a network operation is complete, and many more options.
It is a good idea always to assign a variable a known value the first time you declare 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 local variable exists only as long as the handler in which it is
defined is running. A global variable can exist and retain its value as long as Director is running,
including when a movie branches to another movie. A variable can be global within an individual
handler, a specific script, or an entire movie; the scope depends on how the global variable is
initialized.
If you want a variable to be available throughout a movie, it is good practice to declare it in an
on
prepareMovie
(Lingo) or a function prepareMovie() (JavaScript syntax) handler. This
ensures that the variable is available from the very start of the movie.
For more information on using both global and local variables, see “Using global variables”
on page 22 and “Using local variables” on page 24.
Storing and updating values in variables
Variables can hold data for any of the data types found in Director, such as integers, strings,
TRUE
or
FALSE values, symbols, lists, or the result of a calculation. Use the equals (=) operator to store
the values of properties and variables.
As mentioned in the Data types section of this reference, variables in both Lingo and JavaScript
syntax are dynamically typed, which means that they 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 and C++, 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 by using the
value() function (Lingo) or the parseInt()
method (JavaScript syntax), or a number to a string by using the string() function (Lingo) or
the
toString() method (JavaScript syntax).
The values of some properties can be both set (the value is assigned) and returned (the value is
retrieved), and some property values can only be returned. Properties whose values can be both set
and returned are called read/write, and those that can only be returned are called read-only.
Often these are properties that describe some condition that exists outside the control of Director.
For example, you cannot set the
numChannels cast member property, which indicates the number
of channels within a movie that contains Macromedia Shockwave content. However, you
can return the number of channels by referring to the
numChannels property of a cast member.