What is a Variable?
What is a Variable?

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

 

Now, 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 one, 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
   
   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

Script Local

Script local variables are 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 buttons script . Both of which are executing a repeat loop which adds 10 to our variable “sAdd”.

You should also notice that unlike temporary variables, script locals must be declared before they can be used. For these type of variables we use the “Local” command and this is generally at the top of your objects script.

 

If the declaration wasn’t there, then 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.runrev.com/m/4071/l/13158-what-are-the-alternatives-to-using-global-variables

 

About The Author
Neil Roger
Neil Roger
Neil is the Technical Support Lead for LiveCode.
Read Neil's Blog
Other Articles
Hour of LiveCode
   11.12.2014

Last night we held our "Hour of Code" live online, taking you through the first lesson for the Create it with LiveCode course. We were delighted with the turnout, so many of you participated!

 

For those of you who could not be there, we have recorded the session and you can view it here.

Read more
 
 
Seeking Eureka
   11.12.2014
We've been finding out about some of the people on our Create it with LiveCode course. Our job is to get you all to that moment when you realise, hey, yes, I can do this! I'll be following how well we do together on this journey, and a great place to start is by meeting you, the coders-to-be.
Read more

Build an Xmas Game

   11.12.2014

I was cruising youtube videos featuring LiveCode this week, and I came across this really neat and topical set of videos on making a Christmas themed card or game. I couldn't resist trying it out for myself,

Read more
What is a Variable?
   11.12.2014
It's basic, it's fundamental, it's something you will use all the time in LiveCode, and it's something that beginners are frequently baffled by. This lesson explains what variables are, the different types you can use, and why you use them.
Read more
 
Thank you for your time.

 

© LiveCode 2014