Problem with otherwise in nested ifs

My first attempt using nested ifs worked surprisingly well:

Instead of using something:
	if the player is in the Waiting Room:
		if the noun is the mirror, try examining the mirror;
		if the noun is the vending machine, say "Try putting a quarter in the coin slot.";
		if the noun is the discarded welding equipment, try taking the discarded welding equipment;
		if the noun is the rusty key, try unlocking the closet door with the rusty key;
	if the player is in the Exam Room:
		if the noun is the doorknob, try opening the sauna door;
		if the noun is the handle, try opening the cold storage door;
		if the noun is the brain scanner, try pushing the button;
		if the noun is the probe:
			if the probe is part of the brain scanner, try pushing the button;
			otherwise try inserting the probe into the brain scanner;
	if the player is in the Sauna:
		if the noun is the locker, say "Try putting a quarter in the coin slot.";
	if the noun is the glasses, try wearing the dark glasses;
	if the noun is the vial of alien toxin, try attacking the vial of alien toxin;
        otherwise say "You can't use that here."	

It compiles, runs, and (mostly) does what I’m wanting it to. Each condition is doing its specific thing (for example, using the mirror is looking at it, etc). The only problem is that the otherwise is triggering with every condition.

If I try using something that isn’t listed (for example, “Use book”, it responds “You can’t use that here.”), but if I type “Use vending machine”, it responds “Try putting a quarter in the coin slot.” and then on the next line is says “You can’t use that here.”

I’m sure I’m doing something dumb, but I can’t figure out what.

Any assistance would be greatly appreciated, thanks!

Cheers,
C. Scott Davis

You’re missing an “otherwise” that lines up with all your “ifs”.

I organize nested ifs a little differently-- it makes it easier to spot a problem. Like so:

Instead of using something:
	if the player is in the Waiting Room:
		if the noun is the mirror:
			try examining the mirror;
		otherwise if the noun is the vending machine:
			say "Try putting a quarter in the coin slot.";
		otherwise if the noun is the discarded welding equipment:
			try taking the discarded welding equipment;
		otherwise if the noun is the rusty key:
			try unlocking the closet door with the rusty key;
	otherwise if the player is in the Exam Room:
		if the noun is the doorknob:
			try opening the sauna door;
		otherwise if the noun is the handle:
			try opening the cold storage door;
		otherwise if the noun is the brain scanner:
			try pushing the button;
		otherwise if the noun is the probe:
			if the probe is part of the brain scanner:
				try pushing the button;
			otherwise:
				try inserting the probe into the brain scanner;
	otherwise if the player is in the Sauna:
		if the noun is the locker:
			say "Try putting a quarter in the coin slot.";
		otherwise if the noun is the glasses:
			try wearing the dark glasses;
	     otherwise if the noun is the vial of alien toxin:
		    try attacking the vial of alien toxin;
     otherwise: 
		say "You can't use that here;

I added a line at the end that if lined up with your primary ifs. Do you need the locations in this? If anything can be taken and moved to another location, this code won’t work for those items. And if the items are fixed in place, the locations aren’t needed.

1 Like

Thanks! I see now what the problems were.

To answer your question, I do need the location for a few items like the rusty key and the probe, which can only be used in those locations, but you’re right that I don’t need the locations for any of the other things, since they can’t be moved (D’oh!). It works correctly as is, but the location checks are definitely not needed.

Thanks again!

Cheers,
C. Scott Davis

OK-- you still don’t need the locations, then. If the items can only be used in those rooms and can only be seen in those rooms, then the code will fire correctly if you don’t include locations, and the locations just make the code more complicated.

I should have added that "otherwise: say “You can’t use that here” line to every room, otherwise it won’t recognize any non-usable items in those rooms.

Long series of complicated nested ifs are hard to read and maintain and easy to get wrong. Inform’s rules and their preambles make it easy in most cases to break them up. I’m pretty sure the series of bite-size easy-to-understand rules below offer the behavior you indicated you wanted.

Before using the mirror, instead try examining the mirror.

Before using the discarded welding equipment, instead try taking the discarded welding equipment.

Before using the rusty key when the location is the Waiting Room, instead try unlocking the closet door with the rusty key.

Before using the doorknob, instead try opening the sauna door.

Before using the handle, instead try opening the cold storage door.

Before using the brain scanner, instead try pushing the button.

Before using the probe when the location is the Exam Room:
 if the probe is part of the brain scanner, instead try pushing the button.
 otherwise instead try inserting the probe into the brain scanner.

Before using the glasses, instead try wearing the dark glasses.

Before using the vial of alien toxin, instead try attacking the vial of alien toxin.

Instead of using the locker, say "Try putting a quarter in the coin slot.".

Instead of using the vending machine, say "Try putting a quarter in the coin slot.".

Instead of using something, say "You can't use that here."

Is the locker supposed to have a different response than the vending machine?

4 Likes

… and (if named) easier to debug and reorder.

1 Like