forked from sevenecks/lambda-moo-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark-sleep-lambdamoo-programming-tutorial-non-html5.html
1904 lines (1446 loc) · 74.6 KB
/
dark-sleep-lambdamoo-programming-tutorial-non-html5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<H1>LambaMOO Programming Tutorial (very rough draft)</H1>
<P><EM>by Steven J. Owens (unless otherwise attributed)</EM></P>
<!-- There are 422 paragraphs -->
<P>MOO, of which LambaMOO is the first and biggest, is an interactive,
multiplayer online game that includes the ability for players to build
and even program new things or features into the game. The language
lacks a formal name but is generally referred to as "moo code" or
sometimes "moocode".
</P>
<P>While there is a fair amount of information about LambdaMOO
programming in various places, there is also a lack of a comprehensive
explanation, especially of much of the programming environment. These
incomplete notes are the start of an attempt to remedy that. This is
just a beginning, but maybe someday it'll be an end.
</P>
<P>There are a bunch of other resources out there, starting with the LambdaMOO
Programmer's Manual:
</P>
<P><A HREF="http://www.moo-cows.com/docs/manuals/ProgrammersManual.html">http://www.moo-cows.com/docs/manuals/ProgrammersManual.html</A>
</P>
<P>Also there are a variety of tutorials. I'm not trying to duplicate or
replace them. Instead, this is a combination of filling in the gaps
about the MOO programming environment and jumpstarting you into MOO
programming.
</P>
<H3>Outline of Current Draft</H3>
<UL>
<LI>Basic Terminology
<LI>Verb Invocation and Property Access
<LI>Data Types
<LI>Lists
<LI>Eval
<LI>Comments
<LI>Variable Declaration and Scoping
<LI>Perms and Args
<LI>Flow Control, for, if, while, suspend, fork
<LI>Type Conversion
<LI>Threading, Ticks and Tasks
<LI>Core Classes
<LI>Sight and Sound in MOOCode
<LI>Location in MOOCode
</UL><P><B>Note:</B><EM> See end for draft outline of planned revision</EM>
</P>
<H2>Basic Terminology</H2><P>I'm going to assume you have a basic familiarity with programming
concepts, but don't get too stressed out if you don't, you'll be able
to pick that up as you go along. If you're totally, completely
new at this, I explain some general concepts at the end of this page,
in a section titled:
</P>
<A HREF="#RealBasics">Real Basics of Programming in Moocode"</A><P>I'm going to assume you have a basic familiarity with moo concepts,
but just to be clear:
</P>
<H3>The Server and the Db</H3><P>You have the server and the database (colloquially "the db"). The server
is the foundation; it loads and runs the database, which defines the
objects and their properties and verbs. Much of the code that makes
the up the basic moo-ishness of things is implemented in the db, in
moocode.
</P>
<P><B>Note:</B><EM> Most people tend to associate the term "database" with a
"relational database", and most relational database programs tend to
keep most of their data in disk storage. The purely technical meaning
of "database" is "an organized collection of information."
LambdaMOO's database is not relational, it is an object database, and
it is kept entirely in-memory. The only reason I'm pointing this out
is to head off any chance of you confusing the moo database for a
relational database.</EM>
</P>
<H4>Objects, Verbs and Properties</H4><P>MOO consists of objects.
</P>
<P>MOO objects have properties and verbs.
</P>
<P>Properties are values stored on the object and referenced in code via
the "." syntax, for example:
</P>
<PRE>somevariable = object.propertyname ;</PRE><P>Verbs are code stored on the object and invoked in code via the ":"
syntax, for example:
</P>
<PRE>somevariable = object:verbname() ;</PRE><H4>Object Oriented</H4><P>MOO stands for "Mud, Object-Oriented". If you don't understand what
"object oriented" means, it's an approach for organizing the code and
data in a system.
</P>
<P>MOO's object-oriented approach is slightly different from many
object-oriented programming langauges. In most object-oriented
languages, you have a division between object definition (its
blueprint, so to speak) and instances of the object in the system.
The object definitions (called classes) exist off in some abstract
place, but your code in the system never deals with them directly.
Instead you create "instances" of a given class and use the instance.
</P>
<P>In the MOO world, the object is defined by example. You create an
object instance in the system and then dynamically (aka "on the fly")
add verbs and properties to make your prototype. Then you can create
a new object that is "descended" from the first object. The new
object in turn can be dynamically modified with more verbs and
properties, and then you can create more objects that descend from the
second object.
</P>
<P>If object-oriented programming is new to you, here's a brief
explanation:
</P>
<P>The simplest approach is procedural - a sequential series of
instructions. Procedural languages have concepts for organizing the
code, like subroutines (also called functions or methods, depending on
what programming language you're talking about).
</P>
<P>Object-oriented languages usually have their own version of
subroutines (typically they're called methods, or in MOOcode, verbs),
but they also have the concept of organizing groups of subroutines
(methods, verbs) as well as data (instance variables, or properties).
This conceptual grouping is what we call an "object".
</P>
<H4>Core Db, MinimalCore and LambdaCore</H4><P>The "core db" is the infrastructure that most moos start from, the
initial set of objects, verbs and properties that provide handy
utilities and predefined types of objects. These are all that exists
when the MOO is first started up, until the users start adding custom
code. For example, player objects, room objects, exit objects and
thing objects are all part of the moo core db.
</P>
<P>There are, generally speaking, two different versions of the core db
in use. The minimal db and the lambdacore. The minimaldb is just
about exactly what the name suggests, just the bare bones minimum of
objects to get your MOO up and running. The lambdacore has a whole
bunch of additional objects and features that the lambdamoo wizards
found useful.
</P>
<H4>Built-in Functions, Utils and the System Object</H4><P>The moo language has a number of predefined functions, which are
generally referred to as functions or sometimes as "built-in
functions" or "built-ins" for short.
</P>
<P>Most built-ins are documented in MOO's online help Sometimes there's
more than one help entry that matches a built-in function name, so as
a rule of thumb append an empty parentheses to the functionname() when
you use the help command. For example "help tonum()" will get you
help on the built-in function named tonum (see "Data Types", below).
</P>
<P>In addition to the built-ins, there are a large number of handy little
utility verbs. Since verbs have to be defined on objects, these
utility verbs are organized on several objects, one object per
category of verbs. They are generally referred to as "the utils" or
sometimes "the $utils", because the individual grouping objects tend
to be kept track of in properties on the #0 object, and you can refer
to these properties by using $propertyname, like $string_utils, or
$list_utils.
</P>
<P>Speaking of object #0, object #0 is The System Object. This is used
as a central place to stick values that the wizards want to keep track
of on a moo wide basis, in properties on #0. You can reference these
properties with #0.propertyname, but there's a shortcut, as I used
above, the dollar sign, $, so you can reference the properties in your
code by simply using $propertyname.
</P>
<P>Anytime you see $ in a moo verb, mentally translate it to "#0.", i.e.:
</P>
<PRE>$string_utils:english_ordinal</PRE><P>is really
</P>
<PRE>#0.string_utils:english_ordinal</PRE><P>There are a great many such useful properties on #0 besides the utils,
but here are the utils defined on LambdaMOO's #0 as of this writing:
</P>
<LI> #0.building_utils
<LI> #0.byte_quota_utils
<LI> #0.code_utils
<LI> #0.command_utils
<LI> #0.convert_utils
<LI> #0.gender_utils
<LI> #0.generic_utils
<LI> #0.list_utils
<LI> #0.lock_utils
<LI> #0.match_utils
<LI> #0.math_utils
<LI> #0.matrix_utils
<LI> #0.object_quota_utils
<LI> #0.object_utils
<LI> #0.perm_utils
<LI> #0.quota_utils
<LI> #0.seq_utils
<LI> #0.set_utils
<LI> #0.string_utils
<LI> #0.time_utils
<LI> #0.trig_utils
<LI> #0.wiz_utils
<P>There are also several verbs defined directly on #0, but most of them
are system related and are not particularly relevant to your average
moo coder.
</P>
<H4>The Root Class, Nothing and $nothing</H4><P>Object #1 is The Root Class. All objects are descended from Object #1.
</P>
<P>Object #-1 is "nothing" , the null object, the object that does not
exist. Things that are not located anywhere in particular have their
location property set to #-1. Verbs or functions that do not come up
with a valid result typically return the value #-1.
</P>
<P>There's actually a root property, $nothing, that contains the value
#-1, but I'm pretty sure that $nothing does not define #-1, it merely
points to it and gives you a more expressive way to use the value #-1.
I.e. instead of putting the characters #-1 in your code, you put
$nothing and anyone reading the code (including you, a few months or
years later!) finds it easier to understand what your intent was.
</P>
<H3>Verb Invocation and Property Access</H3><P>In code you invoke verbs with the ":" operator.
</P>
<PRE>foo = object:methodname() ;</PRE><P>In code you reference properties to get the values in them with the "." operator.
</P>
<PRE>foo = object.propertyname ;</PRE><P>You can generally chain properties for verb or property reference,
assuming that each link in the chain is a valid object reference,
e.g.:
</P>
<PRE>title = player.foo.bar:title();</PRE>
<PRE>name = player.foo.bar.name ;</PRE><P>Here's a more realistic example of that:
</P>
<PRE>roomtitle = player.location.title();</PRE>
<PRE>name = player.location.name ;</PRE><H3>Case Insensitivity</H3><P>Moo, unlike unix, linux, etc, doesn't care whether letters are upper
case or lower case. This seems to be generally true for variable
names, object names, verb names, etc. I guess Pavel (or Ghond before
him) just didn't want to deal with an endless number of users getting
their capitalization mixed up and screaming "This is broken!".
</P>
<PRE>>;;foo=5; me:tell(Foo)
5
=> 0
[used 154 ticks, 0 seconds.]
>;;foo=5; me:tell(FOO)
5
=> 0
[used 154 ticks, 0 seconds.]
>;;foo=5; me:tell(fOO)
5
=> 0
[used 154 ticks, 0 seconds.]
></PRE><H3>Data Types</H3><P>MOO has four data types:
</P>
<LI> numbers (integers and floats)
<LI> strings (i.e. text)
<LI> lists of elements
<LI> moo object numbers (sometimes called objnums).
<P>Numbers default to integers. Integers is a math term commonly used in
programming languages, it means no fractions or decimal places. If
you divide 1/2 in MOO, you get 0. Surprisingly, you get get a heck of
a lot done with just integers.
</P>
<P>Originally MOO only did integers, but these days MOO also does floats,
aka floating point numbers, i.e. numbers with a decimal point. That's
how MOO knows that a number is a float, by the presence of a decimal
point:
</P>
<PRE>somefloat = 5.0 ;
someotherfloat = 5.0/2.0;
otherotherfloat = somefloat/someotherfloat ;
</PRE><P>In the example above, "someotherfloat" will contain the value "2.5"
and "otherotherfloat" will contain the value "2.0".
</P>
<P>You can't mix integers and floats, so in MOOcode you need
to either include the decimal point in all the numbers in a statement
or use the "tofloat()" built-in function to convert the value in a
variable into a float (see "Type Conversion", below). Otherwise you
get a type mismatch error:
</P>
<PRE>;1/2.0
#-1:Input to EVAL, line 3: Type mismatch
... called from built-in function eval()
... called from #217:eval_cmd_string (this == #1449), line 19
... called from #217:eval*-d (this == #1449), line 13
(End of traceback)</PRE><P>You can use two periods to indicate a range of values, i.e. 1..10
means 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. The two periods are called
the "range operator" and this mostly comes up in lists (although
can also use it in other places, like foreach loops) so I'll talk
about it more down in the section titled "Lists".
</P>
<P>Strings is just programmerspeak for "text", i.e. "a string of
characters". The term dates all the way back to 1959 and the really
early computer languages. Moo knows something is text because it's
surrounded by double-quotes:
</P>
<PRE>astringvariable = "some text"</PRE><P>Lists are really fancy and get their own section (below).
</P>
<P>Object numbers are references to objects in the MOO database. Moo
recognizes object numbers because they're preceded by a pound
sign #, also called a hash sign and officially named the octothorpe :-).
</P>
<PRE>puffsobjectnumber = #1449</PRE><H3>Lists</H3><P>You define a list by enclosing a comma-separated series of
elements in curly braces
</P>
<PRE>foo = {"a", "b", "c"} ;</PRE>
<PRE>bar = {1, 2, 3} ;</PRE>
<PRE>baz = {1, "two", 3} ;</PRE><P>You reference a specific element in a list by appending a number in
[square brackets] to the variable name. The number is called an
index, and accessing a list element this way is called indexing:
</P>
<PRE>xyzzy = foo[2] ;</PRE><P>The index of the very first item in the list is 1, as you would expect
if you're a normal human being:
</P>
<PRE>foo = {"a", "b", "c"} ;
player:tell("The element at index 1 is: ", foo[1])</PRE><P>The player sees:
</P>
<PRE>The element at index 1 is: a</PRE><P>If you're a programmer rather than a normal human being, you expect
the index of the very first element to be 0, for technical reasons
which I will skip for now. Getting used to this is usually a major
early stumbling block for non-programmers, which is probably why moo's
inventors (Pavel and Ghond) decided to go with 1 instead. But if
you're already a programmer, you're going to expect it to be zero,
which is why I'm pointing this out.
</P>
<P>Note that lists can contain any sort of data, including other lists:
</P>
<PRE>foo = {"a", b", c", {1, 2, 3} } ;</PRE><P>Nested lists can get quite complicated. One particularly common form
of nested lists is called associative lists, or alists for short, and
the $list_utils have a number of special verbs for dealing wtih
alists. Other languages usually use what's called a dictionary type
(often "dict" for short) or "hashtable" or "map" to serve the same
purpose.
</P>
<P><B>Note:</B><EM> hashtable as a name describes how it's implemented; map as a
name describes the act of using one value to look up another value.
I'm explaining this mainly because the terms sometimes come up in
programming discussions, so it's useful to be familiar with them.</EM>
</P>
<PRE>somealist = {{1, "January"}, {2, "February"}, {3, "March"}}</PRE><P>Note that the above isn't really a good example of an alist, since you
could just make a list of month names and use the position (index) of
each month name instead of needing use the associative list utility
functions to look them up. Here's a more realistic example, where
you're storing players high scores in some in-moo game, using their
object numbers as the key:
</P>
<PRE>somealist = {{#1449, 65050}, {#68, 743634}, {#107439, 34544}, {#93202, 235423}, {#94285, 10025}}</PRE><P>Most of the examples above are lists with elements that are all the
same data type, but lists can contain mixed data types:
</P>
<PRE>{"foo", 1, "bar", 1.5, #-1}</PRE><P>Sometimes you'll have a list of mixed data types like above, but of
course the order of the data types is something your code has to keep
track of. For example, you might have a message system where each
list contains the time received as an integer, the person the message
was from as an objectnumber, and the message text as a string. And
then you'd store all of your {time, personnumber, messagetext} lists
in another big list.
</P>
<P>The trick with lists is not the syntax, it's the way lists are
used in day-to-day MOO coding. For example, if you do this, you end up having a list that contains two lists:
</P>
<PRE>
foo = {1, 2, 3} ;
bar = {4, 5} ;
baz = {foo, bar} ;
</PRE><P>baz will now contain {{1, 2, 3}, {4, 5}}
</P>
<P>You expand a list with the at sign @:
</P>
<PRE>
foo = {1, 2, 3} ;
bar = {4, 5} ;
baz = {@foo, @bar} ;
</PRE><P>baz will now contain {1, 2, 3, 4, 5}
</P>
<P>While you only use { and } when you're creating a list, and @ when you
expand a list, the "moo way" is that you do a whole lot of list
creation and expansion. For example the "right" way to append to a
list is:
</P>
<PRE>baz = {1, 2, 3, 4, 5};
baz = {@baz, 6} ;</PRE><P>baz will now contain {1, 2, 3, 4, 5, 6}
</P>
<P>What you are literally doing here is creating a new list that contains
both the contents of the old list, baz, and the new value, 6, and then
storing the reference to that new list in the variable name that baz,
which previously used to store the reference to the old list. In a more
longer form, it might look like this:
</P>
<PRE>baz = {1, 2, 3, 4, 5};
bat = {@baz, 6} ;
baz = bat</PRE><P>bat will now contain {1, 2, 3, 4, 5, 6} and
</P>
<P>baz will now contain {1, 2, 3, 4, 5 ,6}
</P>
<P>But of course, that's longer and using the extra variable name "bat"
doesn't serve much purpose; usually in cases like this you'll never
use the bat variable again.
</P>
<P>Using the shorter version may seem a little less clear at first, but
you end up doing this sort of thing so often that you quickly get used
to it, and it's worth shortening it that way because it makes the code
less cluttered and that makes it easier to read.
</P>
<P><B>Note:</B><EM> Sometimes it may make the code more clear if you be more
explicit, as in the longer version. Sometimes making it shorter makes
that bit a little less clear but makes it easier to understand the
rest of the code. That's a judgement call that you'll learn to make
as you get better at programming. In fact a great majority of
programming is making that kind of judgement call.</EM>
</P>
<P>You can use the range operator (..) in a list index to indicate a
range of elements. So for example if you wanted to get the second,
third and fourth element of a list, you'd index mixedlist[2..4].
</P>
<PRE>foo = {"a", "b", "c", "d", "e", "f"} ;
bar = foo[2..4] ;</PRE><P>bar will now contain {"b", "c", "d"}.
</P>
<P>MOO list indexes and ranges can't do the things that fancier languages
can, they have to be just a single number [4] or two numbers separated
by two periods [2..4]. You have to do any fancy math yourself. For
example, if you want to get from element 3 to the end of the list, you
have to use the built-in function length() to get the length of the
list:
</P>
<PRE>foo = {"a", "b", "c", "d", "e", "f"} ;
bar = foo[2..length(foo)] ;</PRE><P>You'll also want to take a close look at the $list<EM>utils functions ("help $list</EM>utils").
</P>
<H3>Eval</H3><P>Eval is a really, really handy command that basically allows you to write little one-line MOOCode programs and execute them. It works just the same as say or emote, e.g.
</P>
<P>Enter: "foo<enter><BR>
See: You say, "Foo"
</P>
<P>Enter: :grins toothily<enter><BR>
See: Puff grins toothily.
</P>
<P>Enter: ;1+1<enter><BR>
See: =>2
</P>
<P>One minor but important distinction is that only you see the results
of your eval.
</P>
<P>Eval is handy for exploring and experimenting, and also really useful
for setting and looking at property values on your own objects, and
directly invoking verbs.
</P>
<P>One of the first property values you should set with eval is your
player.eval_env property. This may already be set for you; to check,
just type the following:
</P>
<PRE>;player.eval_env<enter></PRE><P>You should see:
</P>
<PRE>=> "here=player.location;me=player;"</PRE><P>If it's not set, type the following:
</P>
<PRE>;player.eval_env="here=player.location;me=player;"<enter></PRE><P>Once you execute this command, you'll be able to use the eval
shortcuts "me" for your player object, and "here" for your player
object's current location. This turns out to be incredibly handy.
</P>
<P>There's also a nifty little property, player.eval_ticks. See the
section below "Threads, Ticks and Tasks" for more on that.
</P>
<P>I never really got in the habit of using multi-statement
evals. Generally I find when I hit that point that it's simpler/easier
to just create a real verb to mess with. "Help eval" will explain how
it works, but here's the basics:
</P>
<P>You start a multi-line eval with a double-semicolon, i.e.:
</P>
<PRE>;;foo = {1, 2, 3}; bar = {4, 5}; for x in ({@foo, @bar}) me:tell(x) ; endfor<enter>
</PRE><P>Also note that multi-line evals don't automatically print the return
value of the statement, so you need to explicitly print whatever you
want to see in the body of your eval statement by calling me:tell(). The built-in function
toliteral() is helpful for this, you can just call:me:tell(toliteral(whatevervariable)):
</P>
<PRE>>;;foo = {"a", "b", "c", "d", "e", "f"} ; bar = foo[2..length(foo)] ; me:tell(toliteral(bar)) ;
{"b", "c", "d", "e", "f"}
=> 0
[used 161 ticks, 0 seconds.]
</PRE><H2>Basic MOOCode Gotchas</H2><H3>Comments</H3><P>Effectively, MOOCode doesn't have comments. MOO actually has
comments, but nobody uses them and nobody, AFAIK, ever has. I can't
even remember what the comment character is - // maybe?
</P>
<P>The problem is, early MOO was designed with the idea that the users
would store/maintain their own source code, and thus the compiler
discarded comments, didn't bother to save them in the compiled verb
bytecodes. When users used the in-server editing tools, which decompiled
the code for the user to edit it, presto, the comments were gone.
(This compiling/decompiling also led to the happy accident that code
indenting/formatting is defacto specified by the decompiler.)
</P>
<P>As a result of this, the custom of using string literals for comments
arose. E.g.:
</P>
<PRE>"this is a string literal" ;</PRE><P>This is so ingrained that by default the help command "help
objectname:verbname" will give the user any string-literal comments at
the top of of the verb source.
</P>
<P>So, current usage is to use string-literals, as follows:
</P>
<PRE>
"this is a MOOCode faux comment." ;
</PRE><P>Of course, note that you need a semi-colon to terminate the line, and
of course any embedded quotes must be escaped with a backslash, i.e.:
</P>
<PRE>
"this is a MOOCode faux comment with some \"embedded quotes\" in it." ;
</PRE><H2>Variable Declaration and Scoping</H2><P>In the MOO world you have objects, which have verbs and properties.
A verb is a method, function, subroutine, whatever. It's invoked on
an object using the ":", like so:
</P>
<PRE>
object:verb(argument)
</PRE><P>Objects, and data stored in properties on objects are the only
persistent things in the moo world.
</P>
<P>Within a verb, variables are dynamically declared, and exist only
within the scope of the verb. If you call out to another verb and
include parameters in that call, the value is passed, not the original
variable (i.e. all verb arguments are pass-by-value). The second
verb can modify the parameter value all it wants, but the original
variable, in the first verb, will remain unchanged.
</P>
<P>Note that although a verb call can pass an object, it's really passing
an object reference by value. The second verb can't do anything to
that object reference to change the object reference in the first
verb. But the second verb <EM>can</EM> use that reference to access verbs
and properties on the object, thus changing the persistent object.
</P>
<H3>A Short Digression on Dynamically Typed Variables</H3><P>Moo code is dynamically typed. This means that you don't have to go
through a lot of bureacracy to set up a variable, you can just throw a
variable assignment in anywhere and presto! It's a variable.
</P>
<PRE>
foo = 3 ;
</PRE><P>This is a lot more convenient (and fun) but it also gives you a lot
more rope to hang yourself. You can be in the middle of a verb and
accidentally typo for variable name as "foop" instead of "foot" (don't
ask me how you typoed a "p" instead of a "t", maybe you have a weird
keyboard layout) and moo will happily create the new variable "foop"
and assign it, leading to all sorts of unexpected craziness.
</P>
<P>Fortunately, hanging yourself isn't all that dangerous in the MOO
world, so you'll usually get a chance to spot the problem and fix it.
</P>
<P>However, unlike a lot of dynamically typed languages, moocode isn't as
friendly at letting you mix types. In perl, for example, you could do:
</P>
<PRE>$message = "The number is " + $somenumvariable; </PRE><P>And perl will try to Do Something Useful and assume that you really
meant to convert $somenumbervariable into a string and concatenate
them. However, in moocode, the compiler will complain because
somenumvariable is a number, not a string. You have to use the
builtin function tostr() to convert it to a string:
</P>
<PRE>message = "The number is " + tostr(somenumvariable); </PRE><P>There are other towhatever() builtins, mainly tonum() which converts
to a numeric value and toobj() which converts to an object reference
number. See "Type Conversion" below.
</P>
<H2>Perms and Args</H2><P>You can see the existing verbs on your object using the @display
command (I consider this command indispensable as an object browser).
</P>
<P>You can set the permissions using @chmod.
</P>
<P>You can set the parser arguments using @args.
</P>
<P>Now we get into how the args work. This immediately gets into parsing
and matching, since that's the whole point of verb args. These topics
deserve a section of their own, and I hope to find time to write it.
However, you can't really do any moocoding without some arg basics, so
here's a short primer meant to give you just sufficient info to get
started.
</P>
<P>In moo programming, "arguments" generally refers to the somewhat
Zork-like command-line parser, though "@args" refers to the verb
arguments.
</P>
<P>When somebody types an interactive moo command, like "look at
objectname", which ends up making your verb code run, any arguments
included in that interactice command (called the invocation) will be
stored in a list. That list will be automatically available to your
verb code, stored in a variable named "args". In your verb code,
usually one of the first things you do is use the @ sign (i.e. @args)
to expand that list. So "@args" became moo coder shorthand for
referring to the parsed verb argument list.
</P>
<P><B>Note:</B><EM> args is what is called a "context variable". There are some
other standard context variables, some of them are discussed in the
following paragraphs.</EM>
</P>
<P>Command-line arguments consist of three elements, (after the verbname) e.g.:
</P>
<LI> verbname any in this
<LI> verbname this to any
<LI> verbname any in any
<LI> verbname none none none
<LI> verbname any any any
<P>And so forth.
</P>
<P>The "foo preposition bar" form gets automatically
parsed by the MOO's command-line parser; it finds the verb name and
argument structure that best matches, and invokes it.
</P>
<P>"this" is a string that matches of the object the verb is defined on,
i.e. the object name or one of the object aliases (see note below on
aliases).
</P>
<P>"any" is any string. It might match an object in the same location
(room) or it might just be a string that the user arbitrarily chose.
Your verb code has to decide what to do with that.
</P>
<P><B>Note:</B><EM> Besides the object.name property, which just about every object
in the moo db has, most objects also have object.aliases, which
contains a list of alternative names for the object. These are also
used by the arg parsing and object matching code.</EM>
</P>
<P>There are some rules for precedence - the parser will try to match to
player:verbname() before trying player.location:verbname(), and so
forth.
</P>
<H3>Context Variables</H3><P>As mentioned above, the "args" variable is automatically available
to any code inside a verb. The args variable contains a list of
all of the arguments passed into the variable. Since it's so often
immediately expanded with the @ sign, it's often referred to ind
discussion as "@args".
</P>
<P>The args variable is one example of a "context variable". There are
several of these super handy context variables. Besides the
parser-related ocntext variables I'll get into below, there are:
</P>
<LI> "this", contains the object reference for the object that the verb is defined on.
<LI> "player", contains the object reference for whichever player started the current chain of execution, usually by invoking a verb interactively, using the command line.
<LI> "here" and "me" aren't actually context variables, but they're customarily defined in your player.eval_env property for use with the eval command, both of which are discussed further below.
<P><B>Note:</B><EM> player doesn't <STRONG>always</STRONG> contain the invoking player's object
reference, there's a special built-in function named
set_task_perms() that will set it to the verb owner's
player object reference, or if the verb owner has wizard perms
(player.wizard=1), can be used to set it to an arbitrary player. This
gets into tricky moo security topics, however. If you're curious
about it, also see the built-in function callers().</EM>
</P>
<P>Somewhat related to the topic, there're also several standard error or
result code values that are available in verb code, much like the $
variable. These are mainly related to the built-in function typeof(),
and are discussed a little in the section "Type Conversion" below.
</P>
<H3>Parser-related Context Variables</H3><P>When the parser invokes the verbs, it pre-populates several special
context variables that each verb starts with; argstr, dobjstr,
iobjstr, and several others. Let's look at an example:
</P>
<P>You have an object named "box" with a "put" verb and the arguments
"any in this", which defined by using the @verb command:
</P>
<P>@verb box:put any in this
</P>
<P>You could also define it in two steps:
</P>
<P>@verb box:put
@args box:put any in this
</P>
<P>You type "put gold in box".
</P>
<P>The parser looks for a player:put command, doesn't find one
</P>
<P>The parser looks for a room:put command, doesn't fine one
</P>
<P>The parser looks for a "gold" with a put command and the appropriate args, doesn't find one.
</P>
<P>The parser looks for a "box" with a put command and the appropriate args, and gets a match.
</P>
<P>The parser invokes box:put().
</P>
<P>The code inside the verb box:put can assume that there will be a
dobjstr (direct object string) variable containing the string "gold"
and an iobjstr (indirect object sring) variable containing the string
"box".
</P>
<P>The parser will also try to match the dobjstr (direct object string)
and iobjstr (indirect object string) and set the dobj variable and
iobj variable for the verb to any matched objects.
</P>
<P>Note that verbs with args of "none none none" aren't meant to have
parameters at all.
</P>
<P>Args of "any any any" is for verbs that expect to do some sort of
custom parsing on their own.
</P>
<H4>this none this and @chmod +x</H4><P>There's also a special set of arguments, "this none this" for verbs
meant to be invoked only by other verbs:
</P>
<P>Generally speaking, if your verb doesn't have its args set to "this
none this", you won't be able to invoke it from another verb.
</P>
<P>In addition, such not-to-be-directly-invoked verbs must be @chmodded
to +x, to allow them to be invoked. If they're not +x, the moo will
report a rather confusing "verb not found" error.
</P>
<P>@chmod +x box:put<EM>helper</EM>verb
</P>
<P>The reason for this is that not-to-be-directly-invoked verbs are
expected (in well-designed code) to do the heavy lifting, the
important and tricky stuff, and Pavel (or possibly Ghond) wanted to
avoid having lots of half-done code be invocable by anybody.
</P>
<H3>Flow Control: if, for, while, suspend, fork</H3><P>A simple list of instructions, one after the other, is technically a
program, but you probably want some more sophisticated options than
just a straight one-after-the-other list of instructions. You want
things like being able to check something and execute one set of
instructions or the other (if/endif), or being able to repeat an
instruction some number of times (for/endfor) .
</P>
<P>The built-in functions suspend() and fork() aren't really technically
flow control operators, but they play a similar role, so I'm going to
talk about them here.
</P>
<P>Some general rules of thumb:
</P>
<LI> Every flow control structure starts with a keyword (if, for, while).
<LI> Every flow control structure ends with a matching keyword that starts with "end" (endif, endfor, endwhile).
<LI> The lines between the start and end are called a "block" of code.
<LI> The block is customarily indented 4 characters. That's only for readability purposes, but the moo compiler/decompiler automatically does it that way.
<LI> The line that starts the flow control structure usually contains an expression.
<P>Let's jump right in and show some examples of if, while, for, suspend