Home‎ > ‎

Variables

Variables
Variables are places where we save data. When a user types something in a field, when that screen goes away, so does what they typed. So you need a variable (like a "box" to store it in).

We also need variables to save calculations and information that we may need again later.

Variables are created just by putting something into it e.g. "put the field "Name" into myName" - which creates a variable called "myName" where your name is saved for later. Variables are described as boxes where you put things. 

Variables can also declared as local or global (which means that they are permanent.). It is better to create variables by declaring them than just create them by using them. By declaring them,, your are always reminded what they are. (It is easy to forget about a variable or try to figure out what it was weeks later when you come back to your program)

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" ("global
    i.e. 
        local timer    - timer can only be used in that section
        global score - can be used anywhere (but you must always have the statement "Global score" everywhere you use it. 

    e.g.   global sGameRunning  or local myScore


1. Naming Variables

Variables 
- containers for holding information or data

Naming rules:
    Any combination of letters, numbers and underscore (_).
    Must start with letter or _
    Must not be the same as a LiveCode language reserved word 

    note: a lot of languages recommend that variables start with lower case letters

        some good variable names are:

            myscore     or theirfirstnames
            my_score    or their_first_name    (This is called "snake" case and is easier to read)
            myScore     or theirFirstName    (this is called "camel" case - think of a camel's hump - also easier to read)

     Create a variable by putting something into it:         

        put "Hello World." into theMessage

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 variable
            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.

        Another reason to follow it is that it is safer. You will not get into conflicts with LiveCode property names. For instance, every object has a "name" If you try to create a variable called "name", you will run into problems and you will not know why your program does not work. It is because LiveCode does not know which one to use - the property "name" or your variable called "name". Other properties that you have to be careful and not use for a variable name are: "height", "width", "text", "left", "top", "location", "size" and others. So you see that it is safer to put a "x" or "t" or "g" in front of the name that you give your variable so it will not cause a problem. It also tells you where the variable is used or where it comes from.


  
Examples of Naming Variables
 
        BAD
            Using a name that is the same as a property - like "name"

on mouseUp
   ask "What is your name?"
   put it into name
   answer "Hello " & name                ERROR or Unpredictable Results
end mouseUp

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

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

        Better
             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

       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



2. Using Variables

    You can save anything off the screen into a variable using the "put" statement. Then change it and put it back on the screen

    Example #1
In the graphic below, we get the name, grade and score off the screen and save them into variables:
We want to put the:
    name from the field we called "name" ==> into our variable 'tName'
    grade from the field that we called "grade" ==> into our variable 'tGrade'
    score from the field that we called "score" ==> into our variable 'tScore'
Then we added 20 to the score and put it back onto the screen into the field "score"
                   
Using the "put" we can save information from the screen into variables, change it and put it back onto the screen. (or save it for later)

    Example #2
In the graphic below, we get two numbers from the user - Number 1 (field "num 1")  and Number 2 (field "num 2"): then we give them those numbers added together and multiplied together
The following code is on the "Done" button: (It gets the numbers that they tped in and saves them as "x" and "y". Then it puts the "sum" and the "product" of those numbers back on the screen

            on mouseUp
                put field "num 1" into x
                put field "num 2" into y
                put x+y into field "sum"
                put x*y into field "product"
            end mouseUp

When they press the "Done" button they get the answers

                  


For a simple exercise, implement the above program 

You may want to add a "reset" button to clear out all the fields Now to see if you understand how to do itwith the following code:

on mouseUp
   put empty into field "num 1" 
   put empty into field "num 2"
   put empty into field "sum"
   put empty into field "product"
end mouseUp

Now to see if you understand what we just did - add the result of subtracting and dividing the two numbers to the screen


Comments