Using Director > Writing Scripts with Lingo > About variables > Storing and updating values in variables

 

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 set x = 1 creates the variable x, which is an integer variable because you assigned the variable an integer. If you subsequently use the statement set x = "one", the variable x becomes a string variable, because the variable now contains a string.

Some properties cannot be set, but can only be tested. Often these are properties that describe some condition that exists outside Director's control. 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):

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"

Variables can also hold the results of mathematical operations. Both of these statements add the result of an addition operation to the variable mySum:

mySum = 5 + 5
set mySum = 5 + 5

It's good practice to use variable names that indicate what the variable is used for. This will make your Lingo easier to read. For example, the variable mySum indicates that the variable contains the sum of numbers.

To test the values of properties or variables:

Use the put command in the Message window or check the values in the Watcher window.

For example, the statement put myNumber displays the value assigned to the variable myNumber in the Message window.

As another example, the following statement returns the cast member assigned to sprite 2 by retrieving the sprite's member property:

put the member of sprite 2