how to use global variable


I've problem on using global variable. Maybe I did not use it correctly. Please help me to correct it.

From testcase.script.

function A

set myglobal = myvar1

return myvar1
end function A

function B
set myglobal = myvar2

return myvar2
end functionB
From CommonShare.script

global myglobal

function mycom
put myglobal into runtimevar

open file and write runtimevar with new line.
end mycom

I got error message: "unable to set global variable".


Best Regards,


  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    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
    function function1
        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
    end function1
    on command1
        -- 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
    end command1
    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.
  • BrickBrick Member

    I am new to egg Plant, the code you have above does not show my problem.

    I have 2 scripts 1 declaring 2 global lists and the other trying to use the lists and it does not work.

    File 1 Named File_1 looks like this:
    global defaultList, secondList

    put (("Print","1"),("Print","2")) into defaultList

    put (("Cancel","1"),("Cancel","2")) into secondList

    File 2 Named File_2 looks like this:
    start using "File_1.script"

    put defaultList &&& secondList into list

    put item 1 of list
    put item 3 of list

    I was expecting the "put item 1 of list" and "put item 3 of list" to print out
    ("Print","1") and ("Cancel","1")

    The reason I want to do this is for maintenance purposes if any options change in time we should only have to change the list items in 1 file.

    Can you help?
    Can you use the global variables in another script, the documentation implies YES?

  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    Hello Brick,

    Yes, you can use global variables in any script. The trick is that you need to declare them as global in each script or handler where they are used. It looks like you're thinking that the "start using" command will do that for you, but "start using" only makes handlers from the other script callable.

    To do what you want, just add this line at the start of your File_2.script:
    global defaultList, secondList
    The only way to access global or universal variables is to declare them as such in each handler where you need access to them.
  • BrickBrick Member
    Thanks Doug,

    That works OK.
    I was thinking the "start using" would do what I wanted.

Sign In or Register to comment.