Global Variables: Example Scripts

The following scripts are simple examples using global variables.

While global variables are in fact global, they still need to be declared everywhere they are used. This means that they need to be declared either within every handler where they are referenced, or simply in line with the variable.

Example 1: Once a Global, Always Global

For example, the variable "dog" in the script below must be declared in both the main script and the helper script in order to be used as a global variable.

Main Script

global dog -- Declares dog as a global var

Put "Hello" into dog -- Stores a string in dog

Test1 -- Runs Test1.script

Called Script (Test1)

global dog -- Declares dog as a global var

log dog -- Logs "Hello"

Example 2: Global Only Sometimes

However, it is also possible to declare "dog" as a global variable in-line. This makes it so that the variable can be used as a global variable sometimes and a local variable at other times. In some circumstances this is necessary but it is less preferable because it can also cause more problems and confusion.

Main Script

Put "Hello" into global dog -- Stores a string in dog as a global variable

Test1 -- Runs Test1.script

Called Script (Test1)

log dog -- Logs "dog", because it is a local variable, not a global one

log global dog -- Logs "Hello", because it is calling the global variable

For more information about global variables as well as the other types of variables, see Variables.

 

This topic was last updated on February 18, 2020, at 11:05:50 AM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2020 Eggplant