Acknowledgement: This text is a formatted version of a message posted to *Teaching on WorldMOO by Andy Bakun.
Variables are named slots that can be used to store a value. The names of variables always begin with a letter or an underscore (_), and can have any number of of letters, numbers, or underscores after that. Variable names are case insenitive, meaning the variable named FOO is the same as the variable named foo. We can use eval to test this:
;;foo = "bone"; return FOO
Variables can not be named after any of the moo keywords, namely, while, for, return, in, endwhile, endfor, if, endif. Variables also can't be named the same as the Error values.
Variables are expressions that return the value they represent. They can take on any value at any time. Once a variable is created, it exists for the entire verb execution, but once the verb ends, the variable is destroyed for that execution cycle. Variables do not keep their values or even existance across verb calls (exceptions exist for the builtin variables) or across verb invocations. Variables can be operated on just like all other MOO values, but only using the rules of the same type with which that variable holds.
x = #4;
return x + 3;
=> E_TYPE (you can't add an object and a number)
x = 4;
return x + 3;
=> 7 (you can add numbers)
Variables are often used to store the result of some expression. If an expression's result wasn't assigned to a variable, then the expression's result would be tossed on the floor and forgotten about.
Variables that are defined return E_VARNF (Variable not found). This will cause a traceback if the verb is +d, or the variables value will be E_VARNF if the verb is !d.
foo = bar;
This assigns the value of the variable bar to the variable foo.
foo = foo + bar;
this sets foo equal to the result of foo plus bar (what exactly foo + bar equals is dependant on the types)
foo + bar is not the same as foobar. foobar is an entirely differnt variable name.
It's good programming practice to use descriptive variable names. It's much easier to understand your code 5 months after you wrote it and it's getting an error if you can tell what each variable if for by looking at it's name.
for x in (players())
If the body of the for is big, you will find yourself wondering what x exactly holds. Its much clearer to write:
for dude in (players())
for loser in (players())
it makes for better readability
The MOO sets aside some variable names for you, that contain certain things about the run time enviroment of your verb. The player variable holds the object number of the current player. args is the arguments that were passed to your verb. dobj and iobj hold the direct and indirect object specifiers on your verb, and dobjstr and iobjstr hold the strings that the player entered for the direct and indirect objects. prep holds the entered preposition. 5 variables also exist to make the results of the typeof() functions easier to read, they are STR, NUM, LIST, OBJ, and ERR. typeof() returns a number, and these variables can be used to compare, allowing greater readability than comparing to numbers.
if (typeof(1) == NUM)
is more readable than
if (typeof(1) == 0)
(note ;NUM => 0, ;STR => 2, in case you want to find out the values of these variables.)
Two other variables are this and verb, which contain the object number that the currently executing verb is on, and the name of the currently executing verb. Any builtin variable that is empty will contain "" if it's type is a string, and $nothing (#-1) if it's type if susposed to be an object.