First and foremost, the most important thing to realize about the LambdaMOO system is that there really isn't anything like a program to deal with. In C++, every program has a main function. For LambdaMOO, any implicit main program is safely concealed inside the internals of the system, and programmers have no say in the matter unless they wish to start hacking away at server code. For LambdaMOO, a programmer is restricted to constructing objects and their verbs. Like any object oriented system, a programmer could argue that the execution paths through the various verbs constitues a program, but unfortunately that argument tends to break down quickly since the entire point of having functions on an object is so that other objects can interact with each other. That's the point of object oriented programming.
Unless otherwise noted, when specifying commands for performing some action, any text enclosed in square brackets is optional. E.g. FunctionName(Arg1 [,Arg2) indicates that Arg2 is optional when calling the function Arg1.
Having said that, the next issue is how one constructs objects to do meaningful things in LambdaMOO. The major issues at this point are
MyList={@MyList,NewElement}
Elements of a list can be of any type allowed for variables in LambdaMOO, including lists themselves.
See the section on Lists for more details.Data Type | typeof(...)== | Conversion Function> | Comments |
---|---|---|---|
Integers | INT or 0 | tonum(...) or toint(...) | Signed whole numbers |
Object | OBJ or 1 | toobj(...) | Reference number for an object |
Strings | STR or 2 | tostr(...) | String or character |
Error | ERR or 3 | none | Error condition. Used for various runtime errors |
Lists | LIST or 4 | none | Immutable list. Used for arrays and sets |
Floating point number | FLOAT or 9 | tofloat(...) | Signed floating point number |
@create ParentObject named "Object Names"
This first method is used by human beings to create objects, and is something which would be entered on the command line. For example:
@create $thing named "my very own object"will create an object derived from $thing (Generic Thing) and set the new object's name to my very own thing
create(ParentObject [,OwnerObject])
This method is intended for programmatically creating new objects by invoking the interpreter's built in create function. Both ParentObject and OwnerObject are required to by object references. If OwnerObject is not specified, then the object will own itself (see object ownership in the security section). The return type of create will be the object reference of the newly created object, or else the appropriate error code.
For example:
create($npc,player)
will create an object derived from $npc and owned by the player object which caused this verb to be executed.
$recycler:_create(Parent, [,OwnerObject)
This method is also intended for programmatically creating new objects but in this case it differs from the built in function in a subtle manner. When create() is used, the object reference will always be unique. $recycler:_create attempts to objects discarded with $recycler:_recycle(NoLongerNeededObject) by reconstructing a previously used object into its new role. In a perfect world this verb is indistinguishable from the create function, except that its friendlier to longer lived systems which create many temporary objects.
@property ObjectName.PropertyName [PropertyValue]
This first method is intended for use by human beings when constructing objects before they are put into use. If PropertyValue is omitted, the property is initialized to 0.
add_property(TargetObject,PropertyName,InitialValue,Info)
This method invokes the built in add_property function to add the property PropertyName to the object TargetObject. For more information, please consult the online help using @help add_property
Use tkMOOlite or MacMOOSE
Andrew Wilson's tkMOOlite or the MacMOOSE programs each have nice interactive object browser's which allow a programmer to interactively add properties and verbs to an object.
ObjectReferece.Property = NewValue;and to change a value at the command line, the only difference is to add a semicolon (to tell the command line interpreter that you are typing in an expression and not a command), like so:
;ObjectReference.Property = NewValue;Creating verbs on an object
To access a particular element of a list or string, the square braces are used. Elements are numbered sequentially from 1 to n, where n is the length of the string or list.
Assigning to an element of a list or string modifies the corresponding element of the list or string.
For example:
Test="George"; // Our example string Test[3]="4"; // Test is now "Ge4rge" Test[5..6]="56"; // Test is now "Ge456e" Test={"A","B","C"}; // Our example list Test[2]="BC"; // Test is now {"A","BC","C"}; Test[2]="BC","DE"; // Error. Invalid statement. Test[2]={"BC","DE"}; // Test is now {"A",{"BC","DE"},"C"};As shown in the example, the elements selected from a list or string can be more than one element long. However, those elements have to be continuous. For example
"George"[3..3] // "o" "George"[3..5] // "org" "George"[3..5,7..9] // syntax errorStrings can be costructed using the + operator to append two strings together. Unfortunately, this modifies neither of the two original strings.
OriginalName="George"; LastName=" The Great"; NewName=OriginalName+LastName;Changes neither OriginalName or LastName. There is absolutely nothing wrong with assigning like
OriginalName="George"; LastName=" The Great"; OriginalName=OriginalName+LastName; // OriginalName = "George The Great" OriginalName="George"+LastName; // Same result OriginalName="George"+" The Great"; // Same resultIn fact, this way of appending to strings in absolutely necessary. In LambdaMOO (as in Java), a string is only as long as it absolutely has to be, and to append to a string (such as changing a string stored in a property) requires such a construct.
Lists are constructed in a similar manner. The ampersand operator, when applied to the front of a list name, expands out to the contents of that list. For example:
List={1,2,3,4,5}; List={@List}; // Identity operation List={@List,6}; // List = {1,2,3,4,5,6}; Counter=1; while(Counter<6) List={@List,Counter}; Counter=Counter+1; endwhile // List is now {1,2,3,4,5,6,1,2,3,4,5} NewList=List[7..10]; // NewList = {1,2,3}
The elements of a list can be lists themselves. There is no restriction on the layout of each sublist, and lists can be nested arbitrarily deep to construct multidimensional arrays or fancy data structures.
List={}; List={@List,{},{},{}}; // List now contains three empty lists. List[1]={1,2,{3,4,5,6}}; // The first list in List now contains // the list {1,2,{3,4,5,6}} List[3]=List[1]; // The third list in List now contains // a copy of List[1] List[3][3][2]=7; // List is now // { {1,2,{3,4,5,6}}, {}, {1,2,{3,7,5,6}} }For both lists, the in operator is very useful. in is a binary operator, the left argument being the element to look for, and the right element being the list to search. Its value is the position in its target to search, or 0 if the element is not found.
(5 in {1,2,9,3,5,2}) // result is 5 (4 in {0,4,4} // result is 2Like assignment, using in to look for substrings won't work. Likewise, if you construct a complicated list structure, in only checks the list and not any elements in the list.
({1,2} in {0,{1,2},{3,4}}) // result is 2 (2 in {0,{1,2},{3,4}}) // result is 0
Various utility objects, such as $string_utils, $set_utils, $assoc_utils, $match_utils, and others are available for use. The specific documentation is available by @examineing the appropriate objects, or reading the comments in the verbs of the object.
The really important thing to remember is that if your were all set to use a binary tree, hash table, or other data structure normally implemented using pointers, you have two choices: 1--rethink the algorithm using lists, or 2--write, find or convince someone to write a set of functions to implement that data structure using LambdaMOO's lists.