I have a matchbox which is a container. I want to put 5 matches in it. Each match can be taken out of matchbox and used separately by the player but I also want to ensure player can put all 5 matches, seperately or all together, used or unused, back in the matchbox. How would the code look like?
Since it’s only 5, I’d just make matches a kind of thing, put five of them in the matchbox, and block the player from removing more than one from the matchbox at a time to avoid disambiguation headaches.
A match is a kind of thing. A matchbox is an openable, closed container. It contains five matches. Check taking a match: if the player carries a match: say "The box clearly warns DO NOT STRIKE MORE THAN ONE MATCH AT A TIME." instead.
Honestly, I don’t think you need the extra check here. Inform is reasonably good about identical objects when they’re defined this way, with nothing to distinguish them. It’ll happily let you X MATCH without asking which you mean, for example, and you can DROP THREE MATCHES THEN TAKE TWO MATCHES and such without an issue.
I think Daniel is technically right though personally I find working with multiple identical objects can quickly get very fiddly.
Anyway if you want a slightly more worked-out example, here’s something I whipped together:
"Test" by Mike Russo The lab is a room. A new-match is a kind of thing. The description of a new-match is "This match is unused." The printed name of a new-match is "unused match". The plural of new-match is matches. Understand "unused/new/-- match" as a new-match. Rule for printing the plural name of a new-match: say "unused matches". An old-match is a kind of thing. The description of an old-match is "This match is burnt-up." The printed name of an old-match is "burnt-up match". Understand "used/burnt/burnt-up/-- match" as an old-match. The plural of old-match is matches. Rule for printing the plural name of a old-match: say "burnt-out matches". The matchbox is a container in the lab. There are five new-matches in the matchbox. There are five old-matches in backstage. The block burning rule does nothing when the noun is a new-match. The block burning rule does nothing when the noun is an old-match. Check burning an old-match: Say "You've already used up that one!" instead. Carry out burning a new-match: Say "You strike the match -- it flares up then goes out."; Now the noun is nowhere; Move a random old-match in backstage to the player.
Your code solved problems I didn’t even think about yet, thank you! I have a (possibly dumb) question. Currently, when player uses a match, it burns out immediately. How do I make it possible to light up something with it, for example a candle?
How long do you want each match to last once it’s been lit?
Is this a matter of seconds or turns? I’m still fairly new to this
Not a dumb question at all, since this is where things get a bit interesting and you need to decide how you want your design to work! Like, the problem with matches is that the player can use them up, and if there are only five matches, once they’re burnt out, they’re burnt out. If lighting a candle is just an optional thing, or there are alternate solutions to the puzzle the candle’s important for, no big deal, you can keep with this more simulationist approach. If this is a critical path puzzle, though, you might need to adopt a different implementation, maybe one where you don’t implement individual matches and just allow the player to light a candle so long as they’re holding a matchbox.
Anyway here’s an implementation building on the above that allows you to light a candle with a match – for simplicity’s sake I continued having the matches used up immediately, though per Daniel’s comment you can also make things more complex by having matches burn out on a timer, too (you’d then need to add a property to a new-match tracking whether it’s on fire or not, and cue the ability to light things off of that, and add an event turning an on-fire new-match into an old-match, per the code above, after a certain number of turns)
Anyway, here’s the instantaneous code – again, you can just add this to what I posted before and it should work:
The block burning rule does nothing when the noun is the candle. The candle is in the lab. Carry out burning the candle: If the candle is lit, say "It's already burning." instead; If the player carries a new-match (called the foo): Say "You strike a match and use it to light the candle before it burns out."; Now the foo is nowhere; Move a random old-match in backstage to the player; Now the candle is lit. Match-lighting it with is an action applying to two touchable things. Understand "light [something] with [something]" as match-lighting it with. Check match-lighting it with: If the second noun is an old-match, say "You can't light anything with an already-used match!" instead; If the second noun is not a new-match, say "You can't light anything with that!" instead; If the noun is not the candle, say "That dangerous act would achieve little." instead; If the noun is lit, say "It's already burning." instead; Say "You strike a match and use it to light the candle before it burns out."; Now the second noun is nowhere; Move a random old-match in backstage to the player; Now the candle is lit. Does the player mean match-lighting something with a new-match: it is very likely.
EDIT: swapped out the slightly-hacky “understand X as a mistake” line for trying to light something with a burnt-out match to make things cleaner.
Thank you so much! I’ll have to analise your code to learn some new things but it worked!