How to stop a Follower from Following

Hi again.

I hope this is an easy question to answer.

I have an npc following the player around but I want them to stop once they get “trapped”. The problem is that once they are trapped the following routine continues and starts throwing the

You’ll have to say which compass direction to go in.
I didn’t understand that instruction.

error because the NPC can’t move.

1 Like

This should be pretty easy, I’d just use a property:

The hallway is a room.  The lounge is south of the hallway.  The kitchen is west of the lounge.  The foyer is north of the kitchen.  The foyer is west of the hallway.

An animal is either trapped or free.  An animal is usually free.

The bloodhound is an animal in the lounge. 

Every turn when the bloodhound is not in the location and the bloodhound is free:
	let the way be the best route from the location of the bloodhound to the location, using doors;
	try the bloodhound going the way.
	
The dog treat is in the kitchen.

The block giving rule does nothing when the noun is the dog treat and the second noun is the bloodhound.

Carry out giving the dog treat to the bloodhound:
	Now the bloodhound is trapped.
	
After giving the dog treat to the bloodhound:
	Say "The bloodhound munches down the treat and curls up, sated."
5 Likes

I’d probably have reached for a scene…

The hallway is a room.  The lounge is south of the hallway.  The kitchen is west of the lounge.  The foyer is north of the kitchen.  The foyer is west of the hallway.

The bloodhound is an animal in the lounge. 
The dog treat is in the kitchen.

Pursuit is a scene.
Pursuit begins when the location is not the location of the bloodhound.
Pursuit ends when the bloodhound has the dog treat.
Every turn when Pursuit is happening and the location is not the location of the bloodhound:
  try the bloodhound going  the best route from the location of the bloodhound to the location, using doors.
	
The block giving rule does nothing when the noun is the dog treat and the second noun is the bloodhound.
	
After giving the dog treat to the bloodhound:
	Say "The bloodhound munches down the treat and curls up, sated."

Offered just for demonstration purposes of how one would use a scene for this. There isn’t any way this is better than Mike’s approach; in fact, I think Mike’s is a little more straightforward and easy to follow. I didn’t know that that would be the case until after I’d written this – I thought the scene implementation might have a slim edge – but having written it, I figured I’d might as well post it. :grinning:

3 Likes

Thanks @DeusIrae and @Zed.

I’ll take a run at implementing Mike’s and see how it goes. I’m sort of pleased with myself for having considered using scenes (despite not fully understanding their implementation).

While I think of it, what is the practical purpose of…

The block giving rule does nothing when the noun is the dog treat and the second noun is the bloodhound.
	
1 Like

@DeusIrae I’ll give this a shot. I’m hopeful that a location can also trigger a change in state.

1 Like

@DeusIrae and @Zed here’s how I implemented this

every turn when the player is in the threshold and mrs whip is in the class and mrs whip is free and mrs whip does not enclose the sticky tack:
	now mrs whip is trapped;
	remove mrs whip from play.

Coming from some pretty basic programming to Inform7 I always feel that there must be a better way to do stuff that saying “every turn” for this sort of logic. Is there? I don’t think it is a bad solution but I doubt my implementation is optimal most of the time.

1 Like

Short answer: nope, that’s as good as it gets. It’s pretty inevitable that parser-based IF will have some kind of foundational event loop. And in a conventional language, you’d have various routines that would get called during the loop, and they’d call other routines, and in one of those you’d have an if-then a lot like yours above.

And if you substitute “rules” for “routines”, that basically describes how Inform does it, too. You just express it as an every turn rule instead of writing it as a line of code in a routine you know will be called at some given point in the event loop.

If you like, you could add something like this to handle things that were discrete events and don’t feel like something you want to test every turn…

Triggered is a rulebook.
This is the triggered stage rule: follow the triggered rules.
The triggered stage rule is listed before the every turn stage rule in the turn sequence rules.

and your code would become:

Triggered when the player is in the threshold and mrs whip is in the class and mrs whip is free and mrs whip does not enclose the sticky tack:
	now mrs whip is trapped;
	remove mrs whip from play.
2 Likes

Thank you for explaining that. You hit the nail on the head in terms of me imagining wasted resources by doing that every turn. Perhaps I’m showing my age and these programs are so resource light it’s not a concern.

1 Like

The Standard Rules, in their wisdom, block giving. Even if persuasion works, if you tell someone to give you something they’re carrying, it’ll fail with “[name] is unable to do that”. And if you try to give them something, it’s “[name] doesn’t seem interested.” You have to unlist that rule, or selectively negate it like Mike did, for the giving action to be able to succeed.

2 Likes

Hmm… haven’t you just implemented a bonus ‘every turn’ rulebook and called it something different? I can’t see the difference between this and ‘First every turn when the player is in the threshold…’ -either way the rule only triggers when the condition is met…

Notwithstanding the other workable solutions offered, I think you just need this, to avoid ‘try Mrs Whip going nothing’, which is what happens when route-finding can’t find a route (or the start & end of the route is the same place) meaning that ‘the best route’ is nothing rather than a direction

if the way is not nothing:
	try Mrs Whip going the way.

EDIT: alternatively, if you’re going to move Mrs Whip out of play when she becomes trapped, you don’t need a separate ‘trapped’ property: you can just use the fact of her being nowhere as the flag that she’s trapped and use that as a condition to stop her trying to even find a route:

every turn when the player is in the threshold and mrs whip is in the class and mrs whip is free [***'mrs whip is free' is redundant here, since if Mrs Whip has been trapped, she can't be in the class- she'll be nowhere!***]and mrs whip does not enclose the sticky tack:
	now mrs whip is trapped;[***this property isn't really needed***]
	remove mrs whip from play.

...
if the location of mrs whip is not the location of the player and mrs whip is not nowhere: [*** ' and mrs whip is not nowhere' added***]
		let the way be the best route from the location of Mrs Whip to the location of the player, using doors;
		try Mrs Whip going the way;
...
1 Like

yup. The only thing it accomplishes is: if one is happier to see Triggered when... then one will be happier (unless one ends up with some conflict where it matters that all the Triggered rules happen before all of the Every turn rules, in which case one will be sad).

4 Likes