Removing from a container

Hey everyone,

Ok, I have a scenario where I would like for the player to automatically dispose of a container when walking past a trash can. This is easy enough to accomplish if the container is empty. I did this:

if the player carries the bag and the bag carries nothing:
	say "You throw away the bag on your way out the door.";
	Now the bag is off-stage;

The problem is, I can’t find a way to get this to happen when the bag is not empty without destroying whatever is in the bag. Ideally, if the bag contained something, I would like to “silently try taking all from the bag” before moving the bag off-stage, but Inform didn’t like that. Nor did it like any variation of “removing it from” etc.

Is there any way to have inform automatically remove something from a container without knowing what the player might have put in it?

If you want to use the taking action with all its machinery, you could try something like this:

Every turn when [whatever conditions]: if the bag contains something: say "You empty the bag."; repeat with item running through things in the bag: try silently taking item; if the bag contains something: say "You couldn't take [list of things in the bag] from the bag, so you decide not to throw it away."; otherwise: say "You throw away the bag on your way out the door."; now the bag is off-stage.

The main part here is the repeat loop that sets “item” to everything in the bag in turn. Then I put in a bunch of other stuff because one of the takes could conceivably fail, and you need to cover that. Also you might need to explain what’s happening. Note that the two “if the bag contains something” checks aren’t redundant–the first one prints a message about emptying the bag and tries to empty it, the second checks to see whether the emptying succeeded.

But if there’s no possibility that taking could fail, then you can just move the contents of the bag directly to the player’s inventory, and this doesn’t require a loop:

Every turn when the player is in the lobby and the bag is carried: say "You [if something is in the bag]empty the bag and [end if]throw [if something is in the bag]it[otherwise]the bag[end if] out on your way out the door."; now the player carries everything in the bag; now the bag is off-stage.

That’s fantastic! Thank you!