A variable is a container for storing information. The act of creating a variable is called declaring the variable. When you declare a variable, you must give it a name . Variable names in Lingo must consist of one or more alphanumeric characters , with no spaces. (They conventionally start with lowercase letters , though this is not technically required.)
The information stored in a variable is called its value . In most cases, you assign a value to a variable when you first declare it. (With global variableswhich you'll read about in a momentyou declare the variable first and assign a value afterward.) Once a value has been assigned to a variable, it may be changed any number of times.
The value of a variable may be numeric, or it may be a series of alphanumeric characters called a string . In order for Lingo to tell the two kinds apart, string values must always be enclosed in quotation marks.
Lingo supports two kinds of variables: local and global. A local variable exists only within a single handler, and it ceases to exist after the handler has finished executing. A global variable, by contrast, persists throughout a movie: After the variable is created, every handler in the movie recognizes its name and its current value.
It's generally a good idea to use local variables unless you specifically need a variable to be global. That way, you avoid potential conflicts if you happen to give two variables the same name.
To declare a local variable:
In any handler, type the line set variable = value , replacing variable with the actual name of the variable, and replacing value with the value that you want to assign to the variable.
For example, to create a variable called spriteWidth with a value of 100 , type set spriteWidth = 100 ( Figure 15.50 ).
Figure 15.50. When the playhead enters the frame to which this script is attached, this script creates a local variable called spriteWidth with a value of 100 .
If you want to change the value of the local variable later in the handler, just type the same line again with the new value.
You can't declare local variables in the Message window. Any variable declared in the Message window is automatically set to be global.
To declare a global variable:
In any handler, type the line global variable , replacing variable with the actual name of the variable.
Global variable names conventionally begin with the prefix g . So, for example, to create a global variable called gFirstName , type global gFirstName ( Figure 15.51 ).
Figure 15.51. When the movie begins, this script creates a global variable called gFirstName with a value of "Susan" .
At any later point in the handler, type the line variable = value , replacing variable with the actual name of the global variable, and replacing value with the value that you want to assign to the variable.
For example, to give the variable gFirstName a value of "Susan" , type gFirstName = "Susan" .
If you want to change the value of the global variable later in the same handler, just type the line from step 2 again but with the new value. If you want to change the value of the global variable in a different handler, you must first redeclare the variable. See "To retrieve the value of a variable," below.
To retrieve the value of a variable:
If you want to retrieve the value of a global variable in a handler other than the one it was created in, re-declare the variable by typing global variable (where variable is the actual name of the variable) ( Figure 15.52 ).
Figure 15.52. When the playhead enters the frame to which this script is attached, this script re-declares the global variable gFirstName , then puts its value ( "Susan" ) into the text cast member called "Nametag" .
If you forget to do this step, Lingo won't recognize the variable as globalit will assume you're using a local variable with the same name.
(This step isn't necessary for local variables, since they only exist within the handler they were created in.)
Use the variable name in any Lingo command that would otherwise include a value.
For example, to set the width of the cast member myMember, you'd ordinarily have to specify a numeric value, as in set the width of member "myMember" to 100 . Instead, you can substitute the name of a variable that has a numeric value, as in set the width of member "myMember" to spriteWidth .