How can I use substring matches for text substitution variables?

#1

I’m working with Inform 7 6M62 and I’ve found myself in need of the ability to have the parser recognize a range of possible user input paired with the action ‘say’ as requiring common handling. The code I have at the moment is as follows:

Understand "*creativity*" as "[creativity control]".
After answering Nuvi that "[creativity control]" for the first time:
	now Nuvi is ready to ride;
	[todo: how can we examine the exact string the user gave and then respond appropriately here?]
	say "all right then, off we go!"

This compiles, but saying ‘creativity’ or any string containing ‘creativity’ does not trigger the expected response. I see from this answer that a raw command can be parsed for substrings, but I’m not sure how I would adapt this example to a case where I want to specifically apply handling for the ‘say’ action.

How can I use substring matches for text substitution variables? If that’s not possible, how can I detect a substring within the string argument given to the ‘say’ action and apply the ‘for the first time’ modifier?

Also, how can I access the full input argument to ‘say’ from within the handler?

0 Likes

(Daniel Stelzer) #2

You’re on the right track! But Inform tries to avoid “sigils” wherever possible: ideally you’ll never have to use any punctuation mark in an unusual way. That includes stars for “any text here”.

I think what you want is this:

After answering Nuvi that when the topic understood includes "creativity":

(This should be a substring match. I think. Sometimes the distinction is matches vs includes, sometimes it’s matches vs exactly matches, and I can never remember which is which—nor do I have I7 here to test it. Sorry.)

EDIT: It’s “includes”, not “matches” here. My bad. Edited.

If you then want to check the exact text the player entered, “the player’s command” is the entire input line, and “the topic understood” is the part that matched [text] in the grammar (the words you’re talking about or looking up or whatever).

1 Like

(matt w) #3

For the topic understood it’s includes vs. matches, so you want “includes” there.

Basically:
for snippets (which is almost always “the topic understood”) it’s [snippet] matches [topic], for an exact match, [snippet] includes [topic] for a partial match. See Writing with Inform §18.33
for texts it’s [text one] exactly matches the text [text two] for an exact match, [text one] matches the text [text two] for a partial match, but in reverse–it checks if text two includes text one. EDIT: No, that was wrong, it checks if text one includes text two, just like the topic/snippet one.

I talk about how much I hate this every time it comes up, but I really hate it! I have to look it up every. single. time.

EDITED TO ADD: Since the comparisons work the same way, maybe a way for Inform to handle this confusion without breaking existing source code would be to allow “(topic) exactly matches (snippet)” as a synonym for “matches,” and “(text one) includes the text (text two)” as a synonym for “matches the text”? That way it would always be safe to use “includes” for inclusion and “exactly matches” for exact match. Well, as long as you remember to use “the text” in text comparisons.

1 Like