As discussed earlier, it is possible to store values in properties on objects; the properties will keep those values forever, or until another value is explicitly put there. Quite often, though, it is useful to have a place to put a value for just a little while. MOO provides local variables for this purpose.
Variables are named places to hold values; you can get and set the value in a given variable as many times as you like. Variables are temporary, though; they only last while a particular verb is running; after it finishes, all of the variables given values there cease to exist and the values are forgotten.
Variables are also "local" to a particular verb; every verb has its own set of them. Thus, the variables set in one verb are not visible to the code of other verbs.
The name for a variable is made up entirely of letters, digits, and the underscore character (`_') and does not begin with a digit. The following are all valid variable names:
foo _foo this2that M68000 two_words This_is_a_very_long_multiword_variable_name
Note that, along with almost everything else in MOO, the case of the letters in variable names is insignificant. For example, these are all names for the same variable:
fubar Fubar FUBAR fUbAr
A variable name is itself an expression; its value is the value of the named
variable. When a verb begins, almost no variables have values yet; if you try
to use the value of a variable that doesn't have one, the error value
E_VARNF
is raised. (MOO is unlike many other programming languages in
which one must `declare' each variable before using it; MOO has no such
declarations.) The following variables always have values:
INT FLOAT OBJ STR LIST ERR player this caller verb args argstr dobj dobjstr prepstr iobj iobjstr NUM
The values of some of these variables always start out the same:
INT
typeof()
, below)
NUM
INT
(for historical reasons)
FLOAT
LIST
STR
OBJ
ERR
For others, the general meaning of the value is consistent, though the value itself is different for different situations:
player
this
caller
verb
args
The rest of the so-called "built-in" variables are only really meaningful for the first verb called for a given command. Their semantics is given in the discussion of command parsing, above.
To change what value is stored in a variable, use an assignment expression:
variable = expression
For example, to change the variable named `x' to have the value 17, you would write `x = 17' as an expression. An assignment expression does two things:
Thus, the expression
13 + (x = 17)
changes the value of `x' to be 17 and returns 30.