Variables

Variables:
Variables are where we save data so it does not go away. Variables are described as boxes where you put things. 

Variables are short-lived. They only exist inside the code segment (handler) where they are used. To get around that, we can declare them ahead of time as "local" or "global"
    e.g. 
        local timer
        global score

    Locals are only defined in the handler that they are declared. 
    Globals can be referenced in other scripts but Globals must be declared in every script. There is no stack-wide or session-wide declaration. 

As for global variables you have to declare them, but you can declare them inside a handler for use in that handler or outside of all handlers of a script to be accessible for all handlers in that script.

 Know also that script local variables may be declared in a script, and that, like globals, must be declared ABOVE the handlers that reference them. This is why it is usual to see these gadgets declared at the top of the script, so all handlers "see" them.

Naming Variables
In many of my examples, I use variables like "theName" or "theGrade" There is nothing special about the variable - theName , we could just have used "x"

on mouseUp
   ask "What is your name?"
   put it into x
   answer "Hello " & x
end mouseUp

(Sometimes it is hard to tell which are special (reserved) words in the language and which are made-up ones. For instance, "Name" is a property of every object. So we can not use it - or if we do, LiveCode could get confused. For that reason, I use "x" and other single letters as my examples. As you get better, you can use variable names that describe the variable - like theName, yourName, aName, theUsersName, etc. Your programs will be easier to read and you do not try to remember what 'x" was for)

IMPORTANT - ACCEPTED STANDARDS and CONVENTIONS in LIVECODE
        In naming variables, most people put a letter in front of the name so they can tell where it was declared.
            s - for screen variables
            g - for global variables
             t - for temporary variables (local variables)
            etc

        So you would have the following variables in your program
            gName
            tCounter
            sScoreWindow

    You will see this convention followed in the scripts that you get on the LiveCode site, or in the forums from other people. It is a good idea to follow these conventions when you write your own code.

    You will see this convention followed in the scripts that you get on the LiveCode site, or in the forums from other people. It is a good idea to follow these conventions when you write your own code.

        Okay
            Using any name for a variable - like "x"

on mouseUp
   ask "What is your name?"
   put it into x
   answer "Hello " & x
end mouseUp

        Good
            Using a descriptive name for a variable - like "theName"

on mouseUp
   ask "What is your name?"
   put it into theName
   answer "Hello " & theName
end mouseUp

        Better
            Using a standard name for a variable
                 - like "tName" for a temp variable (use only here),
                 - like "gName" for a global variable (use anywhere)

on mouseUp
   ask "What is your name?"
   put it into gName
   answer "Hello " & gName
end mouseUp

       Best
            Using a standard name for a variable
                 - like "tName" for a temp variable (use only here),
                 - like "gName" for a global variable (use anywhere)
                and declaring it

global gName
on mouseUp
   ask "What is your name?"
   put it into gName
   answer "Hello " & gName
end mouseUp

Comments