Instead of X, say "Y"

Hi. I made a short game in Inform 7 as a learning exercise. It surprised me that so much of the code–40% or more–consists of individual responses to the different combinations of verbs and objects. For example:

Instead of burning the wall, say “That’s not possible.”
Instead of cutting the wall, say “You have no tool to cut with.”

And so on for (almost) every common verb, for every object in the game.

Is this typical, or is there a better way to handle this? I don’t have a programming background to speak of.

A better way is to make such responses generic. Basically, you make an instead statement apply to the entire kind (category) of things or action you want the message to apply to.

Instead of burning something, say "[The noun] can't be burned." Instead of cutting something, say "You have no tool with which to cut."
Clarification: Those two statements are enough to cover every object of the “thing” kind in the entire game. So as long as you’re burning or cutting something and want a general response, those two lines would be enough.

(It’s probably better to use the Custom Library Messages extension for generic “You can’t take that” type responses, though, and the next release of Inform 7 reputedly provides better ways of handling default messages.)

Instead rules are powerful, as are all rules, but you don’t need many of them to create far-reaching effects. Rules let you create a behavior and then override it when you need to. As a general idea, if you’re printing the same statement in two separate Instead rules, you’re probably using one Instead rule too many.

For instance, this:

Instead of taking the tea cup: say "You don't want it." Instead of taking the sugar lumps: say "You don't want them." Instead of taking the spoon: say "You don't want it." Instead of taking the pot: say "You don't want it." Instead of taking the sugar tongs: say "You don't want them."
…could be condensed into this:

[code]Include Plurality by Emily Short.

Instead of taking something (called the item), say “You don’t want [it-them of the item].”[/code]

Since all the items (the tea cup, the sugar lumps, the spoon, etc) are instances of the thing kind, this rule covers all of them as well as every other thing in the game. By the same token, you can generalize actions:

[code]Cutting is destructive behavior.
Attacking is destructive behavior.
Burning is destructive behavior.

Instead of destructive behavior, say “No, that would damage [the noun].”
Instead of destructive behavior when the noun is a person, say “How barbaric!”[/code]

Thank you! That’s very helpful!

With regard to your example here:

How does Inform 7 decide which of the two lines to follow, if both apply? Does it always follow the more specific condition (“destructive behavior when the noun is a person”) over the more general one (“destructive behavior” in the first line)? For some reason I assumed that using rules that way would result on both responses being printed if both conditions were true, but I am glad to learn that is not the case.

Yes, the general rule is that the most-specific rule applies. See here for more detail:


The fact that these are “Instead” rules is important here. In most other rulebooks Inform will run through all the rules, from most specific to most general, in order, unless you tell it to stop (with a phrase like “stop the action” or “rule succeeds” or “rule fails”). But whenever an Instead rule runs, it stops the action unless you specifically tell it not to (with “continue the action”). (The same is true for “After” rules.) So in this example, the first Instead rule will run and then stop the action rules.

Some more under-the-hood explanation of that is here (especially the part where it discusses the default outcomes of the Instead and After rulebooks).

Ok, so you are saying the most specific rule applies. If it’s an “instead” rule or an “after” rule, the action will stop after running the most specific rule, but if it’s another kind of rule, the action may continue to more general rules. That helps a lot. Thanks so much!

The library also has built-in responses to these actions that in most cases suffice. Just overload it (write a more specific rule) for the cases in which you want it to succeed.

Yes, that’s exactly the case, and that’s the key to writing complex behavior without having to repeat yourself. As you seem to have intuited already, it’s sorted by specificity in a given rulebook. Now, one can occasionally be surprised at what Inform 7 considers the more specific rule. I’ve rarely noticed this and it’s never presented a problem in my code, but should the issue arise I think you can impose your own precedence by manually sorting the rules. For the vast majority of cases though, that shouldn’t be necessary.

There may be some changes in the built-in responses area with the release of the next version, which is tentatively slated to happen during the upcoming weeks. Exciting times ahead.

Thank you all for your help. I’m looking forward to seeing how it works in the new version as well.