Director Tutorials

 

Local, Global and Property Variables

A variable is a data holder. This data usually changes (varies) over time. There are 3 variable types in Director - local, global and property variables. The main difference between them is the accessibility of the data the variable holds, and the ability to get and set the values of the variable.

There are a few basic rules in naming variables:
Names must be single words; you cannot have spaces in a variable name.
You cannot start a variable name with a number.
Variables cannot have ? character in the name.

Local Variables

Local variables can only be used from within the handler they are defined.

For example – create the following movie script
on setLocalVar
     localVar = “I am a local”
end

In the Message Window type:
setLocalVar
This activates the handler, defining the local variable and giving it a value. Next type:
put localVar
The result is
-- <Void>

The reason is that you can’t access the data of a local variable from outside the handler in which it is used.

Change the movie script to
on setLocalVar
     localVar = “I am a local”
     put localVar
end

In the Message Window type:
setLocalVar
The result is
-- “I am a local”

The reason the data was given this time was that the local variable data was accessed from the same handler in which it was defined. The advantage of using local variables is that they are forgotten once no longer needed, once the handler (in which they appear) ends.

Global Variables

A global variable allows the variable information to be available throughout the movie from one handler to another and from one script to another.

For example – create the following movie script:
global gGlobalVar
-- At the start of the script, we have to declare the variable as a global.
-- The letter ‘g’ is placed at the start of the variable name to remind us that the
-- variable is a global. It is a good idea to follow naming conventions to make the
-- code easier to understand.

on setGlobalVar
     gGlobalVar = “I am a global”
end

on getGlobalVar
     setGlobalVar
     put gGlobalVar
end

In the Message Window type:
getGlobalVar
The result is
-- “I am a global”

The global variable data was accessed from a different handler from which it was created. The advantage of a global over local variable is that the information can be accessed at any time from any part your movie. Since the data is always available, they take up more memory than locals.

Property Variables

A property variable is used in a behavior or parent script (properties won’t work in movie scripts) to hold information specific to the particular object to which it is attached.

For example – create the following behavior
property pPropertyVar
-- Like globals, we declare the variable type.
-- Following naming conventions, I have started the variable name with ‘p’.

on beginSprite
     pPropertyVar = “I am a property”
end

Attach the above behavior to sprite 1 and play the movie. While the movie is playing, type the following in the Message Window:
put sprite(1).pPropertyVar
The result is
-- “I am a property”

A property is stored with the script or ScriptInstance that created it. The scriptInstanceList is a sprite property containing a list of all script references attached to the sprite.

Example 2 – using properties in scripts with parameters
Write the following behavior and attach it to sprite 1 and sprite 2.
property pPropertyVar

on setProperty me, varData
     pPropertyVar = varData
end

on getProperty me
   put pPropertyVar
end

The me parameter follows the handler and is used to reference the particular object this script is attached to, and thereby allows pPropertyVar to contain a different value for different sprites. For more information on using me and other parameters look at the Using Parameters Technote.

While the movie is playing, type the following in the Message Window:
setProperty sprite 1,  “property of sprite 1”
setProperty sprite 2,  “property of sprite 2”

The above assigns the sprite number to the parameter me and the text string to varData.
Then type:
getProperty sprite 1
The result is:
-- “property of sprite 1”

and
getProperty sprite 2
-- “property of sprite 2”

The same result could be achieved by typing:
put sprite(1).pPropertyVar

In the above example the property variable pPropertyVar contains different data for each sprite.

There are close similarities between property and global variables. A common question is when should you use properties and when should you use globals. The main advantage that properties have over globals, is the protection you have over changing the data they store. This is especially important with large projects high in code. It is not such an important issue in small projects.

In large projects, it is important to manage your data well, and make sure you don’t accidentally make a change to one code unit and cause problems to another. Using global variable can cause such problems.