Unlocking the door to let an NPC in a room

There is a room with a lockable door and the player is inside it. A certain number of turns in the room, an NPC enters the room. If the room is locked, the game will have to tell the player to unlock the door. If it isn’t, the NPC can enter.
At first, I tried using truth state and if.
friendenter is a truth state that varies.
If door A is unlocked, friendenter is true.
But that wasn’t accepted by Inform.

I did some more searching and tried making a scene for the scenario when the door is locked:
friendenters is a scene.
friendenter begins when the player is in room A for the third turn and door A is locked.
When friendenter begins:
** say “You hear a knock on the door. Perhaps your friend is outside.”.**
friendenter ends when door A is unlocked.

But somehow that didn’t work as expected.
How can I code such a scenario correctly?

Hi,

Please use the preformatted text tags for your code. It makes things easier for everyone. You’ll be able to tell if you’ve got the right one by checking the preview box to the right of the reply box you type into.

There are a few things wrong with your first code, but I’ll save that for later, or perhaps someone else. As far as your scenario, this could get you started:

Lab is a room. "You'll have to wait here."
North of the lab is a door called the back door.
The back door is closed, lockable and locked.
The red key unlocks the back door.

The player carries the red key.

North of the back door is the yard.
Al is a man in the yard.

Al Arrives is a scene.
Al Arrives begins when the player is in the Lab for the third turn.
Al Arrives ends when Al is in the Lab for the first time.

When Al Arrives begins:
	if the back door is locked:
		say "You hear a knock at the back door. Could it be Al?";
	otherwise:
		try Al going south;
		say "Al says, 'Hi!".

Every turn during Al Arrives:
	if Al is in the yard and the back door is locked:
		say "There's that knocking again.".

After unlocking the back door with the red key during Al Arrives:
	say "Al walks in and says, 'Thanks for letting me in.'";
	silently try Al going south.
	
test me1 with "z / z /  z / unlock door".
test me2 with "unlock door / z".

This worked perfectly! Thanks for the help, and I’ll definitely use text tags next time.

So the solution involving scenes seems like the best one here, but just for future reference, I wanted to talk about how to do what you were trying with this:

This sounds like you’re trying to define a condition, as in §11.16 of the documentation. You can do that like this:

To decide whether friendenter:
	if the back door is locked, yes;
	no.

Then you can use “friendenter” in conditions. (Though in this case you could also just say “the back door is locked” without defining a phrase! This helps when more complex definitions are required.)

§11.16 and §11.17 have more stuff you can do with this (and there’s even more fancy similar stuff you can do with relations, but never mind that now).

If you define something using friendenter is a truth state that varies, then you will always have to set its truth value yourself using now friendenter is false or now friendenter is true. Defining something as a variable and with a “to decide” phrase is a common pitfall that leads to really annoying and hard-to-diagnose bugs, because it tends to compile but not do what you want.

The other issue is that you can’t have an “if” statement on its own–it always has to be in a rule or phrase.

2 Likes

Thank you for putting that so succinctly. I knew someone would come along and fill in the part I was too tired to go into. :wink:

You can use backticks – they look like this ` – to demarcate your code if you prefer. The backtick key (on my keyboard) is the one to the left of numeral 1 on the top row, above the TAB key. Just use three backticks at the point where you want your code to start, and then another three at the point where you want to end your code and switch back to “normal” posting style.