LiveCode is different from other programming languages in how variables are used. Almost all languages use variables to store temporary data. LiveCode is similar, but it will immediately destroy the variable(s) outside a message or handler, unless they are placed above any following handlers with a pre-label of 'Local' or 'Global', which would make their contents available to all handlers in that script.
To permanently store data in your programs, you can use properties or create custom properties of objects. Properties and custom properties are available to the scripts anywhere in the program. Properties and custom properties are more livecode program way, they reduce the risks of conflicts or errors.
First of all let's see how to declare and set a variable:
put 23 into myVar
the command put is used to create and change variables. Immediately after put you have to specify the value, then you have to insert one of the following words:
- into: it substitutes the actual value with the indicated value in the variable
- before: it inserts before the actual value with the indicated value in the variable
- after: it inserts after the actual value with the indicated value in the variable
Finally you have to indicate the variable name. Variable names must start with a letter or an underscore, avoid all variable name starting with "gRev" or "rev" since many internal variables use this way of naming.
Put is used for many purposes, in this page we will see only its use where it pertains to variables.
You can use put with any type of data (number, string, binary, etc.), for example:
put 12 into myVar put myVar + 54 into myVar
put "Hello" into myVar out " world" after myVar
In order to insert a complex string in a variable, you can use the & char to concatenate strings and the words RETURN and TAB for new-line and TAB chars; see the next example:
put "Hello " & "world" into myVar put return & "How are you?" after myVar put return & "This" & TAB & "is" & tab & "a tabbed line." after myVar answer myVar
This is the result:
As already stated, the variable scope is limited to the message where the variable resides. If I insert two message in the same object, I can write:
on mouseUp put "Hello" into myVar answer myVar end mouseUp on mousedoubleUp put "Hello" into myVar answer myVar end mousedoubleUp
The variable myVar in mouseUp is different from the one in mouseDoubleUp. Declaring a variable above the handlers using 'local' or 'global' would change this behavior, essentially the mouseDoubleUp handler would overwrite the mouseUp handler above it. For example:
local tmpAppPath on preOpenStack // get the folder where the application stack is... put the effective filename of this stack into tmpAppPath end preOpenStack on mouseUp // display the path in an answer dialog... answer tmpAppPath end mouseUp
The other way is to use Custom properties, for example we create an appFolder property:
on preOpenStack // get the folder where the application stack is... put the effective filename of this stack into tmpAppPath set the appFolder of me to tmpAppPath end preOpenStack on mouseUp // display the path in an answer dialog... answer the appFolder of me end mouseUp