Touch some thing with another thing and change its state

I want to touch an unlit torch with a lit torch, and change the former to lit. I am using code from “Hymenaeus” to establish when a thing is lit or unlit (inform7.com/learn/man/RB_3_7.html)

Being an Inform7 newbie, I’m completely at a loss for how to proceed.

Please advise.

Inform doesn’t have a built-in action for touching one thing with another thing, so you’ll have to define one. The stuff about how to do it starts in §12.7 of the definition. This is a start:

Touching it with is an action applying to two things. Understand "touch [something] with [something]" as touching it with.

The first line tells Inform that there is an action, touching it with, that takes two objects. The “it” tells Inform where the first noun goes in the internal sentence description–so you can refer to the action internally as “touching something with something” or something like that. The second “Understand” line tells Inform how to understand commands the player types–so it knows that the command “TOUCH GLASS WITH NEEDLE” is to be understood as the action of touching the glass with the needle.

Now the action is defined–but it doesn’t do anything, because we haven’t written any rules for it. So we can first cover the case you want:

Carry out touching an unlit torch with a lit torch: now the noun is lit.

“The noun” here means the first thing in the action description–that is, the particular unlit torch the player referred to.

We also want to print something in response to this action, which we can do with a report rule:

Report touching an unlit torch with a lit torch: say "You light [the noun] with [the second noun]."

“The second noun” is the second thing in the action–in this case, the specific lit torch the player used.

We should also prevent the player from using a lit torch they don’t have to light something, which we can do with a Check rule:

Check touching it with: if the player does not hold the second noun: say "You're not holding [the second noun]." instead.

Check rules run before carry out and report rules (see §12.9). The “instead” tells Inform to stop processing the action and not to go to the Carry Out and Report rules.

(Actually, that is not the most player-friendly rule. The polite thing to do is to automatically have the player try to take the second noun, like this:

Check lighting it with when the player does not hold the second noun: say "(first trying to take [the second noun])[command clarification break]"; try silently taking the second noun: ["try silently" means have the player try to take the second noun, don't say anything if it worked, if it fails it will print the message saying why it failed, for instance "That's hardly portable" if the second noun was fixed in place] if the player does not hold the second noun: [the taking action failed] stop the action. [like "instead," this cuts off action processing without going to Carry Out or Report rules]

But I figure you might want to work on the more straightforward way before you embark on this.)

However! There will be all sorts of other things that the player might try touching with things, and we haven’t written any rules to cover those. So if they type “TOUCH ME WITH TORCH” they’ll just get a blank line. We can cover that with a blanket Check rule that takes care of every other case:

Check touching it with when the noun is not an unlit torch or the second noun is not a lit torch: say "There's no point in touching [the noun] with [the second noun]." instead.

although this is a bit crude–in practice you’d want some more helpful messages for more special cases.

…and also, you should probably do something to allow for commands like “light the unlit torch with the lit torch.” And take care of what happens when a player types “light the unlit torch” (as things go, this gets understood as the action of burning that torch, which by default gets the response “This dangerous act would achieve little”–which is not what you want!) But that’s something to think about after you’ve got this working.

[I moved the post to the Inform 7 development forum.]

Thank you for such an in-depth answer. Very helpful.

Sorry for the delay in responding. I apparently did not get an email saying that there had been a response.

Sorry–that might have been because I moved the post. Glad it was helpful!

Matt,

I got the first part to work great. I did change “Touching with” to “Lighting with.” But, of course, that’s insignificant.

Where I got stuck was the reporting code. I coded:

Report lighting an unlit torch with a lit torch:
say “You lit [the noun] with [the second noun]. Now you can see some things in the chamber that were not visible before. Maybe you should look at the room again.”

That simply returns a >

I know the code was successful, because if I type “Light the second torch with the wooden torch” I get:

You can see a burning second torch here.

Previously, second torch was unlit, or “extinguished”

What’s wrong with my “report” code?

Ooh, this is tricky and is probably my fault. I guess I didn’t check it properly. But if you write this:

Report lighting an unlit torch with a lit torch:

then, by the time the action reaches the Report stage, the first torch will have been changed to lit! So the conditions on the rule aren’t met.

One way to handle this might be to combine the carry out and report effects into a single rule, which might work as an After rule. (Sometimes this could be done with Instead, but Instead runs before Check, so it would bypass your check rules.) Like this;

After lighting an unlit torch with a lit torch: say "You light [the noun] with [the second noun]. Now you can see some things in the chamber you couldn't see before. Maybe you should look at the room again."; now the noun is lit.

This isn’t the most Informy way to do it–it can be nice to have these rules broken up in complicated cases–but I can’t think of a better way to have a check in the Report stage for whether the noun was unlit at the beginning of the turn.

I really appreciate your help with this. I am learning a great deal.

However, I’m not getting it to work.

I took out the report code, and added:

After lighting an unlit torch with a lit torch:
say “You light [the noun] with [the second noun]. Now you can see some things in the chamber you couldn’t see before. Maybe you should look at the chamber again.”;
now the noun is lit.

Then I ran it again. Here’s the result:

You can see an extinguished second torch here.

light second torch with wooden torch

Once again, the torch is no longer unlit at the time the action finishes. If you have a check rule that prevents lighting a lit torch again, or lighting with an unlit torch, you could just “report lighting a torch with a torch”. Nothing will make it to that stage unless it’s lighting an unlit torch with a lit one.

Sorry, you’ve completely lost me. What’s a check rule?

And how does it apply? I’m taking a lit torch and lighting an unlit torch. All I want is to add some explanatory text once the second torch is lit.

You have to take out the report code and the carry out code for this to work. In this way of doing it, the After rule is taking care of both those functions–it’s setting the torch to lit, like the carry out rule did, and it’s printing a message, like the report rule did. If you leave the carry out rule in, then the torch gets set to lit during the carry out phase, which means that by the time the After rule tries to run it doesn’t match “lighting and unlit torch with a lit torch.”

Draconis is probably right that the way to do this is to use a Check rule. Look at §12.2 of the manual–that shows which order all the rules run in. (“Check” rules just mean rules like “Check lighting a lit torch with a torch”–there’s already one of those in the code I gave.) So the option Draconis is talking about is writing a rule like this:

Check lighting a lit torch with a torch: say "[The noun] is already lit." instead.

Then you can change the original Report rule to:

Report lighting a torch with a lit torch: say "Your text here."

The check rule will have stopped every case where the first torch started out lit (that’s what the word “instead” does there). So by the time you reach the Report rule, it’s guaranteed that the first torch was unlit at the beginning of the turn.

OK. I finally got it working, although I must admit, it really was a matter not so much of understanding as of hacking at this point.

This is what works:
Lighting it with is an action applying to two things.
Understand “light [something] with [something]” as lighting it with.

Check lighting it with:
if the player does not hold the second noun:
say “You’re not holding [the second noun].” instead.

Check lighting it with:
if noun is lit:
say “That torch is already lit.” instead.

Report lighting an unlit torch with a lit torch:
now the noun is lit;
say “You light [the noun] with [the second noun]. Now you can see some things in the chamber you couldn’t see before. Maybe you should look at the chamber again.”;

My remaining question: is there a way to combine the two check rules? I’ve tried a few ways but seem to be getting the punctuation wrong.

Check lighting it with:
  if the player does not hold the second noun:
    say "You're not holding [the second noun]." instead;
  if noun is lit:
    say "That torch is already lit." instead.

Only the last phrase of the rule can end with a period. All intermediate phrases should end with semicolons.

However, it’s tidier to keep the check rules separate. Inform is meant to work with a lot of simple rules, rather than a few messy complicated ones.

You can also write rules like:

Check lighting a lit thing with something:
    say "That torch is already lit." instead.

Great. Thank you all for helping me work through this one. I think we can put it to bed now.

BTW, this is part of a project with my 10 year old grandson. I’m helping him write a small IF that he will use in an educational experiment. He keeps coming up with new things to add, and I have to keep telling him, “In the upgrade. Let’s just get this done for now.” Other than rules like this, he’s writing and typing the code.

What I’m learning here, I hope to apply to my own, more ambitious project. But plowing through the book is slow going.

Thanks again.