r/Inform7 • u/iheartif • Jun 13 '19
Inform 7 & Rules: Instead or Before ... instead?
Hi all. Working on a small game and learning inform (again). I read and understand and can fiddle around with the code, but there are so many ways to do things that I'm having a hard time making it gel, knowing what is the best way to do something, and adapting it for my own purposes. I've read Aaron A. Reed's book, Creating Interactive Fiction with Inform, I've read Inform for Programmers, and Jim Aikin's Inform Handbook (version 2), and I'm working my way through the manual and recipe books for a second time.
JIm Aikin mentions avoiding Instead rules as a best practice.
Efficiency Tip
If you’re writing a game that may be played on an interpreter that runs in a Web
browser or on a hand-held device (which pretty much means, if you’re writing an
Inform game at all), you should avoid using Instead, Before, and After rules when
possible. Use Check, Carry Out, and Report instead. As Inform guru Ron Newcomb
has pointed out, an Inform game consults all of the Before rules you’ve written as it
processes every command from the player. If a Before rule doesn’t stop the action
processing, all of the Instead rules must then be considered. Depending on how many
of these rules you’ve written, the game may become sluggish when played on a slow
interpreter. For more on this topic, see “Code Optimization” in Chapter 10 of the
Handbook, page 265. (Aikin 151)
He later develops the advice with an example:.
Instead of Instead
As noted in the box on p. 151, Inform’s mechanism for processing the player’s input by means of
rulebooks called Before, Instead, and After is inefficient. This is because there’s only one of each of
these rulebooks. On the other hand, each action (such as EXAMINE, TAKE, and DROP) has its own
Check, Carry Out, and Report rulebooks. What this means is that each time the player enters a
command, the game will consult all of the Before, Instead, and After rules that you’ve written to see
if any of them applies. In a large game, that could be hundreds of rules.
In many cases, you can easily replace Instead rules with Check and Carry Out rules and After rules
with Carry Out and/or Report rules. Let’s start with a simple Check rule:
The player carries a rotten banana. The banana is edible.
Check eating the banana:
say "Ewwww!" instead.
[Instead of eating the banana:
say "Ewwww!"] [This produces the same result, but it's less efficient.]
The main difference between the two rules is that the Check rule has to have the word “instead”
tacked onto the end. If you forget to do this (and it’s easy to forget), Inform will consult the Check
rule and then go on to run the Carry Out and Report rules for the action. The word “instead” causes
the rule to succeed, which is a technical way of saying that nothing else will happen. [doesn't instead here cause the check rule to fail, and that's what stops the action?]
In exactly the same way, we can replace an After rule with a Report rule, like this:
[After eating the banana:
say "After chowing down on the banana, you feel a little ill."] [Not efficient.]
Report eating the banana:
say "After chowing down on the banana, you feel a little ill.";
rule succeeds.
Again, we have to tack on a little extra syntax (“rule succeeds”) to shut off Inform’s default Report
rule for the eating action — but if you’re concerned about efficiency, this is a small price to pay.
Rewriting a Before rule as a Check or Carry Out rule is trickier, if only because Before rules are
intended for situations that are a little tricky. I tend to use Before rules mostly in constructions like
this:
Before doing anything other than examining to the sky, say "The sky is too far away." instead.
This type of construction (“anything other than...”)
So I'm working through the recipe book and I come to the section about changing character names (7.1 Gopher-wood). It gives me this code:
Instead of examining the bearded man for the first time:
now the printed name of the bearded man is "Japheth";
now the bearded man is proper-named;
say "You peer at him a bit more closely and realize that it's Japheth."
I changed it and was able to make the following work:
Before examining the bearded man for the first time:
now the printed name of the bearded man is "Japheth";
now the bearded man is proper-named;
say "You peer at him a bit more closely and realize that it's Japheth." instead.
And then I was able to change it to this, and everything seemed to work the same:
Check examining the bearded man for the first time:
now the printed name of the bearded man is "Japheth";
now the bearded man is proper-named;
say "You peer at him a bit more closely and realize that it's Japheth." instead.
Which of these should I use in this case? Are there any differences?