Using Globals and Universals
Both global and universal variables must be declared in each handler where they are used (including the initial handler of any script, which is often the entire script). There are two ways to do this. I’ll use globals to illustrate (because it’s easier to type than “universal”!) but the same rules apply to universal variables.
You can declare one or more globals on a line, beginning with the word “global” and followed by a list of variable names:
global this,that,theOther
add 1 to this
put that & theOther
Or, you can declare them as global each time you use them:
add 1 to global this
put global that & global theOther
You can also mix the two approaches. So, if some global variables are used a lot, you may want to declare them up front, while others are specified as global when they are used:
global this,that
add 1 to this
put that & global theOther
The Difference Between Global and Universal Variables
In Eggplant, the difference between global and universal variables is that global variables are deleted after every script run, while universal variables retain their values from one run to the next, until you quit Eggplant. Also, keep in mind that the two types of variables are distinct, so it is possible to have both global and universal variables with the same name (and local variables, too, for that matter).
Here’s a little script you can play with to get a feel for the differences between globals and universals:
put universal this && universal that
put global this && global that
put 1 into global this
put 2 into universal this
put "Hello" into global that
put "Greetings!" into universal that
put global this && global that
put universal this && universal that
You’ll find that the first time you run this script, both the global and universal variables will start off empty (unless you’ve been running other scripts with universal variables). The second time you run it, the universal variables will still have their values from the previous run.