Global variables can be confusing sometimes. The most important thing to keep in mind is that each script, or handler within a script, is independent in SenseTalk, and must declare any global variables that are used.
There are two ways to indicate that a variable is a global: either declare it on a global declaration line (a line beginning with the word “global” followed by a comma-separated list of variable names); or use the word “global” before the variable name in each place where it is used in the script.
Here is a tiny example script that illustrates their use. You should be able to create a new script, copy this code into it, and then run it:
global var1, var2 -- declare 2 global variables
put "hello" into var1
put 1 into var2
put "initial handler" into handlerName -- a local variable
put "START: " & handlerName && var1 && var2 -- display our variables
put function1() -- call function1 and display its return value
command1 -- call command1
put "END: " & handlerName && var1 && var2 -- display our variables
global var1 -- declare only var1 as a global here
put "function1" into handlerName -- a local variable
put 1000 into var2 -- this var2 is local to function1
return "Returned From: " & handlerName && var1 && var2
-- Note: don't declare any globals up front in this handler
put "command1" into handlerName -- a local variable
add 6 to global var2 -- add to the var2 global variable
put "I'm local" into var1 -- this is the var1 variable local to command1
put "Greetings!" into global var1 -- now change the global var1
put handlerName && global var1 && var1 && global var2
To follow exactly what’s going on here, hold down the Option key when you click the Run button (or choose “Debug Script” from the Run menu) to start the script run paused in the debugger. Then step through the script one line at a time by clicking the “Step Into” item in the Run window’s toolbar (or choose “Step Into” from the Run menu).
Each step of the way, you can examine the current contents of any of the variables by typing “put var1” or “put global var2” in the Ad-Hoc Do Box at the bottom of the Run window. I strongly encourage playing around in the debugger in this way to get a feel for how this all works!
Universal variables work just the same as global variables, except that they retain their values across different script runs in the same Eggplant session (until you quit Eggplant). Also, keep in mind that globals and universals are distinct – each type has its own set of values.
I hope that helps! I’m not sure exactly what the error message was that you were seeing. If you are still having trouble, post the exact message that is displayed, and the relevant part of your script and we’ll do what we can to help get it all working.