Variable Scope

Can any Kindly Explain me clearly about the variables Scope in sensetalk. The reference document is not much pretty clear.

Kindly explain me with some examples to get difference between local,global and universe variables.

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    edited July 2011
    The scope of a local variable is just the handler it's created in.

    Global variables can be accessed from any handler during a given run (but you have to use the "global" keyword in each handler when declaring or referencing them.)

    Universal variables are like global variables, but their values persist between script runs for as long as your current eggPlant session is running. As with global variables, you have to use the "universal" keyword in each handler in which you want to reference these variables. Universal variables aren't used that frequently; when you use them you need to remember that their values persist and reinitialize them as appropriate.
  • Can you Please Explain me with Examples,with simple Code snippets
  • EggplantMattEggplantMatt ForumAdmin admin
    global earth // global variable declaration
    universal universe // universal variable declaration
    put "crater" into moon // local variable creation and assignment -- no declaration
    put "asteroid" into earth
    put "solar system" into universe
    
    playDice
    put earth // global variable -- outputs the word "asteroid"
    starTrek
    put moon // local variable -- still outputs the word "crater"
    put earth // global variable changed by starTrek handler -- outputs the phrase "peace, love, and understanding"
    put universe // universal variable changed by starTrek handler-- outputs the phrase "background radiation"
    
    on playDice
        put moon // local variable -- outputs the word "moon"
        put earth // local variable -- outputs the word "earth"
        put universe // local variable -- outputs the word "universe"
        put global earth // global variable -- outputs the word "asteroid"
        put universal universe  // universal variable -- outputs the phrase "solar system"
        put "alien invasion" into earth // locatl variable -- changes the value of earth only in this handler
        put earth // local variable -- outputs the phrase "alien invasion"
        put global earth //global variable -- still outputs the word "asteroid"
    end playDice
    
    on starTrek
        global earth // earth now declared to be global in this handler
        universal universe // universe declared universal in this handler
        put earth // global variable -- outputs the word "asteroid"
        put universe // universal variable -- outputs the phrase "solar system"
        put "green cheese" into moon -- local variable -- no effect on variable with the same name elsewhere in the script
        put "peace, love, and understanding" into earth // global variable -- changes the value of the global variable everywhere 
        put "background radiation" into universe // universal variable -- the next time the script is run, the universe variable will contain the phrase "background radiation" 
    end starTrek
    
Sign In or Register to comment.