For instance, I might want to allow 3 different rings to be worn
simultaneously, but I wouldn't want to allow 3 different hats to be
worn simultaneously. How do I make it so that trying to put one hat on
causes any worn hat to be taken off first?
If you want the player to *automatically* remove one hat before putting
on another, you'd have to define a new precondition. The code below
seems to work, and is modeled on ObjOpenCondition, one of the library's
more easily understandable preconditions.
HTH...
Greg
class Hat: Wearable
dobjFor(Wear) {
// switch the order of these preconditions if you want
preCond = [notWearingHat, objHeld]
}
;
greenHat: Hat 'green hat' 'green hat' @startRoom;
redHat: Hat 'red hat' 'red hat' @startRoom;
modify Actor
curHat {
for(local cur = firstObj(Hat); cur != nil;
cur = nextObj(cur, Hat)) {
if (cur.isWornBy(self))
return cur;
}
return nil;
}
;
notWearingHat: PreCondition
checkPreCondition(obj, allowImplicit) {
// if gActor is already not wearing a hat, we're already done
if (gActor.curHat == nil)
return nil;
// try to implicitly remove the hat
if (allowImplicit && tryImplicitAction(Doff, gActor.curHat)) {
// We just tried the implicit command. If it didn't work,
// abort.
if (gActor.curHat != nil)
exit;
// tell the caller we executed an implied command
return true;
}
// can't remove the hat implicitly; report the failure
conditionFailed(obj);
exit;
}
// The condition failed; report the failure and give up.
conditionFailed(obj) {
// can't open it implicitly - report failure and give up
reportFailure('{You/he} {can\'t} do that because {you\'re}
wearing a hat already. ');
// make it the pronoun
gActor.setPronounObj(obj);
}
;
That works perfectly! Thanks!