User Guide

24 Chapter 2: Director Scripting Essentials
If you declare a variable inside or outside a JavaScript syntax function by using the syntax
_global.varName, the variable is available to all scripts within a movie.
The following example uses the syntax
_global.gMovie in one script to declare the variable
gMovie as global. This variable is available to all scripts within the movie.
// JavaScript syntax
_global.gMovie = 1; // Declare gMovie in one script
// Create a function in a separate script that operates on gMovie
function mouseDown() {
_global.gMovie++;
return(_global.gMovie);
}
The following example declares the global variable gScript in one script. This variable is
available only to functions within that script.
// JavaScript syntax
var gScript = 1; // Declare gScript in a script
// gScript is available only to functions in the script that defines it
function mouseDown() {
gScript++;
return(gScript);
}
In JavaScript syntax, when you define variables before any handlers, the variables automatically
have
undefined as their initial value.
Using local variables
A local variable exists only as long as the handler in which it is defined is running. However, after
a local variable is created, you can use the variable in other expressions or change its value while a
script is still within the handler that defined the variable.
Treating variables as local is a good idea when you want to use a variable only temporarily in one
handler. This helps you avoid unintentionally changing the value in another handler that uses the
same variable name.
To create a local variable:
In Lingo, assign the variable a value using the equals (=) operator.
In JavaScript syntax, inside a function precede the variable name with the keyword var, and
then assign the variable a value using the equals operator.
Note: Because JavaScript syntax variables are global by default, if you attempt to declare a local
variable inside a function without using the keyword
var, your script could produce unexpected
behavior. Therefore, although using
var is optional, it is strongly recommended that you declare all
local JavaScript syntax variables using
var to avoid any unexpected behavior.
To display all current local variables in the handler:
In Lingo only, use the showLocals() function.
In Lingo, you can use this method in the Message window or in handlers to help with debugging.
The result appears in the Message window. The
showLocals() method does not apply to
JavaScript syntax.
To monitor the values of local variables during movie playback, use the Object inspector. For
more information on the Object inspector, see “Debugging in the Object inspector” on page 91.