Can't seem to get IF statements right

I’ve been trying now for a few days to crack the IF statement. Every example I dig up say the same, but I keep getting the same error. The more I read the more confused I get. I’ve been programming since 1983 and make my living from it these days, so the general idea of algorithmic logic not the issue, but rather the syntax in Inform 7. I can’t seem to grasp it.

Take this example from Beginner’s Guide to Interactive Fiction with Inform 7:
if score is 30
begin;
say “You emerge from the ruined temple, having finally taken photos of all the wonderful artifacts therein.”;
end the game in victory;
end if.

I get these error messages:
Problem. The text ‘if score is 30 begin’ is followed by a semicolon ‘;’, which only makes sense to me inside a rule or phrase (where there’s a heading, then a colon, then a list of instructions divided by semicolons). Perhaps you want a full stop ‘.’ instead?

Problem. The text ‘say “You emerge from the ruined temple, […] l the wonderful artifacts therein.”’ is followed by a semicolon ‘;’, which only makes sense to me inside a rule or phrase (where there’s a heading, then a colon, then a list of instructions divided by semicolons). Perhaps you want a full stop ‘.’ instead?

Problem. The text ‘end the game in victory’ is followed by a semicolon ‘;’, which only makes sense to me inside a rule or phrase (where there’s a heading, then a colon, then a list of instructions divided by semicolons). Perhaps you want a full stop ‘.’ instead?

From the error messages I would think adding a colon to the first sentence (if the score is 30:) would fix the problem.

But then I get this message:
Problem. You wrote ‘end if’ : but this is an ‘end’ with no matching ‘begin’, which should not happen: every phrase like ‘if … begin;’ should eventually be followed by its bookend ‘end if’. It makes no sense to have an ‘end …’ on its own.
Perhaps the problem is actually that you opened several such begin… end ‘blocks’ and accidentally closed them once too many? This is very easily done.

This makes me think the indentation could be wrong, or there could be another broken IF statement somewhere in my source text. That is not the case. Can’t speak for the indentation, but it looks right as it’s from the example I copied.

Can you spot what’s wrong? I guess it pertains to making a rule of sorts to deal with this? Any help would be much appreciated.

I had similar issues with the if / else syntax when I started learning Inform 7 (and I also come from a programming background). I personally prefer to use conditional blocks as follows:

if x is 1:
   say "x is 1.";
otherwise if ( x is 2 ) and ( z is not 143 ):  
   say "x is 2";
   increment x;
   let y be x + 10;
   say "y is [y].";
otherwise:
   say "x is not 1 or 2";

Just make sure your conditions end with a colon and that your blocks are indented. Easy. Much cleaner looking code than using ‘begin’.

Do you have the if statement in a rule? It can’t exist without context because otherwise Inform doesn’t know when to perform that check. You’ll have to put it in a rule like “Every turn: if score is…”

The next problem is that “end the game in victory” has changed to “end the story finally” in the latest versions.

Thank you both! I can’t wait to try your suggestions.

I can see my indentations got lost when I copy-pasted.

Your replies were exactly what I needed: put the if statement in a rule and do away with begin/end if (for now).

If you post your code inside code tags, it’ll preserve your indentations:

Every turn: if the custodian is in a room (called the old space): showme the location of the old space; showme the location of the custodian; showme list of doors that vent on the old space; let xanadu be a random door that vents on the old space; showme xanadu; try the custodian entering xanadu.

Code tags look like this:

[code]

Your code here. [/code]

and you can make them automatically by clicking on the “Code” formatting button above the box where you type your code.