Chapter 11: Phrases
11.3. Pattern matching

The following phrases all use different wording from each other:

award 2 points
award 8 points
award 30 points

but they have the same meaning, with only one detail being different between them. A single definition (internal to Inform, since "award ... points" comes built in) covers these and all similar cases. It is written like so:

To award (some - a number) points: ...

This pattern of words matches the text "award 4 points", and indeed "award four points", but not

award "blue cheese" points;

which results in the following error:

Problem. You wrote 'award "blue cheese" points' , but '"blue cheese"' seems to be some text, whereas I was expecting to find a number there.

The number does not need to be a literal constant such as 52, but can be anything which works out to be a number: the name of a number variable, for instance.

The bracketed part of the definition takes the form (name - what it is). The definition only applies if the text supplied agrees with the "what it is" part - for instance, 8 agreed with being a number, but "blue cheese" did not. If the definition does apply, then the name holds whatever value was supplied. For instance:

To slam shut (box - an open container): say "With great panache, you slam shut [the box]."

The "what it is" will usually be a description of some objects, or the name of a kind of value, but it is also allowed to give a specific value. For instance, we could define:

To award (some - 13) points: say "You shiver uncontrollably."

which would withhold this unlucky bounty, applying only to that one case. (As elsewhere in Inform, more specific definitions take priority over more general ones, so "award (some - 13) points" takes priority over "award (some - a number) points".)

Sometimes it will not be possible to tell if the value supplied meets the requirements until the game is actually playing. If, at run-time, no definition fits some phrase which has to be carried out, a run-time problem message is produced.

Finally, and more straightforwardly, we can specify variations in wording using slashes between alternative words in a "To ..." definition. For instance:

To award (some - a number) point/points: ...

allows the final word to be either "point" or "points". Slashes like this can only be used with literal words (not bracketed values) and give alternative forms only of a single word at a time. (If we need more variation than that, we should make more than one definition.)


165
* Example  Ahem
Writing a phrase, with several variant forms, whose function is to follow a rule several times.

RB
166
** Example  Ferragamo Again
Using the same phrase to produce different results with different characters.

RB


PreviousContentsNext