Distilling possible combinations into display text

This is probably more a lesson in programming, rather than just Inform7, but I’ll still need to figure how to implement in i7 nonetheless…

Essentially I’ve got a security panel in the game which will show text to the effect of “4 of 4 locks engaged”. And as the player unlocks each lock that ticks down; “3 of 4 locks engaged” etc.

The player can unlock locks in any order. Also, the player can re-lock locks and sometimes will need to. For reasons that are hard to explain here I need to be able to pull this information at the time the player examines the security panel (as opposed to changing a variable at the time of a lock being unlocked or re-locked).

Is there some “shortcut” besides me writing every iteration of “[if Lock1 is locked and Lock2 is locked and Lock3 is locked and Lock4 is locked]4 of 4 locks engaged[otherwise if Lock1 is unlocked and Lock2 is locked and Lock3 is locked and Lock4 is locked]3 of 4 locks engaged” until I’m dead?

let N be the number of unlocked locks;
say "[N] of 4 [regarding N]lock[s] engaged.".

The weird part here is the “regarding”: [s] is a text substitution that prints nothing if you’ve regarded the number one, and “s” if you’ve regarded any other number. So this ensures it says “lock” or “locks” as appropriate.

You can also say "lock[if N is not 1]s[end if] for the same effect.

How is “number of unlocked locks” determined on the fly?

That’s the magic of Inform! It’s designed to make “descriptions” like this (which get calculated as needed) as easy-to-use as possible.

As long as “locks” are a type of thing in the world, and they can be “unlocked”, this description will run through them all and count the ones that are unlocked.

One of the main benefits of Inform is the ability to say things like “move the player to a random lit room which is not the location” and have it do the calculations for you automatically. Many things that require an explicit loop in other programming languages can be done with descriptions in Inform.

2 Likes

Neat! Didn’t know I could just count stuff so directly. Thank you!