FANDOM


Variables are used for temporarily data storage. This is a very important feature of LiveCode and most computer languages, but variables operate a little different in LiveCode.

The Put command Edit

In LiveCode, the Put command is used to store a value into a variable. If the variable already exists, its value is changed by the Put command. If that variable does not yet exist, a local variable by that same name will be created and given that value. For example:

put 23 into myVari                      -- myVari = 23                  an integer
put myVari into yourVar                 -- yourVar = 23                 an integer
put "I live in Phoenix" into Boogy      -- Boogy = "I live in Phoenix"  a string
put false into myCarry                  -- myCarry = false              a boolean

Suppose you create a variable, then use a Put command to give it a new value. But you misspell the name of the variable. LiveCode will then create a new variable with that misspelled name and give that variable the value. This is a problem that is not easy to find, because you are not looking for it. The take-away is to make sure your variable names are spelled correctly.

From this point on, we are going to look at three aspects of Variables - Naming, Containers, and Scope. Then we will look at the three different types of variables in LiveCode.

Naming Edit

A Variable in LiveCode can be given any name you want, provided the following are not violated:

1  The variable name cannot be a LiveCode reserved word
2  The variable name cannot begin with "rev"
3  The variable name must contain only letters, integers and underscores.
4  The variable name must start with a letter or an underscore

Containers Edit

In LiveCode, a variable can contain any kind of data - integer, floating point, text, or Boolean. The programmer does not have to give a variable a "type". LiveCode keeps tract of all variables, converting them from one type to another as necessary, releaving the programmer of that responsibility.

In LiveCode, a variable is a container. It can have one or more values and naturally operate as a list of values. Those values do not have to be of the same type of data. Chunk expressions are used to access the information in a container variable.

For example:

put Scarlet && Red && Pink into myColors 
put 888 && "Sunshine" && Blonde & " " before myColors 
put " " & Purple && "Violet" after myColors

The above will result in a variable called myColors = 888 "Sunshine" Blonde Scarlet Red Pink Purple "Violet"

Now the following chunk expressions are use to access the information in myColors:

put number of words of myColors into myItems            -- myItems = 8
put third word of myColors into myIdeal                 -- myIdeal = Blonde
put character 1 of fifth word of myColors into myNix    -- myNix = "R"
put first word of myColors + 112 into Fred              -- Fred = 1000

A variable with two or more values can be changed into an array using the Split command. An array can be changed into a container variable using the Combine command. Check them out.

Scope Edit

The Scope of a variable has to do with where the variable exists and for how long. The longer a variable exists and in the more places, the greater the risk of being inadvertently changed by the program. Such problems, while rare are very difficult to find. If a variable is limited in its existence, that problem is lessened. LiveCode uses 3 different types of variables, depending on the scope - Local, Script Local and Global.

Local Variables Edit

The scope of a local variable is local to the one handler in which that variable is used. As a result, inadvertent errors with this type of variable are diminished. It is not necessary to declare a local variable, it is assumed. Once a handler finishes, its local variables cease to exist.

For example:

on mouseUp                   -- myVox doesn't yet exist
  answer myVox               -- myVox has been created, but doesn't yet have a value
  put "Hello" into myVox     -- myVox = "Hello"
  answer myVox               -- answer = "Hello"
end mouseUp                  -- with this statement, myVox no longer exists

It is possible for two handlers to have local variables with the same names, but they are totally separate variables. Changing the value of a local variable in one handler has no effect in another handler.

Script Local Variables Edit

Sometimes, its important to be able to give a variable a value in one handler and use it in another handler. While this increases the risk of inadvertent errors, sometimes its too useful to pass up. A Script Local Variable is local to the scrip in which it is declared and can only be accessed from the handlers in that scrip. A Script Local Variable is persistent and continues to exist for as long as the program is executing.

A Script Local Variable must be declared by using the local statement at the top of the script outside of any handler. Then, a script local variable can be used in any handler in that script. It is not necessary to declare the variable in each handler, it is assumed.

For example in this card script:

local XX                         -- XX is declared as a script local variable

on openCard                      -- XX exists with scope = script local
  put 5 into XX                  -- XX = 5
end openCard                     -- with this statement, XX still exists

on mouseUp                       -- XX still exists
 answer XX                       -- answer = 5
end mouseUp                      -- with this statement, XX still exists

Global Variables Edit

The scope of a global variable is global to the program, it can be used in any handler in any script of the program. A global variable is persistent and continues to exist for as long as the program is executing. Using global variables makes for a greater risk that the program will make an inadvertent error, but sometimes there is the no other way to do it. Another possibility is to use Custom properties. This is a good substitute for a global variable. Check it out.

A Global Variable must be declared by using the global statement at the top of the first script outside of any handler. It is also necessary to put a global statement at the top of each handler in which that variable is used. If the variable is not declared as global in a handler, a local variable by that name will be created instead. This is a problem of a different sort, very difficult to find because you are not looking for it.

Once a Global Variable has been declared, it can be used in any handler. An example of a Global Variable in the script for a stack:

global Circuit                  -- Declaration of global variable
on openStack                    -- Start of handler
   global Circuit               -- Declaration of global variable for this handler
   put 45 into Rex              -- Rex = 45
   put Rex + 5 into Circuit     -- Circuit = 50
end openStack                   -- End of handler

The variable Circuit can used in other handlers in the same program. For example, in the script for a button:

on mouseUp                      -- Start of handler
   global Circuit               -- Declaration of global variable for this handler
   answer Circuit               -- answer = 50
end mouseUp                     -- End of handler

See also: Getopt, Special_chars_like_+,-,&,...

Community content is available under CC-BY-SA unless otherwise noted.