Each handler has its own variables
A variable you create in a handler belongs to that handler alone. Other handlers cannot see it, and it is gone when the handler ends. Programmers call the part of a program where a name means something its scope.
on setName
put "Ada" into tName
end setName
on showName
put "Name:" && tName
end showName
setName
showName
Name: tName
showName has never had anything put into a tName of its own, so it reads the word as plain text, the way Variables explained. The same happens the other way round, from the main lines into a handler:
on showColour
put "inside:" && tColour
end showColour
put "red" into tColour
showColour
put "outside:" && tColour
inside: tColour outside: red
That is a feature, not a nuisance: two handlers can both use a tCount without getting in each other's way. To give a handler a value, pass it as a parameter; to get one back, use return.
Declaring a local
You can say up front which variables a handler uses with local, and give them a first value as you do:
on addTwo
local tX = 5, tY = 10
put tX + tY
end addTwo
addTwo
15
In an ordinary script this is optional. But if you turn on Variable Checking in the Script Editor (Edit > Options > Variable Checking), xTalk insists that every variable is declared before it is used, and a mistyped name becomes an error you see straight away, rather than a word that quietly turns into text. The setting is called explicitVariables, and many experienced scripters keep it on.
Variables a whole script shares
Declare a variable with local outside any handler, at the top of the script, and every handler in that script can use it. It keeps its value between calls, for as long as the stack is open. These are called script locals, and many people start their names with s:
local sCount = 0
on bump
add 1 to sCount
end bump
bump
bump
bump
put "Bumped" && sCount && "times"
Bumped 3 times
A script local is the natural home for something a button or card needs to remember: the score in a game, the file it opened last, whether it is busy.
local sTotal
command addUp pN
add pN to sTotal
return sTotal
end addUp
addUp 4
put the result
addUp 6
put the result
4 10
Globals: one value for every script
A global is shared by every script in every open stack. Each handler that uses it must say so with global and its name, either inside the handler or at the top of the script:
global gScore
on addPoints pN
global gScore
add pN to gScore
end addPoints
put 0 into gScore
addPoints 10
addPoints 5
put "Score:" && gScore
Score: 15
A global nobody has put anything into is empty, and it lasts until you quit, even after the stack that made it closes.
Constants: names that never change
A constant is a name for a value that stays the same while the program runs. Write it once, at the top, and use its name everywhere. When the value changes in a later version, you change one line.
constant kMaxLives = 3
constant kGreeting = "Welcome back"
put kGreeting & "! You have" && kMaxLives && "lives."
Welcome back! You have 3 lives.
xTalk will not let you put anything into a constant, which is the whole point of one:
constant kMax = 3
put 5 into kMax
The letters at the front
The names in these lessons follow a habit common across the xTalk world, which tells you where a variable lives without looking for its declaration:
tfor a temporary variable inside one handler:tTotalpfor a parameter:pNamesfor a script local:sCountgfor a global:gScorekfor a constant:kMaxLives
xTalk does not require any of this, but code that follows it is much easier to read, and it also keeps your names from colliding with xTalk's own words.
Try it yourself
Write a command keepWord that remembers every word it is given in a script local, and a function keptWords that gives back the words kept so far, joined by commas. Keep apple, pear and fig, then put keptWords().
Show a solution
local sWords
command keepWord pWord
if sWords is empty then
put pWord into sWords
else
put comma & pWord after sWords
end if
end keepWord
function keptWords
return sWords
end keptWords
keepWord "apple"
keepWord "pear"
keepWord "fig"
put keptWords()
apple,pear,fig
Words in this lesson
Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.