Another 6G60 vs 6M62 parsing/rule question (instead of ... )

I’m back with another 6G60 vs 6M62 parsing question.

Here’s some code from my game originally written in 6G60:

A light source is a kind of device. Carry out switching on a light source: now the noun is lit. Carry out switching off a light source: now the noun is unlit. 

A flashlight is a light source. The description of the flashlight is "A heavy duty, black metal flashlight.[if the battery compartment is open] The end of the metal tube is open, revealing the battery compartment.[otherwise] The end of the tube screws open, allowing access to the batteries." One battery compartment is part of the flashlight. 

A battery is a kind of thing. The description of a battery is "A standard D-cell battery."

[ actions related to the flashlight and battery compartment ]
Understand "turn on [device]" as switching on. 
Understand "turn off [device]" as switching off. 
Understand "open [openable closed thing]" as opening. 
Understand "close [openable open thing]" as closing. 
Understand "put [something] in [container]" as inserting it into. 

Instead of opening the flashlight, try opening a random battery compartment which is part of the noun. 
Instead of closing the flashlight, try closing a random battery compartment which is part of the noun. 
Instead of inserting something into the flashlight, try inserting the noun into a random battery compartment which is part of the second noun. 

[ require four batteries for the flashlight to operate ]
Definition: the flashlight is empty: 
	if the battery compartment contains four batteries:
		no; 
	yes. 

Instead of switching on an empty flashlight: 
	[ make sure we have enough batteries ]
	if the battery compartment contains no batteries:
		say "Nothing happens ... maybe it needs batteries?";
	otherwise:
		say "Nothing happens ... maybe it needs more batteries?".
   
Before switching on a flashlight:
	if the battery compartment is open:
		[ make sure the battery compartment is closed before turning on the flashlight ]
		silently try closing the flashlight;
		say "You screw closed the end of the tube and turn on the flashlight.";
	otherwise:
		say "You turn on the flashlight.";

The compile in 6M62 is failing on “instead of switching on an empty flashlight”:

You wrote 'Instead of switching on an empty flashlight', which seems to introduce a rule taking effect only if the action is 'switching on an empty flashlight'. But that did not make sense as a description of an action. I am unable to place this rule into any rulebook.

If I still want to use ‘instead of’ it looks like I have to do something like this?

Instead of switching on the flashlight:
	if the flashlight is empty:
		[ make sure we have enough batteries ]
		if the battery compartment contains no batteries:
			say "Nothing happens ... maybe it needs batteries?";
		otherwise:
			say "Nothing happens ... maybe it needs more batteries?";
	otherwise:
		[ make sure the battery compartment is closed before turning on the flashlight ]
		if the battery compartment is open:
			silently try closing the flashlight;
			say "You screw closed the end of the tube and turn on the flashlight.";
		otherwise:
			say "You turn on the flashlight.".

But since that’s an ‘instead of’ I need to actually turn the flashlight on. If I use ‘silently turn on the flashlight’ I get a loop (because that fires the ‘instead of’ code again). I’ve tried ‘now the flashlight is lit’ but that doesn’t actually seem to change the property of the flashlight to on.

I still want (or need) the ‘before switching on the flashlight’ to check for an open flashlight. I only need the ‘instead of’ for when the flashlight is empty. Can someone please nudge me in the correct direction to use code close to the original ‘instead of turning on an empty flashlight’?

Thank you!

This is nothing you’re doing wrong! It’s a really weird bug which arises when you use “empty” before the name of a specific thing. “Flashlight” in your code being a specific thing rather than a kind. More discussion here.

The workaround that requires the least work is to change “Instead of switching on an empty flashlight” to “Instead of switching on the flashlight when the flashlight is empty.” That means that the Instead rule won’t fire when the flashlight isn’t empty, so it will avoid your problem.

Another thing you could do is stick “continue the action” into your Instead rule whenever you need to pass it on the to rest of the action rules.

One stylistic thing, though, is that it seems like a lot of what you’re doing here could be accomplished by breaking the code up into Check/Carry Out/Report rules, where I guess you can just rely on the standard Carry Out and Report. So you could say:

Check switching on the flashlight when the flashlight is empty: 
	[ make sure we have enough batteries ]
	if the battery compartment contains no batteries:
		say "Nothing happens ... maybe it needs batteries?" instead;
	otherwise:
		say "Nothing happens ... maybe it needs more batteries?" instead.

This is nice because you can have a bunch of different such check rules, and you don’t have to worry about having one run when you don’t want, as in Instead–as long as you reserve “instead” in the check rule for failure cases, when the check rule doesn’t hit one of those, it’ll go on to the next check rules and then to the carry out phase when the check rules run out.

3 Likes

Fantastic, thanks again Matt! That worked.Not sure why I didn’t find that other thread about the “empty” bug when I was searching.

Once I get this game working with the changes I need for 6M62 I might go back and clean up some more of the syntax.I do have a lot of other check rules so I’m not sure why I didn’t use that for the flashlight when I wrote this originally. :slight_smile: