Variables in LiveCode
The concept of variables is not just limited to LiveCode but can been seen within all programming languages available today. Simply put, a variable is a place to store data that you create within your application. There are many special/advanced types of variable at your disposal (e.g array, parameters) but in the context of this lesson we will be focusing on the 3 main types (scope) of variables. These are local (temporary), script local and global.
Visualisation of Variables
First lets start with a simple visualisation of a variable. Above (1), Bob wants to go into his house. In the real world, he would simply open the door and enter (2).
In LiveCode, the process is very similar...we want to put “bob” into the house. An actual LiveCode script that can depict this is:
put "bob" into tHouse
In this example, “bob” is our data and “tHouse” is our variable.
You may notice that I have used a lower case “t” at the beginning of the tHouse variable in the above examples. Letters such as these are used so we easily know what scope of variable we are dealing with. When we use “t” this is a temporary variable, “s” is script local and “g” is global.
These are the naming conventions we use here at LiveCode HQ but please feel free to use whatever you are most comfortable with. Ok... lets discuss the differences between each of these variable types:
These types of variables only retain their data in the handler that called them. If you run a handler more than once, the variable will reset each time the handler is executed. An example of this is something like the following within a button script:
on mouseUp local tAdd put 1 into tAdd -- tAdd is our temporary variable repeat with x = 1 to 10 add 1 to tAdd -- add 1 to tAdd ten times end repeat put tAdd -- this places the value of tAdd in the message box end mouseUp
Now, each time you run this script, the message box will show 11 no matter how many times you run it. This is due to the temporary nature of this variable...as soon as the handler ends...it resets.
In Strict Compilation Mode, temporary variables must be declared before they can be used. In this case, we use the “Local” command and this is generally at the top of the handler.
Script local variables are a step up from local variables as not only do they retain their value, any handlers within an object script will be able to use these values. Lets adjust the above example for this purpose:
local sAdd on mouseUp repeat with x = 1 to 10 add 1 to sAdd -- add 1 to sAdd ten times end repeat put sAdd -- this places the value of sAdd in the message box end mouseUp on mouseDown if sAdd is empty then put 1 into sAdd end if -- initializes sAdd if empty repeat with x = 1 to 10 add 1 to sAdd -- add 1 to sAdd ten times end repeat put sAdd -- this places the value of sAdd in the message box end mouseDown
In this example, you will see we have two handlers “mouseUp” and “mouseDown” within our button script. Both of these are executing a repeat loop which adds 10 to our variable “sAdd”.
Unlike temporary variables, script locals must always be declared before they can be used, even outside of Strict Compilation Mode. For this type of variable we use the “Local” command and this is generally at the top of your object's script.
If the declaration wasn’t there, then when not in Strict Compilation Mode the variable would simply be treated as a temporary variable.
Our final standard variable type is a Global variable. What makes these unique is that they can be accessed from anywhere within your application (as long as they are declared). The following is our repeat loop example edited for globals. This time we are using two buttons
global gAdd on mouseUp repeat with x = 1 to 10 add 1 to gAdd -- add 1 to gAdd ten times end repeat put gAdd -- this places the value of gAdd in the message box end mouseUp
global gAdd on mouseUp put "the value of gAdd is" && gAdd end mouseUp
Like before, we have our repeat loop adding 1 to our variable 10 times. As our variable is now global, we have to declare it as such with the Global command.
You will have to declare this variable wherever you want to access it and this is what I have done in button 2. Now, I can set the variables value in 1 button and I can easily access this value in the 2nd button.
Although Global variables have their place, using them can lead to some headaches so using an alternative to these might be an option. The following LiveCode lesson explains these alternatives and how to use them: