[I7] Unlock keylessly with any matching key made

Is there a way that I can tell Inform 7 to unlock any door with its matching key and not have to specifically mention one key for each rule? I’m writing a game which relies heavily on puzzles and reusing keys. I don’t want to use Inform 6 since I find I7 to be easier for me to work with. When I run my game with the code, Inform gives me a Problem report, saying 'The phrase or rule definition ‘Rule for supplying a missing second noun while unlocking’ is written using the ‘colon and indentation’ syntax for its 'if’s, 'repeat’s and ‘while’s, where blocks of phrases grouped together are indented one tab step inward from the ‘if …:’ or similar phrase to which they belong. But the tabs here seem to be misaligned, and I can’t determine the structure. The first phrase going awry in the definition seems to be ‘otherwise, say “You will have to specify what to unlock it with.”’ , in case that helps.’ I didn’t use spaces in any of my code, I specifically used tabs.

Rule for supplying a missing second noun while unlocking: if the matching key of the door is carried: now the matching key of the door is the second noun; otherwise, say "You will have to specify what to unlock it with.".

Well, the specific problem is indeed the indentation. The “otherwise” line should be at the same level as the matching “if,” and I don’t think you can use the one-line comma syntax in the middle of a big block like that (though I could be wrong on this one). So it would be:

Rule for supplying a missing second noun while unlocking: if the matching key of the door is carried: now the matching key of the door is the second noun; otherwise: say "You will have to specify what to unlock it with.".

bbbut you also need to say “now the second noun is the matching key of the noun” rather than the other way around–what you’ve got will try to set the matching key of the door to the second noun, which at best will give you a run-time error and at worst will quietly set the matching key of the noun to nothing, so you get an annoying “What do you want to unlock the door with?” message and then find that the key doesn’t unlock the door anymore.

Also I think the built-in extension Locksmith by Emily Short takes care of this, if you don’t want to mess around with it yourself (but messing around with it is fun!)

I followed your instructions and I got another Problem report:

In Part SR1 - The Physical World Model, Section SR1/0 - Language in the extension Standard Rules by Graham Nelson:

Problem. In the sentence ‘Definition’ , I was expecting to read a condition, but instead found some text that I couldn’t understand - ‘matching key of the door is carried’.

I was trying to match this phrase:

if (matching key of the door is carried - a condition):

But I didn’t recognise ‘matching key of the door is carried’.

Rule for supplying a missing second noun while unlocking: if the matching key of the door is carried: now the second noun is the matching key of the noun; otherwise: say "You will have to specify what to unlock it with.".

Oh, right! Inform won’t understand “the door.” You know you mean the door you’re unlocking, and I know that, but Inform can’t figure that out. That should be “matching key of the noun.” (And in fact, the thing you’re unlocking can be a container rather than a door.)

That worked, but I wrote something else under it and that didn’t work. I’m trying to change the original ‘unlock’ command from trying to unlock with the matching key when using it alone and carrying the key.

Rule for supplying a missing second noun while unlocking: if the matching key of the noun is carried: now the second noun is the matching key of the noun; otherwise: say "What do you want to unlock?"; otherwise if the second noun matches the first noun: say "What do you want to unlock?".
I know what an otherwise if is but I’m not sure what Inform’s syntax would specify something like this as.

You want “the noun” rather than “the first noun”. And an “otherwise if” needs to come before a plain “otherwise”.

But that aside, what’s the point of the “otherwise if”?

The point of the otherwise if is to stop the unlock command by itself from trying to unlock the key with itself when it is carried by the player. I want the command to not relate to the key when it runs alone and say the regular error message instead.

The solution you gave didn’t work on my end. Here’s the report.

'Problem. You wrote ‘if the second noun matches the noun’ , which I tried to match against several possible phrase definitions. None of them worked.

  1. (second noun - value) matches (noun - description of values)
  2. (second noun - snippet) matches (noun - topic)

I recognised:
second noun = a non-temporary variable, holding an object
noun = a non-temporary variable, holding an object’

Rule for supplying a missing second noun while unlocking: if the matching key of the noun is carried: now the second noun is the matching key of the noun; otherwise if the second noun matches the noun: say "What do you want to unlock?"; otherwise: say "What do you want to unlock?".

I mean, your “otherwise if” does exactly the same thing as the plain “otherwise” clause, so it doesn’t matter whether the condition is true or not. (Also, if the “Rule for supplying a missing second noun” is running in the first place, it means there was no second noun in the player’s command, so it doesn’t make any sense to check what it is.)

And Inform is…weird…about the word “matches”. If you look at the example “The Cow Exonerated” the author goes to great effort to implement matches (firestarters) without ever using the word. I’d rephrase it as “if the second noun is the matching key of the noun”.

I think “If the second noun unlocks the noun” also works here.

If you want to get into the nitty-gritty, this code from the Standard Rules:

Lock-fitting relates one thing (called the matching key) to various things. The verb to unlock means the lock-fitting relation.

basically means that “The bronze key unlocks the bronze door” and “The bronze key is the matching key of the bronze door” are two ways of ascribing the same underlying “lock-fitting” relation. If you want more on what’s going on here, look at §13.5 and §13.9 of the documentation on new relations and new relation verbs.

If this helps, this is the output I’m trying to change.

[code]>unlock
(the blue key with the blue key)
That doesn’t seem to be something you can unlock.

[/code]

Oh, you want to capture the case in which the noun and the second noun are the same thing! That would be “if the noun is the second noun.” Draconis and I both thought you meant “matches” in the sense of “is the matching key of.”

The code below seems to work for right now–I’ll check it more and see if I can explain it later. You don’t actually have to test for whether the noun is the second noun–if a rule for supplying a missing second noun is running, then the second noun hasn’t been defined yet, so we don’t need to check it.

The mechanism you were just encountering is one where Inform sometimes automatically supplies nouns for an action and it can be damnably hard to tweak, but in this case ensuring that there is a rule for supplying a noun as well as a rule for supplying a second noun seems to be helping.

[code]The office is a room. The blue chest is a closed, locked container in the office.

The player carries a blue key. The blue key unlocks the blue chest.

The closet is west of the office.

Does the player mean unlocking when the noun is the second noun: it is very unlikely.

Understand “unlock” as unlocking it with.

Understand “unlock [something]” as unlocking it with.

Rule for supplying a missing noun while unlocking:
if the number of locked visible things is one:
now the noun is a random locked visible thing.

Rule for supplying a missing second noun while unlocking:
if the matching key of the noun is carried:
now the second noun is the matching key of the noun;
otherwise:
say “You don’t have a key that unlocks that.”[/code]

(EDITED to include the correct code above.)

Nothing I have tried so far seems to work.

[code]>unlock
What do you want to unlock?

purloin blue key
[Purloined.]

unlock
(the blue key with the blue key)
That doesn’t seem to be something you can unlock.

[/code]

How do I implement Locksmith? I know how to include it in my game, but what would I need for my purpose?

Oh, I’m sorry, I completely failed to post the correct code above! I edited it in, but here’s what I meant to post:

[code]The office is a room. The blue chest is a closed, locked container in the office.

The player carries a blue key. The blue key unlocks the blue chest.

The closet is west of the office.

Does the player mean unlocking when the noun is the second noun: it is very unlikely.

Understand “unlock” as unlocking it with.

Understand “unlock [something]” as unlocking it with.

Rule for supplying a missing noun while unlocking:
if the number of locked visible things is one:
now the noun is a random locked visible thing.

Rule for supplying a missing second noun while unlocking:
if the matching key of the noun is carried:
now the second noun is the matching key of the noun;
otherwise:
say “You don’t have a key that unlocks that.”[/code]

Locksmith will take care of these cases automatically if you include it:

[code]Include Locksmith by Emily Short.

The office is a room. The blue chest is a closed, locked container in the office.

The player carries a blue key. The blue key unlocks the blue chest.

The closet is west of the office.[/code]

I just included Locksmith and that section works great now! Thanks guys!