[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.3 Naming Values Within a Verb

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:


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:


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:

an integer, the type code for integers (see the description of the function typeof(), below)
the same as INT (for historical reasons)
an integer, the type code for floating-point numbers
an integer, the type code for lists
an integer, the type code for strings
an integer, the type code for objects
an integer, the type code for error values

For others, the general meaning of the value is consistent, though the value itself is different for different situations:

an object, the player who typed the command that started the task that involved running this piece of code.
an object, the object on which the currently-running verb was found.
an object, the object on which the verb that called the currently-running verb was found. For the first verb called for a given command, `caller' has the same value as `player'.
a string, the name by which the currently-running verb was identified.
a list, the arguments given to this verb. For the first verb called for a given command, this is a list of strings, the words on the command line.

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.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on September, 30 2008 using texi2html