Answer to Exercise 86

| ANSWER TO EXERCISE 86
| The label object itself is not too bad:
    Object -> label "red sticky label"
      with name "red" "sticky" "label",
           number 0,
           before
           [;  PutOn, Insert:
                   if (self.number~=0)
                   {   print "(first removing the label from ",
                       (the) self.number, ")^"; self.number=0; move self to player;
                   }
                   if (second==self) "That would only make a red mess.";
                   self.number=second; remove self;
                   print_ret "You affix the label to ", (the) second, ".";
           ],
           react_after
           [ x; x=self.number; if (x==0) rfalse;
               Look: if (x in location)
                       print "^The red sticky label is stuck to ", (the) x, ".^";
               Inv:  if (x in player)
                       print "^The red sticky label is stuck to ", (the) x, ".^";
           ],
           each_turn
           [;  if (parent(self)~=0) self.number=0; ];
Note that `label.number` holds the object the label is stuck to, or 0 if it's unstuck: and that when it is stuck, it is removed from the object tree. It therefore has to be moved into scope, so we need the rule: if the labelled object is in scope, then so is the label.
    Global disable_self;
    [ InScope actor i1 i2;
      if (label.number==0) rfalse; if (disable_self==1) rfalse;
      disable_self=1;
      i1 = TestScope(label, actor);
      i2 = TestScope(label.number, actor);
      disable_self=0;
      if (i1~=0) rfalse;
      if (i2~=0) PlaceInScope(label);
      rfalse;
    ];
This routine has two interesting points: firstly, it disables itself while testing scope (since otherwise the game would go into an endless recursion), and secondly it only puts the label in scope if it isn't already there. This is just a safety precaution to prevent the label reacting twice to actions (and isn't really necessary since the label can't already be in scope, but is included for the sake of example).

Back to the exercise in section 28

Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.