I’m really new to writing IF with Inform 7. I have started a project and I need a bit of help with one specific and easy thing:
I have an openable container containing several other openable containers. I want to implement an instead rule for closing the big container:
for each item in the big container:
if the item is open, say "You need to close [the item] first, [the list of items in the container] might fall out.[line break]"
I want to print these messages instead of closing the big container until everything inside is closed.
Here’s a bit of source code you can use:
The schoolbag is an openable wearable closed container. Some schoolbooks, some exercise books, a pencil case, a ruler, a lunchbag, some money and a drinking bottle are in the schoolbag.
The pencil case is an openable closed container. Some crayons, a pen, some ink cartridges and a timetable are in the pencil case.
The lunchbag is an openable closed container. A sandwich is in the lunchbag.
The drinking bottle is an openable closed container in the schoolbag.
A quantity of water is in the drinking bottle.
Maybe add some tweaks later, just so you know I might get back with another question.
Thanks for your help!
[code]Definition: a container is non-empty if it contains something.
Instead of closing the schoolbag when the schoolbag contains a non-empty open container (called the hazard): say “[The list of things in the hazard] would spill out!”.[/code]
Ah ok, that’s one way to do it - thanks!
However it doesn’t quite achieve what I needed because this only prints a list of the things in ONE of the containers, no matter how many of them are open. I nevertheless found out a way to achieve what I needed:
Instead of closing the schoolbag when the schoolbag contains a non-empty open container (called the hazard):
repeat with item running through open non-empty containers in the schoolbag:
Say "You need to close [the item] first or [the list of things in the item] will spill out."
And when I’m already at it, I can add different sentences for different containers (“spill out” isn’t exactly what you would use for the contents of a pencil case…), so something like this:
Instead of closing the schoolbag when the schoolbag contains a non-empty open container (called the hazard):
repeat with item running through open non-empty containers in the schoolbag:
if the item is the pencil case:
say "Unless you don't want [a list of things in the item] scattered inside your schoolbag, you should close the pencil case first.";
try closing the pencil case;
else if the item is the lunchbag:
say "Your tasty [list of things in the item] would fall out and be ruined afterwards. You also don't want to soil the inside of the schoolbag.";
try closing the lunchbag;
else if the item is the drinking bottle:
say "You almost forgot to close the bottle, risking a massive spillage of half a litre in your schoolbag!";
try closing the bottle;
otherwise:
Say "You need to close [the item] first or [the list of things in the item] will fall out.";
try closing the item;
continue the action;
try closing the schoolbag.
Of course if more than one item is open in the schoolbag, you’ll get a lengthy message but it saves the trouble of closing every single container.
Did I do well?
That also works! If you want to shorten up the message a bit, you can also do
say "You need to close [the list of non-empty open containers in the schoolbag] first, or your bag will be a mess.
This will give “the lunchbox” for only one, “the lunchbox and the pencil case” for two, “the lunchbox, the bottle and the pencil case” for three, and so on.