Requiring Items Before Executing an Action

Hi guys,
I have run into a small snag which I’m sure has a simple solution. I’d like to implement a rule which requires a slip of paper to be in the inventory before opening a safe. I have not run into this type of situation as of yet, and would appreciate any help regarding this issue. Here is the work so far–I’m not sure if I’m way off at this point:

The Inside Small House is a room.
	The description of Inside Small House is "This house is as saddening on the inside as it is on the outside. There is a small iron stove, a bed that looks rock hard, and a rug that makes a futile attempt to make the floor softer. You wonder if the floor is really made of cast iron."
	There is a rug on the floor.
	The rug is scenery.
	There is a safe under the rug.
	[The safe is an openable container.
	When the player tries to open the safe with a scrap of paper in the inventory:
		Open the safe;
		Give the player 75 gold coins;
		Say "You dial in the combination that is on the scrap of paper. The safe creaks open, revealing a well-loaded cache of gold. You retrieve 75 gold coins. This will make a nice addition to your paycheck.".;
		Increase the score by 2.
	When the player tries to open the safe without a scrap of paper in the inventory, say "I don't know the combination."]

I’ve put the offending part of the code in brackets as to exclude it temporarily from parsing.
Thanks in advance for any and all solutions. :slight_smile:

For most of this sort of thing you want to write your rule so it applies to the action the player is performing or trying to perform. In this case the action is “opening the safe,” so your rule to prevent the player from opening the safe without the paper is something like this:

Check opening the safe when the player does not hold the scrap of paper: say "I don't know the combination." instead.

The “instead” means that the action stops processing at this point, so the safe doesn’t actually get opened.

For the other one, you could just put 75 gold coins in the safe and let the player take them themselves. There are standard rules for opening the safe that will ensure that the “opening the safe” action opens the safe, if it isn’t blocked by that check rule or something. And there’s a report opening rule that will print an appropriate message (in this case, I think, “You open the safe, revealing 75 gold coins”). If you want you own custom message you can write a rule "After opening the safe: say “blah blah blah, remembering to take account of the case where the gold coins aren’t in the safe.”

Oh, and “There is a rug on the floor” and “There is a safe under the rug” are probably not doing what you want. Inform doesn’t have a world model that allows it to understand that this means that the safe is supposed to be concealed until you lift the rug… or even that any of this stuff is in the room you just specified. What this code does is create:

  1. an supporter called “the floor”
  2. a thing called “the rug” which is on “the floor” supporter (Inform is sophisticated enough to know that “on” means that one thing is on another, which has to be of a kind that you can put other things on, although it doesn’t know that the floor has any special properties)
  3. a thing called “the safe under the rug” (!!)
    all of which wind up in a sort of limbo, not in the room you’ve defined. In fact the “safe under the rug” object has nothing to do with the rug!

You could look at example 233 of the documentation (“Beneath the Surface”) for a way to implement a rug with stuff under it, though it might be more sophisticated than you need. A standard way to do this is to put the rug in the room, start the safe out of the room, and do something like this:

The rug is scenery in Inside Small House. The safe is a fixed in place openable closed container. Instead of looking under the rug: say "You discover a safe under the rug!"; move the safe to Inside Small House.

Thank you for that swift and thorough reply! I was able to successfully implement the mechanic that you suggested. I never once thought about bringing the safe in after the player finds it. I guess I am thinking a little bit too literally. :slight_smile: However, is it possible for this mechanic to apply to multiple objects? For example, I want the player to find two halves of a key to a grate, and when the grate is reached, have the player automatically put together the key and be moved to the sewers without having to implement an actual grate. This would allow me to print an action sequence, but I’d like this to execute if and only if the player is carrying both halves. Any suggestions? Would the same mechanic work here? Here is the code I have so far, obviously kind of buggy:

After the player enters the End of Alley North for the first time, and is carrying the Loop Part of Key and Tooth Part of Key:
	say "You see a grate and immediately realise the importance of this lost key. It was not a key to treasure. No, it is a key to the only thing these impoverished people want--escape from the hellhole that this city is for them. You quickly put the key together and unlock the grate. Just as you are opening the heavy grate, a band of guards come around the corner, led by the thug whose life you spared just a while ago.
	'There he is!', he yells.
	'Stop right there! You are under arrest!', roars one of the guards, as they begin to charge towards you.
	You slip into the grate without thinking and slam the grate locked just as the mob gets to the foot of the grating.
	'Damn you, thief! We'll catch you yet! I won't give up this easily! We'll just see how long you can rot in the cesspit of the sewers!', howls the chief guard menacingly.";
	Move the player to the Sewer Entrance;
	Remove the Tooth Part of Key from play.
	Remove the Loop Part of Key from play.
	The Key to Sewers is an object.
	Give the player the Key to Sewers.

Thanks in advance for the help! :slight_smile:

OK, once again you need to remember that these rules are keyed to action descriptions. So it’ll be “After going to the End of Alley North” (and you don’t want “for the first time,” as that will prevent this from happening if the player doesn’t have the key halves the first time they go to The End of Alley North; you won’t have to worry about this happening more than once, because after it happens once the two halves of the key are out of play so the rule won’t trigger again).

Similarly you can’t say “…and is” in the rule header like that. You’d need to say “…when the player carries the Loop Part of Key and the player carries the Tooth Part of Key” (these need to be separate; Inform doesn’t understand what “carries the loop part of Key and Tooth part of Key” means).

You also need to make sure that you don’t include a period until your rule ends; right now “Remove the Loop Part of Key from play” isn’t part of the rule, because the line before ended with a period.

And “Give the player to key to sewers” isn’t valid syntax. You need “Now the player carries the Key to Sewers.” See §8.11 of Writing with Inform on how this works – “now” is something you’ll need a lot.

You’ll also have to declare the existence of “The Key to Sewers” in its own sentence; you can’t create an object inside a rule. And it should be a “thing,” not an object; things are the ordinary stuff you can manipulate, where objects could be things or could be more abstract non-manipulable things (objects that are not things include rooms and directions).

So you will wind up with something like this:

The Key to Sewers is a thing. After going to the End of Alley North when the player carries the Loop Part of Key and the player carries Tooth Part of Key: say "Your text here."; Move the player to the Sewer Entrance; Remove the Tooth Part of Key from play; Remove the Loop Part of Key from play; now the player carries the Key to Sewers.

Just as general advice, you might want to try working through some of the examples in the documentation a bit more; you’ve got some parts of it right, but there are other parts where you’re sort of trying to say how things should work in English instead of putting it in the syntax Inform wants. Inform looks like English but it really demands some quite particular syntax; the best way to figure it out (at least the way that worked best for me) was to plow my way through a lot of the examples in the documentation and see what they were doing.

Wow, thanks as always for the snappy response! I greatly appreciate it. :slight_smile: Yes, I’m still fairly new to Inform 7, and it is truly taking some effort to almost forget what the rules of the English language stipulate. I must say, though, it has been an enlightening experience so far, and with the help of this forum and amazing people like you, I was able to stay focused and really begin to dig into this type of language. I’m really getting to enjoy this and will make sure to code through some more of the examples as I have found them helpful as well. Thanks again and have a great day! :slight_smile:

I would especially recommend the complete worked examples, a series of full games written by famous IF authors in Inform 7. “Bronze” is the most classic-IF-like of these.