A variable is for temporarily data storage, a very important feature of LiveCode and most computer languages. The Put command is used In LiveCode to store a value into a variable. If that variable does not yet exist, a local variable will be created by that name and given the value. If the variable already exists, the value is changed by the Put command. For example:
put 23 into myVari -- myVari = 23 (integer) put myVari into yourVar -- yourVar = 23 (integer) put "I live in Phoenix" into Boogy -- Boogy = "I live in Phoenix" (string) put false into myCarry -- myCarry = false (boolean)
Be sure to check out the operation of the Put command.
Variables are used a little different in LiveCode than other computer language. We are going to look at three aspects of this - Naming, Containers, and Scope.
Variables 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".
In LiveCode, a variable is a container. This means that it can have more than one value. A variable can naturally operate as a list of values and the values do not have to be of the same type of data. Use chunk expression to access the values in the list variable. For example:
put Scarlet && Red && Pink into myColors put 888 && "Sunshine" && Blonde & " " before myColors put " " & Purple && "Violet" after myColors
-- myColors = 888 "Sunshine" Blonde Scarlet Red Pink Purple "Violet"
put number of words of myColors into myItems -- myItems = 8 put third word of myColors into myIdeal -- myIdeal = "Blonde" put character 1 of fourth word of myColors into myN -- myN = "R" put first word of myColors + 112 into Fred -- Fred = 1000
The Scope of a variable has to do with where the variable exists and for how long. The longer the variable exists, the greater the risk of being inadvertently changed by the programmer. Such problems are very difficult to find. If a variable is limited in its existence, that problem is lessened. LiveCode uses 3 different styles 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 the variable are diminished. It is not necessary to declare a local variable, it is assumed.
Suppose a local variable with the same name is used in two different handlers. Even though they have the same name, those are two totally separate variables. 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
on mousedoubleUp -- myVox doesn't yet exist answer myVox -- myVox has been created, but doesn't have a value end mousedoubleUp -- with this statement, myVox no longer exists
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 very useful. A Script Local Variable is local to the script in which the variable is used. For example, within any of the handlers in the script for a button.
A Script Local Variable must be declared by putting the local statement at the top of the script outside of any handler. It is not necessary to declare the variable as a local variable in each handler, it is assumed. Once declared, such a variable can be used in any of the handlers of that script. For example in the script of a stack:
local XX on preOpenStack -- XX exists with scope = script local put 5 into XX -- XX = 5 end preOpenStack -- with this statement, variable still exists on mouseUp -- XX still exists with value answer XX -- answer = 5 end mouseUp -- with this statement, XX no longer exists
Global Variables Edit
The scope of a Global Variable is global to the program in which the variable is used. Such a variable can be used in any handler in any part of the program. The risk is greater for the programmer to make an inadvertent error, but sometimes there is the no other way to do it. Another possibility is to use Custom properties. It is a good substitute for a global variable. Check it out.
A Global Variable must be declared by putting 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 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 header. 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
Now the the variable Circuit can used in any other handler in the 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