LiveCode is different from other programming languages. Usually every programming language uses variables to store temporary data; LiveCode is similar, but it will immediately destroy the variable(s) outside a message. To store permanently data in your script, you have to use a properties or a custom properties of objects.
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 about 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 & char to concatenate string 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.