Pressing buttons in the correct sequence

Hey all-
I’ve gotten halfway through writing my first game, and this is only my second question here! Yay me!

I have a keypad lock with 9 buttons (labeled #1 button through #9 button). They are all separate objects that are part of the lock. They’re all good to go and pressable.

Of course I want the player to press them in a certain order. If the player gets the order wrong, nothing happens. If s/he gets it right, the lock is unlocked.

I just can’t find an example that is remotely close to what I want- the closest I’ve found is the Leopard-skin example, and so much of that either isn’t relevant, or isn’t spelled out well (like what is the way to tell the program what the correct sequence is?).

I’ve been searching this site and the documentation for hours now, and I can’t get any example that even remotely helps me. There’s an old question here, but that isn’t helping because it’s an exchange that is clearly between two experienced coders. Can someone point me to a piece of code that is similar to this so I can learn the rules?

1 Like

It would be easier for you and maybe for the player if you allowed just entering a sequence, e.g., enter 3141 and you could follow the Safety example. But that might not be appropriate depending on whether you want side effects for failure or other reasons.

One thing I love about this site is that if no one posts an answer to an authoring question, I can put down some code that works but is ugly and someone else will almost immediately post much better code that is perfect and also which I can steal for my own games.

Here’s what I’ve done for a similar puzzle (my game revolves around making noises with your feet by travelling from room to room):

A room has some text called creaktext. The creaktext of a room is usually "creak".

The playercreaks is a list of text that varies. The playercreaks is {"howl", "howl", "howl", "howl", "howl", "howl", "howl", "howl"}.

Every turn when the player is in creakyregion (this is the stalkertext rule):	
	add creaktext of the location to playercreaks;
	remove entry 1 from playercreaks;
	if playercreaks is {"creak", "creak", "rattle", "rattle", "shake", "shake", "turn", "turn"}:
		destroy the house;

So you’d want to change ‘every turn’ to ‘instead of pushing a button’ or something like that, and instead of ‘creaktext’ you’d have ‘buttonnumber’ or something like that.

3 Likes

I’m of course tempted to hack out some code, but you actually just asked for an example to use and modify, and the closest thing I can find in the Inform Recipe Book is #433, which involves a safe dial that has to be set number by number.

2 Likes

You could also achieve this with a text variable that is built up with a string of digits as you press buttons and then after so many buttons pressed (like 4 different button presses), compare that variable to the code required.
That would keep the original idea of actually pressing each button intact without switching it to user input.

2 Likes

This is just a quick rudimentary implementation to illustrate the variable approach.
I’m sure with more thought this can be improved but should give you a place to start from.

InputSequence is text that varies.

Instead of pressing Button1:
	Now InputSequence is the substituted form of "[InputSequence]1";

This builds the string by creating a sequence of “[the last digits pressed so far]+the value of the button pressed”…

Then the rest of this goes directly under the 'Now InputSequence is…" line.

if the number of characters in InputSequence is 4:
		if InputSequence matches the text "2134":
			say "[line break]something, somewhere, clicks..";
			Now InputSequence is "";
		otherwise:
			say "[line break]hmm..entering in [InputSequence] seems to have done nothing.";
			Now InputSequence is "";
otherwise:
	say "You press Button #1...";

The rules of the game don’t allow for simply entering a sequence. I have a very limited verb set, all novel actions. Which of course is making me doubt the wisdom of attempting this type of puzzle, especially in my first game. But then, no one has ever accused me of being excessively wise.

3 Likes

Thanks for this. I can puzzle over this and try to figure out why it works and then adapt it, which is how I learn best.

1 Like

If you give each button a bit of text.

Button1 has some text called digit.
The digit of button1 is "1".

Repeat for each button, then you only need one if statement…

Instead of pressing:
	Now InputSequence is the substituted form of "[InputSequence][the digit of the noun]";
	if the number of characters in InputSequence is 4:
		if InputSequence matches the text "2134":
			say "[line break]something, somewhere, clicks..";
			Now InputSequence is "";
		otherwise:
			say "[line break]hmm..entering in [InputSequence] seems to have done nothing.";
			Now InputSequence is "";
	otherwise:
		say "You press Button #[the digit of the noun]...";
2 Likes

Thank you! I’ll see if I can figure out why this works and scavenge some of it.

2 Likes

Thanks for this. Just got to sit playing with it until I can see why it works.

This worked beautifully!
Now trying to adapt it to a series of different actions that have to happen in order.
This was tremendously helpful.

2 Likes