NPC Accompanying the PC into a Booth

The Tour Guide has a nice example (on the AccompanyingState page) of how to get an NPC to follow the PC into a NestedRoom using afterAction. But when I add that code to my game, the NPC waits until the following turn to move into the NestedRoom. Normal AccompanyingState behavior has the NPC following the PC in the same turn.

Oddly enough, the NPC will get out of the NestedRoom on the same turn when the PC leaves it, but there’s a one-turn delay when the PC enters the NestedRoom.

How can I get the NPC to move into the NestedRoom on the same turn as the PC?

The problem I’m having is this: Once in the NestedRoom, the player can immediately give a command that causes the NestedRoom to whiz off to a different location. If that happens, the NPC won’t be able to enter the NestedRoom at all, because it will be gone. There will be no containment path between the NPC and the NestedRoom.

Since NestedRooms don’t work like normal rooms for arrivals and departures, you’ll need to adapt the second AccompanyingState example in the tour guide, and move the NPC into the NestedRoom directly.

EDIT: Hmm. I need to read more carefully, and test. Hang on.

OK, I’ve tested this and it should work.

modify Actor
	travelWithin(dest) {
		nestedDest = dest;
		inherited(dest);
		nestedDest = nil;
	}
	nestedDest = nil
;

me: Person
	vocabWords = 'self'
	location = bedroom
;

cat: UntakeableActor 'cat' 'cat' @bedroom
	curState = catFollowing
;

+ catFollowing: AccompanyingState
	specialDesc {
		if (getActor.isIn(pethouse))
			"The cat is hungry, not tired! ";
		else
			"The cat is hungry! ";
	}
	stateDesc = "The cat wants food! "
	accompanyTravel(leadActor, conn) {
		if (!conn && leadActor.nestedDest) {
			getActor.moveInto(leadActor.nestedDest);
			gLibMessages.sayDepartingWith(getActor, leadActor);
			return nil;
		}
		return leadActor == gPlayerChar;
	}
;

bedroom: Room 'bedroom' 'bedroom'
	"Just another bedroom. "
	west = bathroom
;

+ pethouse: Booth 'pet house' 'pet house'
	"The cat sleeps in there. "
;

Thanks! That works just fine … except that I don’t want gLibMessages.sayDepartingWith. I want my own custom messages. If I just comment out that line and replace it with a double-quoted string, then my string is printed both when the NPC hops into the NestedRoom and when she climbs out. So I’m trying to test the value of nestedDest, and that isn’t working yet.

I’ll keep hammering at it.

nestedDest will point to either the Room or the NestedRoom object, so you can test it that way.

	accompanyTravel(leadActor, conn) {
		if (!conn && leadActor.nestedDest) {
			if (leadActor.nestedDest == pethouse)
				"The cat comes with you, but is not happy! ";
			else
				gLibMessages.sayDepartingWith(getActor, leadActor);
			getActor.moveInto(leadActor.nestedDest);
			return nil;
		}
		return leadActor == gPlayerChar;
	}

Got it, thanks. I stumbled because I was testing nestedDest instead of leadActor.nestedDest.

If I end up needing a second NestedRoom I’ll have to fiddle with this, but that shouldn’t be difficult.