Rûn Variables and Scope
A variable is a name that holds a value. Event scripts and programs keep their variables in different places, for different lengths of time, and cannot see each other's.
Event script variables
set makes a variable or changes it; add adds to a number, making it at 0 first if it does not exist. They are the game's global variables:
- Every event script sees every variable. There is no local variable in an event script.
- A save keeps them and a load puts them back. A new game, and every new test in the editor, starts with none.
- A game holds at most 64. Past that,
setandaddmake nothing and say nothing, so reuse names rather than numbering them. - A name holds at most 23 characters; text at most 63.
{name}in asayline, and in any verb's words, fills in the value.
[use]
add visits 1
if visits >= 3
flag set shrine_blessedThe Event Debugger's Variables list shows them all while a script is held (see Debugging Scripts).
Program variables
A program declares a variable with a type, or makes one by setting it:
number hp = 30
text name = "Wolf"
toggle angry = false
count = 3
print(name, hp, angry, count)Wolf 30 off 3- A declaration needs a value:
number hpalone is not a declaration. - A declared type is held:
number hp = "full"is an error. A variable made by setting it takes any kind of value, and so does a declared one when it is set later. - A name used before it exists is an error that says so.
number hp = "full"hp is declared number, and "full" is textprint(score)nothing is named scoreHow long a program variable lives
- For one run
- A program's variables live while its section, rule block or top level runs, and are gone when it ends. The next time the section runs, it starts with none.
- Inside a function
- A variable a function makes, its parameters included, is gone when the function returns.
- Inside a for
- A variable the
formakes, the counter included, is gone when the loop ends. - Inside if and while
- The braces of an
if,elseorwhiledo not make a new scope: a variable made inside is still there after the block.
if (true) {
number found = 5
}
print(found)
for (number i = 0; i < 3; i = i + 1) { }
print(i)nothing is named iThe first print above prints 5; the second fails, because i lived only in the loop.
Looking a name up
A name is looked up in the innermost place first and then outward, through every function call that led to it. A function therefore reads and changes its caller's variables when it uses their names, and a declaration in a function changes a caller's variable of the same name.
Reset() {
number x = 99
}
number x = 1
Reset()
print(x)99Give a function's own variables names its callers do not use, and hand values in as arguments.
Sections do not see the top level
When the world calls a program's section (on use), only that section runs: the lines outside every section are not run first, so a variable made there does not exist in the section.
number count = 5
on use {
print(count)
}nothing is named countA live program, one with an on update(number dt) block, is the exception: during a play its top level runs once, and its variables are kept and shared by all its sections and rule blocks until the play ends. See Rûn Events: Where Scripts Run.
Between the two
A program cannot read or change an event script variable, and an event script cannot read a program variable. Flags are shared by both: flag set and flag(<name>) in event scripts, World.FlagSet and World.Flag in programs. See Rûn Saving State.