Making the Items do Something
The game is still rather bland; it has no puzzles. So, let's introduce a small puzzle. Let's assume that the gold skull wasn't merely left lying around; instead, whoever left it there arranged for a trap to go off if it should be lifted off the pedestal. To implement this, we need to add a method to the goldSkull object. A method is a special type of property which contains code (i.e. a sequence of one or more statements) to be executed; it is very much like a function in C or Pascal. The new goldSkull with the method looks like this:
|
goldSkull: Thing
|
name = 'gold skull'
|
vocabWords = 'gold skull/head'
|
location = pedestal
|
|
actionDobjTake()
|
{
|
"As you lift the skull, a volley of poisonous
|
arrows is shot from the walls! You try to dodge
|
the arrows, but they take you by surprise!";
|
|
finishGameMsg(ftDeath, [finishOptionUndo]);
|
}
|
;
|
|
|
You might wonder why we didn't need a actionDobjTake method in our original definition of goldSkull, or you might have assumed that the system automatically knows what to do if no actionDobjTake is defined for an object. In fact, all objects do need a actionDobjTake method, and the system doesn't automatically know anything about it. However, since practically every object has the same actionDobjTake, with a few exceptions such as goldSkull, it would be extremely tedious to type a actionDobjTake method for every object in the game. Instead, we use something called "inheritance." By defining the goldSkull to be a member of the Thing class, you tell TADS 3 that goldSkull "inherits" all of the definitions for Thing, in addition to any definitions it makes on its own. The Thing class, which appears in the adv3 library file included at the beginning of the program, defines a actionDobjTake method, so anything that is defined to be a Thing inherits that definition. However, if something is defined in both Thing and goldSkull, as actionDobjTake is in this example, the definition in goldSkull takes precedence - it "overrides" the inherited method.
|
|
smallRock: Thing
|
name = 'small rock'
|
vocabWords = 'small rock'
|
location = cave
|
;
|
|
actionDobjTake()
|
{
|
if (location != pedestal || /* am I off the pedestal? */
|
smallRock.location == pedestal ) /* or is the rock there? */
|
inherited; /* yes - take as usual */
|
else /* no - the trap goes off! */
|
{
|
"As you lift the skull, a volley
|
of poisonous arrows is shot from
|
the walls! You try to dodge the
|
arrows, but they take you by surprise!";
|
|
finishGameMsg(ftDeath, [finishOptionUndo]);
|
}
|
}
|
|
|
|
|
Getting Started in TADS 3
[Main]
[Previous] [Next]