# I7: NPC Arrives

Hi

I’m battling with a problem which I’m sure is easy but I just can’t seem to crack it. I want to detect when an NPC arrives at a location and then immediately carry out some actions. I can get the NPC to report their arrival but whatever I try the action I want to follow just will not run until the player takes a turn.

So, what is the best way to achieve the results I’m after?

To clarify: do we want to do this whenever the NPC arrives at any location, or only when he arrives at a particular room? If it is a particular room, do we want these actions to fire more than once if the NPC goes there more than once, or every turn the NPC stays in the room, or one time only?

Due to the game, I need to have this checked whenever the NPC arrives at any location.

Perhaps you can call the action directly from the code that moves the NPC?

[code]Definition: a person is other if it is not the player.

Every turn:
repeat with monster running through the list of other persons:
if the monster is in a room (called the current space) and a random chance of 1 in 4 succeeds:
let next space be a random room which is adjacent to the current space;
if the monster is visible, say “[The monster] heads to [the next space].”;
move the monster to next space;
if the monster is visible:
say “[A monster] arrives from [the current space].”;
try examining the monster.[/code]

Thanks, never thought about a repeat loop. This was the basis of my solution, just needed to add another loop for all the locations. This will be to check whether other NPC’s are up to stuff out of sight.

Not sure if this will help, but this is the code I used for an NPC who wanders around randomly. If the player enters a location where the NPC is, or if the NPC enters a location where the player is, the game reports it.

[code]There is a man called the cop. Understand “cop” as the cop. Understand “pig” as the cop. Understand “the pig” as the cop. Understand “fuzz” as the cop. Understand “the fuzz” as the cop.

The description of the cop is “The cop wears a blue uniform and a brightly polished tin badge. He looks mean and carries a billy. In fact, he decides to hit you with it just for looking at him.”

The cop wears a badge. Understand “tin badge” as the badge. The description of the badge is “This is the cop[apostrophe]s license to do anything he wants. Right now, he wants to beat the crap out of you.”.

The cop carries a billy. Understand “club” as the billy. Understand “billy-club” as the billy. Understand “billy club” as the billy. Understand “nightstick” as the billy. The description of the billy is “This is a stout wooden club. It is quite worn and is encrusted with bloodstains. Some of the blood is yours.”.

When play begins:
Now the cop is in a random room in The Island.

Cop Roomfound is a truth state that varies. Cop Roomfound is false.
Current Cop Room is a room that varies. Next Cop Room is a room that varies.
Copway is a direction that varies.

To reset cop:
Let R be a random room;
Now next cop room is R;
If next cop room is not current cop room:
Now cop roomfound is true.

Every turn:
Now current cop room is the location of the cop;
Now cop roomfound is false;
While cop roomfound is false repeatedly reset cop;
If the cop is visible:
Choose a random row from the table of cop mayhem;
Say “The cop [action entry], then leaves the area to patrol his beat.”;
Now copway is the best route from current cop room to next cop room, using even locked doors;
Let Theroom be the room copWay from current cop Room;
Move the cop to Theroom;
If the cop is visible:
Choose a random row from the table of cop mayhem;
Say “The cop appears and [action entry].”.

Table of Cop Mayhem
Action
“smacks you on the head with his billy”
“swings his billy at your gut”
“kicks you in a very sensitive place”
“punches you in the nose”