This is my first post here. I am an experienced programmer, groping towards better understanding of Inform 7 which (frankly) is not easy.
I am trying to run some repeat loops inside a custom rule.
The compiler is complaining that repeat loops must be left-aligned, but if I do that, then the repeat loops will no longer be subordinate to the Rule (or maybe I am mistaken about that). I have no trouble nesting a repeat loop inside a built-in rule (such as After Looking:)
Unfortunately, the documentation only seems to include brief or one line custom rules.
Can you post some sample code? You’re right that the statements inside a repeat rule should be indented under the repeat statement, so I’d guess there’s an earlier spacing or punctuation issue messing things up. But it’s hard to get more specific without seeing what’s going wrong, so posting a cut-down example demonstrating the issue would be helpful (NB you can use the preformatted text tool - the little </> icon, which might be under the gear - to preserve spacing).
(I’m also not sure what you mean by “custom rule” but I’m guessing an example would make that clear too!)
If you’re an expert programmer finding not easy gaining understanding of Inform 7, why you don’t give a try to TADS3 ?
Because yours is the first post in this forum, I presume that you are new to IF coding, which is a bit different from general, or even gaming, programming, so perhaps learning how to code IFs with a more traditional IF language isn’t a bad idea.
My immediate thought is you have a previous line ending with a period, which is an explicit “end of rule” marker, and thus Inform expects the line after the period to be left-aligned and start a new rule. But that’s just a guess.
"Corporate Dresscode" by Medea
The story description is "A small example game with three rooms, a closet, and an HR lady who cares about your attire."
[1. Define the Rooms]
The Corridor is a room. "A long, nondescript corridor connecting the rooms."
The Closet is a room. "A walk-in closet filled with various garments. [if the player is not wearing the hat and not wearing the boots and not wearing the gloves and not wearing the coat]There's a hat, a pair of boots, a pair of gloves, and a coat here.[otherwise if the player is not wearing the hat]There's a hat here.[otherwise if the player is not wearing the boots]There's a pair of boots here.[otherwise if the player is not wearing the gloves]There's a pair of gloves here.[otherwise if the player is not wearing the coat]There's a coat here.[otherwise]There are no garments here.[end if]"
The Office is a room. "A bland office space. The HR lady sits at her desk, eyeing you suspiciously."
[2. Connect the Rooms]
The Corridor is north of the Closet and south of the Office.
[3. Define the Garments]
The hat is in the Closet. It is wearable. The description is "a stylish fedora".
The boots are in the Closet. They are wearable. The description is "a sturdy pair of leather boots".
The gloves are in the Closet. They are wearable. The description is "a pair of black leather gloves".
The coat is in the Closet. It is wearable. The description is "a long trench coat".
[4. Override the Indefinite Articles]
The indefinite article of the boots is "a pair of".
The indefinite article of the gloves is "a pair of".
[5. Define the HR Lady]
The HR lady is a woman in the Office. The description is "She looks stern and professional, with a clipboard in hand."
[6. Define Required and Forbidden Garments]
The required-garments are a list of things that varies. The required-garments is { the hat, the boots }.
The forbidden-garments are a list of things that varies. The forbidden-garments is { the coat }.
[7. Customize the Appearance of Worn Items]
Instead of examining the boots when the boots are not worn:
say "A sturdy pair of leather boots.";
Instead of examining the boots when the boots are worn:
say "The sturdy pair of leather boots you're wearing.";
Instead of examining the gloves when the gloves are not worn:
say "A pair of black leather gloves.";
Instead of examining the gloves when the gloves are worn:
say "The pair of black leather gloves you're wearing.";
Instead of examining the coat when the coat is not worn:
say "A long trench coat.";
Instead of examining the coat when the coat is worn:
say "The long trench coat you're wearing.";
[8. Generic Rule for Describing Worn Items]
After looking:
let worn-list be a list of text;
repeat with item running through wearable things worn by the player:
add "[the description of the item]" to worn-list;
if the number of entries in worn-list is 0:
say "You are naked.";
otherwise:
say "You are wearing [worn-list].";
[9. Define a Phrase to Check Attire]
To decide whether attire is acceptable:
let missing-list be a list of text;
let forbidden-list be a list of text;
[Check for missing required garments]
repeat with item running through the required-garments:
if the player is not wearing the item:
add "[the description of the item]" to missing-list;
[Check for forbidden garments]
repeat with item running through the forbidden-garments:
if the player is wearing the item:
add "[the description of the item]" to forbidden-list;
[If the player is improperly dressed, nag and eject them]
if the number of entries in missing-list is greater than 0 or the number of entries in forbidden-list is greater than 0:
say "The HR lady clears her throat and gives you a disapproving look. 'Excuse me,' she says, 'but your attire is not up to company standards.'";
if the number of entries in missing-list is greater than 0:
say "'You are missing [missing-list].'";
if the number of entries in forbidden-list is greater than 0:
say "'Additionally, [forbidden-list] [if the number of entries in forbidden-list is 1]is[otherwise]are[end if] strictly forbidden.'";
say "'Please leave until you are properly attired.'";
move the player to the Corridor;
decide no;
otherwise:
decide yes.
[10. HR Lady's Behavior]
Every turn when the player is in the Office:
if attire is acceptable:
rule succeeds;
It’s annoying, and honestly I’m tempted to make a feature request for it in the next version of Inform. Python also uses line breaks and tabs syntactically, and still allows blank lines within blocks, after all!
I have made a couple of attempts to write code for IF (even making a crappy attempt in BBC BASIC back in the mid-1980s). And I first tried Inform 7 about 10 years ago, but I rarely had time to dig into it. I don’t think this discussion board existed back then either, so it was hard to move beyond the most simple “toy” examples, like those in the documentation.
I might try TADS3 at some point, but I am utterly fascinated by Inform 7 because of its natural-ish language syntax. Years ago, I enjoyed working with languages like Hypertalk, Lingo and AppleScript, especially when the code could be made to look something like prose. Inform 7 is far ahead of those, of course.
Of course, everything seems obvious and logical when I read the documentation, but actually building something with a little complexity is more tricky. I enjoy the challenge, and Inform is so unlike the imperative code I have worked with. (It’s also very different to declarative code like markup and CSS).
Haha. So true. It reminds me of the Monty Python sketch in the bed shop where if you say the word “mattress” the salesman puts a bucket on his head, and wont remove it unless everyone gets in the fish tank and sings “Jerusalem”.
One bit of unsolicited advice on your code – I noticed that you use a couple “instead of examining the X” rules to contextually shift the output for X TRENCH COAT or whatever. This isn’t the simplest way to do this kind of thing in Inform, since you can just do stuff like:
The description of the trench coat is “A long trench coat[if worn] currently covering your body[end if].”
But more importantly, using an instead rule to jump out of the regular action-processing rules to run bespoke code can make life much harder later on. Like, say you decide you want to output some text after the player examines a forbidden garment – if you’re using the standard examine rulebook, that’s as easy as writing an after examining a forbidden garment rule. But with the instead approach, you’re in for a lot of manual copy-and-pasting to get the same effect. Also, checks like “if the player has ever examined the X” won’t behave intuitively because if the relevant rule book gets short-circuited with an instead rule, it will never register as having successfully completed.
Apologies for the hectoring here, and maybe you were just doing this to get simple dummy code, but having used a bunch of instead rules in my first Inform game and then running into all the issues above and then some, I wanted to flag the potential dangers!
No apologies needed. I was aware that that section of code was kinda dorky and unscalable, but had other fish to fry.
Your finger-wagging is very insightful. I have followed your advice, and taken it further, by putting the worn/unworn descriptions into separate table entries for each garment. I think I am getting the hang of this. Thanks to all of you!