## Reusing Code for or from Events

When an event is defined inside an object type and a new set of commands are defined:

    objectType someObject ("whatever") {
       event someAction {
          # Do stuff here
       }
    }

...this is equivalent to the following code:

    sub myFunction () {
       # Do stuff here
    }

    objectType someObject ("whatever") {
       event someAction = myFunction;
    }

Using the second method, the name of the function is obviously myFunction. Using the first method, with the code defined inside the object type itself, the code is stored in a function with no parameters, the name of which is the name of the object, followed by a period, followed by the name of the action. So, in the first example, the function is created with the name doorFromAToB.lookAt. This makes it possible to access events directly without using the callEvent command and to reuse predefined actions for new events:

    objectType doorFromAToB ("big metal door") {
       event lookAt {
          say (ego, "It's only a door. Never seen a door before?");
       }
    }

    objectType doorFromCToD ("tiny wooden door") {
       event lookAt = doorFromAToB.lookAt;
    }

    objectType doorFromEToF ("purple door") {
       event lookAt {
          doorFromAToB.lookAt ();
          say (ego, "The hinges are a bit rusty...");
       }
    }

Or, as is perhaps clearer to read:

    sub lookAtDoor {
       say (ego, "It's only a door. Never seen a door before?");
    }

    objectType doorFromAToB ("big metal door") {
       event lookAt = lookAtDoor;
    }

    objectType doorFromCToD ("tiny wooden door") {
       event lookAt = lookAtDoor;
    }

    objectType doorFromEToF ("purple door") {
       event lookAt {
          lookAtDoor ();
          say (ego, "The hinges are a bit rusty...");
       }
    }

(Note that because the events in the first two object types above reuse an existing function, there are no new functions created called either doorFromAToB.lookAt or doorFromCToD.lookAt.)

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