Other Responses to Actions
So far we have concentrated on how you can customise the responses to actions on objects directly involved in those actions as direct or indirect object, and that will probably be the most common type of action customisation you'll perform. But there are other types of response we should look at for the sake of completeness. (NOTE: This discussion assumes that gameMain.beforeRunsBeforeCheck is nil, otherwise stopping an action in check() will not prevent beforeAction() and roomBeforeAction() from being called on other objects, although everything else will be the same).
If you want an object not directly involved in a command to respond to it, you can use beforeAction() or afterAction(). As their names suggest, the first of these responds to the action just before it's performed (i.e. just before the appropriate action routine is invoked) while afterAction() is called just after an action is performed. These two routines are called on every object that's in the actor's scope when the action is performed, but only if the command reaches the action stage (i.e. it hasn't already been ruled out by verify(), check(), or preCond). Thus, if you want another object to respond to an action that fails, you must make it fail in action(), rather than before, e.g.
|
"Boy is she ugly! "
isHer = true
dobjFor(Kiss)
{
verify() { }
action()
{
reportFailure('You move your lips towards hers, but your
nerve fails you at the last moment. ');
}
}
;
|
"He looks a fine young man. "
isHim = true
isProperName = true
beforeAction()
{
if(gActionIs(Kiss) && gDobj == wickedWitch)
{
"<q>Hey, what do you think you\'re doing!</q> cries Bob,
grabbing you by the arm and pulling you back, <q>Don\'t you
  ; know that kissing her will turn you into a lump of vile green
blancmange?</q> ";
exit;
}
}
;
|
|
"Hey, what do you think you're doing!" cries Bob, grabbing you by the arm and pulling you back, "Don't you know that kissing her will turn you into a lump of vile green blancmange?"
|
|
You move your lips towards hers, but your nerve fails you at the last moment.
|
|
{
if(gActionIs(Kiss) && gDobj == wickedWitch)
{
"<q>Wise decision</q> Bob approves, <q>I suppose you realize
that if you had gone ahead and kissed her you\'d have been
turned into a lump of vile green blancmange!</q> ";
}
}
|
|
"You cling precariously to the trunk, next to a firm, narrow branch."
down = clearing
enteringRoom(traveler)
{
if(!traveler.hasSeen(self) && traveler == gPlayerChar)
addToScore(1, 'reaching the top of the tree. ');
}
&n bsp;roomBeforeAction()
{
if(gActionIs(Jump))
failCheck('Not here -- you might fall to the ground and
hurt yourself. ');
}
roomAfterAction()
{
if(gActionIs(Yell))
"Your shout is lost on the breeze. ";
}
;
|
actorAction() { if(isTiedUp && !gAction.ofKind(SystemAction) && !gActionIn(Look, Inventory, Examine) { "You can't do that while you're tied up. "; exit; } }
Finally, note that we've introduced a few more of those things beginning with g in the
last page or so. Remember that gAction
means the current action; gActionIs(Whatever)
tests whether gAction
is Whatever
and returns true or nil accordingly; gActionIn(Foo, Bar, Whatsit)
tests whether gAction
is either Foo
or Bar
or Whatsit
and returns true if it is or nil otherwise. The other one, gMessageParams(obj)
, is a bit different; it's used to allow obj
to be used in message parameter strings (like '{the obj/him}'
) where obj
is the name of an existing local variable. This allows us to write:
cannotPutComponentMsg(obj) { gMessageParams(obj); return 'You can\'t do that, because it\'s part of {the obj/him}. '; }
Instead of:
cannotPutComponentMsg(obj) { return 'You can\'t do that, because it\'s part of ' + obj.theName + '. '; }
Getting Started in TADS 3
[Main]
[Previous] [Next]