How to pass an argument to a custom command?

What I’m trying to achieve is something akin to:

(the following is pseudocode, does not compile in any way)

The Lab is a room. 

A magic chest is in the lab. The magic chest is a locked, closed container.

Understand "scream [some text]" as screaming. Screaming is an action applying to nothing.

Carry out screaming (T - some text):
	if T is "mellon":
		now the magic chest is unlocked;
		say "You hear a clang and see the lock falling off the chest.";
	otherwise:
		say "(echo) '[T]'.".
		
test me with "scream open sesame/scream mellon".

I can do this quite easily with phrases within the source, but I’m completely stumped by how to do it via a player command…

I’m no good at wrangling text. I would do it by creating an “invisible” scenery object with the name “mellon,” which responds to anything but the scream action by saying, “You can’t see any such thing.”

Maybe I can hack together something like that, but that would work only for that specific case. What if for example I had to pass a number? Something like this:

(again, this doesn’t compile)

The Lab is a room. 

Coins is a number that varies. Coins is 10.

When play begins:
	now the command prompt is "Coins: [coins]>";
	
Understand "bet [a number]" as betting. Betting is an action applying to nothing.

Carry out betting (N - a number):
	if N > coins:
		say "You don't have that much to bet!";
	otherwise:
		now coins is coins - N;
		say "You bet and lose [N] coins.".
		
test me with "bet 14/bet 6".

It’s “number understood”, as in:

The Lab is a room. 

Coins is a number that varies. Coins is 10.

When play begins:
	now the command prompt is "Coins: [coins]>";
	
Understand "bet [a number]" as betting. Betting is an action applying to one number.

Carry out betting:
	if the number understood is greater than coins:
		say "You don't have that much to bet!";
	otherwise:
		now coins is coins minus the number understood;
		say "You bet and lose [number understood in words] coins.".
		
test me with "bet 14/bet 6/bet three".
1 Like

Text is a little weird. It’s “topic understood” and you need to use “matches” unless you’re using a table (see chs.17.5 & 18.33, respectively). I forget why.

The Lab is a room. 

A magic chest is in the lab. The magic chest is a locked, closed container.

Understand "scream [text]" as screaming. Screaming is an action applying to one topic.

Carry out screaming:
	if the topic understood matches "mellon":
		now the magic chest is unlocked;
		say "You hear a clang and see the lock falling off the chest.";
	otherwise:
		say "(echo) '[topic understood]'.".
		
test me with "scream open sesame/scream mellon".
6 Likes

Thanks again!

While I stared in confusion for quite some time at the error message given by the line:

Understand "scream [text]" as screaming. Screaming is an action applying to one text.

I did manage to have

Understand "bet [a number]" as betting. Betting is an action applying to one number.

compile. But I didn’t find out how to use the parameter in the function.

P.S.

Of course I immediately tried this:

Understand "bet [a number] [a number]" as betting. Betting is an action applying to two numbers.

And got this error as an answer:

I’m guessing there is not much to be done in this case ;_;

Once you have the number understood, you can do whatever you want with it. Can you give an example of what you mean?

As for this:

I’m pretty sure there’s a way to hack that in, but I can’t find it now and I’m on my way out. I tried searching this forum for “action two values” but didn’t find anything helpful. I’ll see what I can dig up later. (The thing is, having two kinds of value as the two nouns in an action is really just a limitation of I7, I think. IIRC i6 does not place that same restriction.)

That was before me posting the topic, after your example I got it all working. Thanks!

Don’t sweat it too much, it’s not something I desperately need right now, I just thought it could be quite useful in the future.

I… am often thinking if I just should have started with I6 instead of I7…

EDIT:

I tried to use a table, and as you said “matches” doesn’t work:

The Lab is a room.

Understand "scream [text]" as screaming. Screaming is an action applying to one topic.

Understand "mellon/amigo/friend" as "[friend]".
Understand "dog/cat/horse" as "[cat]".

Table of Projects
Project	Status
[this works]
"friend"	-1
"cat"	-1
[this doesn't
"[friend]"	-1
"[cat]"	-1
]

Check screaming:
	repeat through the Table of Projects:
		if project entry is the topic understood:
			continue the action;
	say "You shouldn't scream about such things.";
	stop the action.

Carry out screaming:
	say "Your screaming is allowed.".
		
test me with "scream open sesame/scream mellon/scream cat".

I’m using just “is” here, but with either “is” or “matches” if I try to use the tokens “[friend]” or “[cat]” the compiler gets angry at me ;_

What if you try changing the name of the column to “topic”? Columns called “topic” are special, as per §16.13 of Writing with Inform:

When double-quoted matter appears in a column of a table, Inform will normally treat that as text for printing out. The exception is when the column is called “topic”, where it is treated as text for comparing against what the player has typed. There is really only one operation allowed with topic columns, the “…listed in…” construction, but fortunately it is the one most often needed.

UPDATE: Yep:

The Lab is a room.

Understand "scream [text]" as screaming. Screaming is an action applying to one topic.

Understand "mellon/amigo/friend" as "[friend]".
Understand "dog/cat/horse" as "[cat]".

Table of Projects
topic	Status
"[friend]"	-1
"[cat]"	-1

Check screaming:
	if the topic understood is a topic listed in the Table of Projects:
		continue the action;
	otherwise:
		say "You shouldn't scream about such things.";
		stop the action.

Carry out screaming:
	say "Your screaming is allowed.".
		
test me with "scream open sesame/scream mellon/scream cat".
2 Likes

That did the job. Many thanks!

(I7 quirks are quite quirky)

Ok, I found the code I was looking for. @ChristianB originally posted this back in 2011 (which was subsequently quoted by @Draconis in '14) and @zarf posted something similar in '13. Both of these were adapted from the DM4 (p. 489). Their code adds a third noun token. I adapted it to add a second number token instead which turned out to be a little trickier than I thought it would be. It could presumably be adapted to work with user-defined KOVs, (to halfway answer the question posed by Daniel over six years ago) but this is it for now:

The Lab is a room. 

Adding it to is an action applying to one number.

Include (-
Global second_number;
-) after "ICL Commands" in "Output.i6t".

The second number understood is a number that varies.
The second number understood variable translates into I6 as "second_number".

Include(-
[ SecondNum x firstnum;
	firstnum = parsed_number; ! saves the first parsed number locally
	x = ParseToken(ELEMENTARY_TT, NUMBER_TOKEN);
	if (x == GPR_FAIL or GPR_REPARSE) return x;
	second_number = parsed_number; ! saves the second parsed number into a global
	parsed_number = firstnum; ! returns the first number to its usual global spot
	return GPR_PREPOSITION;
];
-).

The Understand token second-number translates into I6 as "SecondNum".

Understand "add [number] to [second-number]" as adding it to.

Check adding it to:
	say "adding [number understood] to [second number understood]...".
	
Carry out adding it to:
	let total be a number;
	now total is the number understood plus the second number understood;
	say "total = [total]."
	
test me with " add 2 to 7 / add one to three / add thirteen to 75 / add 1465 to 6356".
4 Likes

That’s incredible stuff! I can’t thank you enough.

Now the next level would be having one number and one text, two or even tree texts. I think I could see a story in which you cast spells by combining a few “words of power”…

Now that I type this I think that maybe this could be achieved with only text argument and some trickery, but I guess I’ll have a stab at it when writing that story ^^.

Very nice! Yes, it wouldn’t be hard to extend this to other kinds of value, because Inform names the internal parsing routines for them in a predictable way. The biggest issue now is that Inform won’t treat the second number as part of the action variables (so it won’t get saved and restored when a new action is attempted, won’t become part of a stored action, etc), but that’s something that can’t really be fixed until Inform gets open-sourced.