## Setting Default Events

In the Object Types and Events section of this help file this code was given as an example:

    objectType ego ("") {
    }

    objectType lookAt ("Look at") {
    }

    objectType pickUp ("Pick up") {
    }

    objectType box ("box") {
       event lookAt {
          say (ego, "It's full of junk, none of which looks useful.");
       }
    }

    objectType hat ("old battered top hat") {
    }

However, if your code was to call one of the following lines:

    callEvent (lookAt, hat);

    callEvent (pickUp, box);

    callEvent (pickUp, hat);

...nothing would happen, as there are no events specified which match the parameters given. Therefore, in order to provide default reactions it is a good idea to create a default object type as follows:

    objectType default ("") {
    }

The lookAt and pickUp object types could then be expanded to include default code:

    objectType lookAt ("Look at") {
       event default {
          say (ego, "Nothing interesting to look at there.");
       }
    }

    objectType pickUp ("Pick up") {
       event default {
          say (ego, "I can't pick that up.");
       }
    }

Of course, the callEvent function doesn't know how to resort to this default behaviour, so it's best to provide your own function instead, such as this:

    sub findEvent (action, object) {

       # callEvent returns a value of TRUE if it's worked, so...
       if (callEvent (action, object) != TRUE) {
          callEvent (default, action);
       }
    }

Of course, you'll probably want to combine objects with other objects - and sooner or later you'll probably forget a default event somewhere. Also, combining object A with object B should probably have the same effect as combining object B with object A. So, let's make our findEvent function even more complicated...

    sub findEvent (action, object) {
       if (callEvent (action, object)) return;
       if (callEvent (object, action)) return;
       if (callEvent (default, action)) return;
       if (callEvent (default, object)) return;
       say (ego, "I can't use these things together.");
    }

So let's say your function called:

    findEvent (box, hat);

First it would try to use the box with the hat. Then, because there was no event for that, it would try to use the hat with the box. Then it would try to use the default action in the box object type. Then the default action in the hat object type. Finally, because none of those events exist, the script would resort to making the ego say the immortal line "I can't use these things together."

SLUDGE and this SLUDGE documentation are copyright Hungry Software and contributors 2000-2012