Inform 7 help

“tes” by foo

Hiya! I’m trying to get started with Inform 7 and need a bit of assistance. I’m a programmer at heart, but I find inform to be frustrating and fascinating at the same time. It’s just close enough to normal speech to make me feel comfortable, but just far enough away to be frustrating. take this piece for example:

[code]the scary classroom is a room.

the broken box is in the classroom. the box is a container.

the spooky pencil is in the box.

Instead of taking the box, say “I’m not touching that.”

the scary hallway is north of the classroom. the description is “its a very creepy, dark hallway.”

every turn:
If the player carries a spooky pencil:
instead of going to the hallway:
now the player is in the classroom;
say “I think I should leave the pencil here.”[/code]

everything works except leaving the room. you can pick up the pencil, not the box, you can drop the pencil, but you can never leave the room. whether you have picked the pencil up or not, or dropped it, you can never leave, it just says “I think I should leave the pencil here.” you could start the game, touch nothing, try to go north, and you just can’t. And as a programmer, I have no clue why. I’ll probably post a few of these as I go along, so if anyone has the time to help, it would be greatly appreciated.

(Last time I tried suggesting something in one of these threads, it was pointed out to me that I was being decidedly Not Helpful in teaching good Inform coding. Nevertheless, hope springs eternal.)

You don’t need to implement an “every turn” rule to do what you want to do here - it slows down the game and isn’t necessary. You don’t need to specify that the player is now in the classroom, since they’re already there when you start. What you’re trying to do from the look of it is just to restrict the player’s ability to go north while carrying the spooky pencil, which is easily handled just by tweaking your instead code a bit.

Instead of going north from the classroom: if the player is holding a spooky pencil: say "I think I should leave the pencil here."; otherwise: continue the action.

So you had the basic idea. Inform makes a lot of assumptions for you, which make more sense with practice.

(Teaspoon posted while I was writing this; I’ll post it anyway in case it’s helpful.)

You can begin a rule with “every turn” or with “instead of”* but it won’t work to combine both hooks in the same rule. Those are two different kinds of rules.

You don’t need to test whether the player is carrying the pencil every turn. You only need to test it when the player tries to go to the hallway. Try this instead:

instead of going to the scary hallway when the player carries a spooky pencil: say "I think I should leave the pencil here."

Edited to clarify:
*You can use the word “instead” inside of another kind of rule, but it won’t be “instead of [action]” like you’re doing here. E.g.

Before taking something: say "I don't want to take anything." instead.

To supplement: what you’re doing now is the equivalent of defining a function inside another function, which Inform doesn’t like. “Every turn” causes your rule to go off every turn, after the player’s action is complete; “instead of…” causes it to go off when an action is triggered, pre-empting the normal processing rules.

Too many cooks spoil the broth, they say, but I was typing up a great response while 2 (now 3, good grief) others posted, so here’s MY version! It offers further clarification.

Every turn rules are for code that you want to run on every single turn of the game. This is not what you want here. What you want is to check if the player has the pencil ONLY at a moment when they try going north from the classroom to the hall. Otherwise you’ve got Inform thinking about the classroom/hall/pencil situation on every turn, even when they player is far past this point in the game.

So we get rid of the every turn approach and isolate your check to that going north moment:

[code]the scary classroom is a room.

the broken box is in the classroom. the box is a container.

the spooky pencil is in the box.

Instead of taking the box, say “I’m not touching that.”

the scary hallway is north of the classroom. the description is “its a very creepy, dark hallway.”

Check going north in the classroom:
if the player carries the spooky pencil:
instead say “I think I should leave the pencil here.”;

Test me with “get box / north / south / get pencil / north / drop pencil / north”.[/code]

I used a ‘check’ rule instead of an instead rule. Check rules are good for, uh, checking, or gatekeeping actions that you generally want to allow. We expect the player should be able to go north between these rooms, but we want to CHECK for a particular circumstance before we allow them to do this.

Instead rules are more drastic in the sense that if you use one, you’re totally rerouting Inform away from the original action. Teaspoon’s example shows this - it needs to manually ‘continue the action’ to prevent the attempt to go north fail completely.

bg’s example refines Teaspoon’s by isolating the circumstances that would block you going north to one clause. Either the clause fires because the circumstances match exactly, or the clause doesn’t even get a look in.

With my way, you can add other ‘check’ clauses later if you want. But if you’re never going to add any more going-north-checks to this particular moment in the game, the ‘instead’ way will be fine, too. Deciding what to use is gained from experience and familiarity with Inform’s logic and what you need your own game to do.

-Wade

I find this quite interesting; it seems that kittiewolfie’s rule

every turn: 
	If the player carries a spooky pencil:
		instead of going to the hallway:
			now the player is in the classroom;
			say "I think I should leave the pencil here."

is functionally equivalent to

Every turn: 
	if the player carries a spooky pencil:
                  do nothing.	
	
Instead of going to the hallway:
	now the player is in the classroom;
	say "I think I should leave the pencil here."

(In both cases the Index shows that two distinct rules are created.) I’m surprised that the compiler accepts the first version. Anyone know why? (Or maybe there’s no particular reason.)

Indentation at the start of a declaration is not forbidden. However, an “if” statement with no body should cause an error report; that looks like a compiler bug.

Zarf, Emily would seem to agree and would like to see it filed on Mantis to be taken care of, if that’s all right.

If it’s already there, do forgive me. :slight_smile: I only know what I can see here in these threads.

Thanks guys, all of these are wonderful, and make lots of sense. I guess i’m just used to programming in languages where the engine doesn’t think for me, so inform is intriguing. I’ll probably post other issues in this thread as I learn, if any of you decide to drop back in. for now though, thank you all! I think I like the “check” option best, it seems the most flexible, most concise and least interruptive to the game flow.