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

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.

Variable Naming

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.

Character Example Scope
g
gVar
Global variables
t
tVar
Handler-local variables(temporary)
s
sVar
Script-local variables
p
pVar
Parameters
k
kVar
Constants
c
cVar
Custom Properties

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:

Temporary Variables

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

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.

Global

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

 

Button 1

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	

Button 2

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:

http://lessons.livecode.com/m/4071/l/13158-what-are-the-alternatives-to-using-global-variables

 

7 Comments

Eric

Since you don't do anything with the variable "x" in the repeat loop
"repeat with x = 1 to 10"
you might as well use
"repeat for 10" instead.

For that matter "repeat with x = 10 to 1"
will also work. But since this is for beginners that is even more confusing.

MrCoolLion

I would also like to have card variables that are valid within a card and it's objects. When you leave a card the variable is deleted from memory.

Elanor Buchanan

You can use script local variables, which can be accessed by any handler in the card script but not the objects of the card. One way to achieve this would be to use custom properties of the card, which you could then delete in a closeCard handler.

You can read about custom properties in this lesson

http://lessons.livecode.com/m/4603/l/565727-livecode-properties

Kind regards

Elanor

Nicholas Spies

This would be a good place to put naming conventions for variables.

Elanor Buchanan

Hi Nicholas, thanks for the suggestion!

I have added the naming convention we suggest to the lesson.

Kind regards

Elanor

Enrique Troyo del Valle

How to restart a variable? I mean to set back its value to 1 or any other given value. Thanks in advance!

Elanor Buchanan

Hi Enrique

You can set the value of a variable with

put into

for example

put 0 into tScore

I hope that helps.

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.