Why aren't "if" and "otherwise" doing what I expected in Inform 7?

0 votes
165 views
asked Mar 29 in Authoring by bg (692 points)
edited Mar 29 by bg

I have a rule with "if" and "otherwise" (or "else"), and it's not doing what I expected. How can I troubleshoot this?

commented Mar 29 by bg (692 points)
edited Mar 29 by bg
I thought it would be useful to have general troubleshooting tips for this kind of problem. I'm a relative newbie to Inform, though, so feel free to make suggestions or come up with a better answer than mine.

1 Answer

0 votes
answered Mar 29 by bg (692 points)
edited Mar 29 by bg

If the code doesn't compile:

Ideally the error message you get when you try to compile will point you in the right direction, but that's not always enough to identify the problem. Here are some common problems to check for:

(1) Make sure the punctuation at the end of each line is correct.

In the following code, there's no semicolon after the first "Say" phrase, so when Inform gets to "Otherwise", it doesn't know that "Otherwise" is supposed to be a continuation of the same rule.

Instead of taking the nail when the nail is in the piece of wood:
    If the player does not carry the hammer:
        Say "You'll need a hammer to pull out the nail." [<---semicolon is missing]
    Otherwise:
        Say "Using the hammer, you pull out the nail and take it with you.";
        Now the player carries the nail.

(2) Check any indentation. Are corresponding "if" and "otherwise" lines indented to the same level? Are subordinate blocks of code indented exactly one level farther in than the lines that should trigger them?


If the code compiles, but doesn't do what you want:

(1) Are there possibilities that are not accounted for? In a rule where you always want something to happen (even if it's just a reject message), make sure to give instructions for what to do when an if condition is not true. See the example below.

(2) Add "Say" phrases to your rule at each step so that when you run the game, you'll be able to figure out which lines are executing and which are not.

Let's say this is your code:

Instead of taking the nail:
    If the nail is in the piece of wood:
        If the player does not carry the hammer:
            Say "You'll need a hammer to pull out the nail.";
        Otherwise:
            Say "Using the hammer, you pull out the nail and take it with you.";
            Now the player carries the nail.

You can add different "Say" phrases to the sections of code that don't already have one:

Instead of taking the nail:
    Say "1";                        
    If the nail is in the piece of wood:
        Say "2";        
        If the player does not carry the hammer:
            Say "You'll need a hammer to pull out the nail.";
        Otherwise:
            Say "Using the hammer, you pull out the nail and take it with you.";
            Now the player carries the nail.

If you run the game and try to take the nail, and never even see the "1", you'll know that the rule didn't execute at all, and something elsewhere in the code must be preventing it.

If you run the game, try to take the nail, and see "1" and nothing else, you'll know that the Say "1" line executed, but Say "2" was never triggered. This tells you that there's a possibility that was not accounted for. What should happen if the player tries to take the nail, but the nail is not in the piece of wood? Inform doesn't know, so it does nothing. An "Otherwise" could be added to cover this scenario. Or you could move the condition about the wood to the first line of the rule so that the rule will only apply when the nail is in the piece of wood. So the first line of the rule would become

Instead of taking the nail when the nail is in the piece of wood:

If you do this, taking the nail when it's not in the piece of wood should behave according to the regular rules for taking, or according to other rules you may have written.

Sometimes when you add "Say" phrases to figure out which lines are executing, you may find that the only thing off is the indentation, and fixing that will make the code do what you want.

This site is now closed.
As of 1st November 2015, this site is a read-only archive. For more information see the intfiction forum post

Welcome to IF Answers, a site for questions and answers about Interactive Fiction.

Technical questions about interactive fiction development tools such as Inform, Twine, Quest, QuestKit, Squiffy, Adrift, TADS etc. are all on-topic here.

Non-technical questions about interactive fiction are also on-topic. These questions could be about general IF design, specific games, entering the IF Comp etc.
...