Global
A global variable is a variable which can be accessed from anywhere in the movie. Global variables need to be declared in every script that uses them. The declaration must appear before any use of the variable. By convention, global declarations are made at the top of the script. By convention, all global variable names should be prefixed with a lower-case "g". A variable that is not declared as a global will treated as a Local Variable, and will be flushed from memory at the end of the handler.
global gUserName global gUserLevel
on prepareMovie gUserName = "George" gUserLevel = 1 gUserData = [] -- not declared as a global, so may cause errors later end prepareMovie
The fact that globals need to be declared in every script that they are used could lead to a long list of declarations at the beginning of every script. It is generally considered good practice to use as few global variables as possible.
A better alternative is to use a global property list of variables:
global gPropertyList
on prepareMovie gPropertyList = [:] gPropertyList[#userName] = "George" gPropertyList[#userLevel] = 1 gPropertyList[#userData] = [] end prepareMovie
All variables in the Message window while the Debugger is not running are treated as global variables.
From Director 8.0 onwards, you can use the globals? to access the list of global variables, without needing to use a global declaration.
put (the globals).gPropertyList.userName -- "George"
You can print the value of all current globals to the Message window by using the command showGlobals. showGlobals will not display any global variables whose value is VOID.
You can set the value of all global variables to VOID by using the command clearGlobals. The globals will still remain declared, but they will no longer be displayed by the showGlobals command.
The global version? will not be affected by the clearGlobals? command. The global version is initialised to contain a string describing the version of the current Director player. This may contain one or more point characters, so it cannot be reliably converted to a float?ing point number:
put version -- the Message window treats the variable as a global -- "8.5.1"
put version -- "10.1"
version = #unorthodoxUseOfGlobalVersion clearGlobals put version -- #unorthodoxUseOfGlobalVersion