# Help on NPC

I have this NPC that randomly goes to a near by dark room. The player is suppose to kill it however when after you defeat it it says “[** Programming error: nothing (object number 0) has no property IK1_Count to read **]” Help!

Code:

``````The Shadow Beak is a neuter animal in Labyrinth 15.

Every turn:
if a random chance of 1 in 2 succeeds:
let current location be the location of the Shadow Beak;
let next location be a random room which is adjacent to the current location;
if the Shadow Beak is visible:
say "The Shadow Beak [one of]slouches[or]slithers[or]shambles[or]lurches[at random] away.";
move the Shadow Beak to next location;
if the Shadow Beak is visible:
say "A Shadow Beak [one of]oozes[or]staggers[or]ambles[or]creeps[at random] into the room.";

say "With your bare fists? Surely you can think of a better way!" instead.

let X be a random number from 1 to 5;
if X is 1:
say "You killed the Shadow Beak!";
remove the Shadow Beak from play;
now the Nightmare Fuel is in the location;
otherwise if X is 2:
end the story saying "You have been killed by a Shadow Beak.";
otherwise:
``````

This looks like the kind of error you get when your code is trying to use a property of something that might not exist.

In this case, after you kill the Shadow Beak, its location is nowhere. So when you try for “a random room which is adjacent to the current location,” you’ve set the current location to nowhere, which doesn’t have any rooms adjacent to it. So you’ve got trouble.

One way to solve for this would be to check whether the Shadow Beak is in play in the heading of the rule, like “Every turn when the Shadow Beak is not nowhere:”.

Another potential issue comes from trying to use “a random [list of something]” when there might not be anything on the list. That can lead to errors too, so sometimes you may want to check whether there is such a thing before you use the “a random…”

Hope this helps!

By the way, it’s a good idea to enclose your code in code tags, by putting three back ticks ``` on separate lines before and after your code. That makes it easier to read, preserves the indentations, and creates a button that allows people to copy the code for testing purposes. (I’m about to edit that into your post.)

2 Likes

I took a look at the code and here is a guess. Every turn there is a 1 in 2 chance that you will need to move the Shadow Beak but there is no check to determine if the Shadow Beak is still in play. I would conditionally check to see if the Shadow Beak is still in play before the random 1 in 2 chance that you need to do anything with it. I would guess that your error is coming up after the Shadow Beak is removed from play and you hit the case on the next turn where the 1 in 2 chance succeeds and then attempts to move the Shadow Beak to next location.

I did a quick prototype and here is some code that will run standalone. It checks to see if the Shadow Beak is anywhere before attempting the 1 in 2 check every turn. I was not able to get an error to occur after killing the Shadow Beak with this addition.

``````Labyrinth 15 is a room.
Labyrinth 16 is a room.
Labyrinth 16 is west of Labyrinth 15.
The Shadow Beak is an animal in Labyrinth 15.
There is a Crowbar in Labyrinth 15.
Nightmare Fuel is a thing.

Every turn:
if the Shadow Beak is anywhere:
say "Shadow Beak is still wandering around... [line break]";
if a random chance of 1 in 2 succeeds:
let current location be the location of the Shadow Beak;
let next location be a random room which is adjacent to the current location;
if the Shadow Beak is visible:
say "The Shadow Beak [one of]slouches[or]slithers[or]shambles[or]lurches[at random] away.";
move the Shadow Beak to next location;
if the Shadow Beak is visible:
say "A Shadow Beak [one of]oozes[or]staggers[or]ambles[or]creeps[at random] into the room.";
otherwise:
say "Shadow Beak has been vanquished... [line break]";

say "With your bare fists? Surely you can think of a better way!" instead.