Pressing a button multiple times?

Hello! I’ve been browsing around for a little bit and haven’t been able to find this anywhere. I created a room with a series of buttons, where pressing each button alters a number. It would be nice if you could press the button multiple times with one action, so that you wouldn’t have to type out “press xyz button” each time to get the desired result (typing things out a couple times is fine, but if you want to press the button 10 times, it can get pretty tiring). Is there any way to have “press xyz button 10 times” make sense to Inform?

Sure thing!

Pressing it multiply is an action applying to one thing and one number. Understand "press [something] [number] time/times" as pressing it multiply. 

Report pressing it multiply: say "You press [the noun] [the number understood] time[if the number understood is not 1]s[end if]."

Lab is a room. A blue button is fixed in place in Lab.

Now, you’ll probably want to come up with a way to understand “twice.” And you might want a couple other formulations. But hopefully this will get you started!

1 Like

Hmm. Is there really no built-in way to repeat a command n times? That’s kind of surprising.

In any case, Matt’s solution will work if all you need to do is report the number of pushes. If pushing the button has an effect on the game state and you actually want the action to be repeated internally, you can accomplish this with a while loop just like our forefathers did in BASIC. I played around with the button scenario and came up with the following:

"Testing"

The Control Room is a room. 

A button is a kind of thing. A button has a number called the button number. A button usually has button number 0.

A blue button is a button in the control room. "A blue button sits below a screen displaying the number [button number of the blue button]."

A green button is a button in the control room. "A green button sits below a screen displaying the number [button number of the green button]." It has button number 1.

The action number is a number that varies. The action number is zero.

Pressing is an action applying to one thing. Understand "press [a button]" or "push [a button]" as pressing.

Carry out pressing:
	if the noun is the blue button:
		increment the button number of the noun;
	otherwise if the noun is the green button:
		now the button number of the noun is the button number of the noun times two.

Report pressing: say "You press [the noun]. The screen now displays the number [button number of the noun]."

Pressing it multiply is an action applying to one thing and one number. Understand "press [a button] [number] time/times" as pressing it multiply.

Check pressing it multiply: if the number understood is less than zero, say "You can't press a button negative times." instead.

Carry out pressing it multiply:
	now the action number is zero;
	while the action number is less than the number understood:
		increment the action number;
		silently try pressing the noun.
	
Report pressing it multiply: say "You push [the noun] [the number understood] time[s]. The screen now displays the number [button number of the noun]."

This example has two buttons that have different effects, both of which can be repeated using just one multiple-pressing action.

1 Like

Thanks for the suggestions! I’ll see if I can get it to work using your code.

I had to rebuild the thing from the ground up- my old code was getting really messy and I couldn’t figure out where the issues where- but I was eventually able to get it working with my buttons and it works great. Thanks again!! Including the code I have so far below for reference :slight_smile:

The Chamber is a room. Description is "Press the buttons to assign stats. Red for Strength, Orange for Dexterity, Yellow for Constitution, Green for Intelligence, Blue for Wisdom, and Purple for Charisma. Examine the screen to see your current stats."

A button is a kind of thing.

The red button is a button in Chamber. Description is "This button reads 'Strength'.".
The orange button is a button in Chamber. Description is "This button reads 'Dexterity'.".
The yellow button is a button in Chamber. Description is "This button reads 'Constitution'.".
The green button is a button in Chamber. Description is "This button reads 'Intelligence'.".
The blue button is a button in Chamber. Description is "This button reads 'Wisdom'.".
The purple button is a button in Chamber. Description is "This button reads 'Charisma'".

Strength is a number that varies.
Dexterity is a number that varies.
Intelligence is a number that varies.
Constitution is a number that varies.
Wisdom is a number that varies.
Charisma is a number that varies.
XP is a number that varies. XP is 5.


To say /l: say “[line break]”

A screen is scenery in Chamber. Description is "A giant screen with the following information displayed...[/l][/l]STR: [strength][/l]DEX: [dexterity][/l]INT: [intelligence][/l]CON: [constitution][/l]WIS: [wisdom][/l]CHA: [charisma][/l][/l]XP: [xp][paragraph break]”

The action number is a number that varies. The action number is zero.

Pressing is an action applying to one thing. Understand "press [a button]" or "push [a button]" as pressing.

Carry out pressing the red button:
	increase strength by 1;
	decrease xp by 1.

Carry out pressing the orange button:
	increase dexterity by 1;
	decrease xp by 1.

Carry out pressing the yellow button:
	increase constitution by 1;
	decrease xp by 1.

Carry out pressing the green button:
	increase intelligence by 1;
	decrease xp by 1.

Carry out pressing the blue button:
	increase wisdom by 1;
	decrease xp by 1.

Carry out pressing the purple button:
	increase charisma by 1;
	decrease xp by 1.

Report pressing: say "You press [the noun]. Up on the screen, you now see....[/l][/l]STR: [strength][/l]DEX: [dexterity][/l]INT: [intelligence][/l]CON: [constitution][/l]WIS: [wisdom][/l]CHA: [charisma][/l][/l]XP: [xp][paragraph break]"

Pressing it multiply is an action applying to one thing and one number. Understand "press [a button] [number] time/times" as pressing it multiply.

Check pressing it multiply: if the number understood is less than zero, say "You can't press a button negative times." instead.

Carry out pressing it multiply:
	now the action number is zero;
	while the action number is less than the number understood:
		increment the action number;
		silently try pressing the noun.
	
Report pressing it multiply: say "You push [the noun] [the number understood] time[s] Up on the screen, you now see....[/l][/l]STR: [strength][/l]DEX: [dexterity][/l]INT: [intelligence][/l]CON: [constitution][/l]WIS: [wisdom][/l]CHA: [charisma][/l][/l]XP: [xp][paragraph break]"

Check pressing a button:
	if xp is 0:
		say “You’ll need to gain some XP before you can add to your statistics!” instead.


2 Likes

Also (and I imagine you know this, but just to make absolutely sure) Inform has an “again” command (“g” for short) and I feel like most games that want the player to repeat things just make sure that they’re aware of that. Which is still a little tedious, but g enter g enter g enter isn’t so bad.

1 Like

Inform does have built-in loop structures if you want to take that approach! See §11.10 of the documentation (and page back and forth for some other loopish structures). You can say

Carry out pressing it multiply:
	repeat with the index running from 1 to the number understood:
		silently try pressing the noun.

I didn’t implement things this way because I figured that it might produce multiple actions reports in a way that would be tedious, but doing a try silently takes care of most of that. Though if you press the button more times than you have XP, the message that “You’ll have to add to your XP” comes before the final stat report. Maybe it’s be good for multiple pressing to check that the number understood is not greater than XP in this situation anyway?

Another thing is that if the buttons just change stats, you might not need a loop–just add the number understood to the stat and subtract it from XP. Here’s an implementation of that:

The Chamber is a room. Description is "Press the buttons to assign stats. Red for Strength, Orange for Dexterity, Yellow for Constitution, Green for Intelligence, Blue for Wisdom, and Purple for Charisma. Examine the screen to see your current stats."

A button is a kind of thing.

The red button is a button in Chamber. Description is "This button reads 'Strength'.".
The orange button is a button in Chamber. Description is "This button reads 'Dexterity'.".
The yellow button is a button in Chamber. Description is "This button reads 'Constitution'.".
The green button is a button in Chamber. Description is "This button reads 'Intelligence'.".
The blue button is a button in Chamber. Description is "This button reads 'Wisdom'.".
The purple button is a button in Chamber. Description is "This button reads 'Charisma'".

Strength is a number that varies.
Dexterity is a number that varies.
Intelligence is a number that varies.
Constitution is a number that varies.
Wisdom is a number that varies.
Charisma is a number that varies.
XP is a number that varies. XP is 5.


To say /l: say “[line break]”

A screen is scenery in Chamber. Description is "A giant screen with the following information displayed...[/l][/l]STR: [strength][/l]DEX: [dexterity][/l]INT: [intelligence][/l]CON: [constitution][/l]WIS: [wisdom][/l]CHA: [charisma][/l][/l]XP: [xp][paragraph break]”

Pressing it multiply is an action applying to one thing and one number. Understand "press [a button] [number] time/times" as pressing it multiply.

Check pressing it multiply: if the number understood is less than one, say "You must press at least once." instead.

Carry out pressing it multiply:
	decrease XP by the number understood;
	if the noun is:
		-- the red button:
			 increase strength by the number understood;
		-- the orange button: 
			increase strength by the number understood;
		-- the yellow button: 
			increase constitution by the number understood;
		-- the green button: 
			increase intelligence by the number understood;
		-- the blue button: 
			increase wisdom by the number understood;
		-- the purple button: 
			increase charisma by the number understood.
	
Report pressing it multiply: say "You push [the noun][if the number understood is greater than one] [the number understood] time[s][end if]. Up on the screen, you now see....[/l][/l]STR: [strength][/l]DEX: [dexterity][/l]INT: [intelligence][/l]CON: [constitution][/l]WIS: [wisdom][/l]CHA: [charisma][/l][/l]XP: [xp][paragraph break]"

Check pressing a button multiply:
	if xp is less than the number understood:
		say “You don't have enough XP to do that!” instead.

Instead of pushing a button:
	try pressing the noun multiply one. [this reads ugly, but that's how you call pressing it multiply with the number one]

I did a few other things. I put all the button-pushing rules into one, because that seemed more compact (you don’t want to know how badly I messed up the – syntax the first time I tried that). Also I used the built-in pushing action instead of making a pressing action, and I redirected pushing to pressing multiply, because that should make it easier to maintain the code (if you change which button does which thing, you only need to change it in one place instead of in the other).

2 Likes

Out of curiosity, I tried to implement a general mechanism that would interpret “[command] [number] times” automatically.

Unfortunately, the Inform parser isn’t smart enough to handle this. If it recognizes the first word, it’ll never check an UnknownVerb line, even if all lines for that verb end in failure.

This would probably still be possible by digging deep into the I6 of the parser, but there’d have to be some demand for such a feature.

1 Like

It’s crude, but you can check for [number] times in an After reading the command rule:

Lab is a room.

Command repeating is a truth state that varies.
Command repetition count is a number that varies.

Before reading a command: now command repeating is false.

After reading a command when the player's command includes "[number] time/times":
	cut the matched text;
	now command repeating is true;
	now command repetition count is the number understood.

First before when command repeating is true:
	let repeated action be the current action;
	now command repeating is false;
	repeat with index running from 2 to the command repetition count: [after this rule, the action will happen as part of the normal action processing, so we do repetition count minus 1 reps here]
		try the repeated action.

Now if the game requires “calculate two times two” that will be a problem, but…