19.7. Indexed text in variables, properties and tables

| Chapter 19: Advanced Text |

| 19.7. Indexed text in variables, properties and tables

So far we have been able to ignore the difference between "text" and "indexed text" because Inform has always been able to convert the former to the latter when needed. But the reverse conversion cannot be made. Ordinary text is stored in a specially compacted, read-only form: it's really part of the program, not part of the data.

The upshot of this is that if we need a variable to contain text which can be internally altered during play, then we must declare this. Instead of writing

> The player's forename is a text that varies.

we must write:

> The player's forename is an indexed text that varies.

Similarly for a property:

> A person has an indexed text called nickname.

For a named "let" value, we have a dilemma. These are normally created by writing something like:

> let the target be 17;

and Inform looks at the value - here 17, a number - to deduce that the new value, called "target", must be a number. So if we write:

> let the target be "excellent";

the result is that "target" is just a text, not an indexed text. We get around this by writing instead:

> let the target be an indexed text;

which creates the value, initially having the empty text as contents, and then

> let the target be "excellent";

to subsequently set its initial state.

The same issue arises for table columns, because we are allowed to simply write out values in table columns, and let Inform work out what kind they are. If there are blank entries, we can always write in the kind of value by hand, but suppose the entries are initially all filled in? The answer is that we can imitate this:

> Table of Neptune's Moons

> moon | surface (indexed text)
> "Nereid" | "utterly unknown"
> "Triton" | "cryovolcanic ridges"
> "Proteus" | "highly irregular and sooty"

Here the two columns are called "moon" and "surface", so we can talk about the "moon entry" and the "surface entry" once a row has been chosen: but the moon entry is text, and the surface entry is indexed text.

Lastly, suppose we want to define new phrases which deal with text. Here we have two things to remember. First, we need to say that the value decided is indexed text, not text; secondly, that text converts to indexed text by magic, but not vice versa. So:

> To decide what indexed text is (T - text) doubled:
> decide on "[T][T]".

works fine so long as we only want to pass text to it, but would not let us double indexed text. This, on the other hand, handles both:

> To decide what indexed text is (T - indexed text) doubled:
> decide on "[T][T]".

So for instance:

> say "Neptune" doubled.

prints "NeptuneNeptune" because Inform automatically converts "Neptune" to indexed text when "doubled" is used on it.

405
| Example Identity Theft Allowing the player to enter a name to be used for the player character during the game. |
RB

406
| Example Mirror, Mirror The sorcerer's mirror can, when held up high, form an impression of its surroundings which it then preserves. |
RB

407
| Example The Cow Exonerated Creating a class of matches that burn for a time and then go out, with elegant reporting when several matches go out at once. |
RB

Here we create a class of matches that can be used to burn other objects. Our objectives are as follow: Burned objects other than matches should be removed from play instantly (just as edible objects are instantly eaten). We could give everything its own burning duration, but that complicates matters and allows for fire to spread from one object to another; for an example of how to do that, see the example "In Fire or in Flood". Matches should be described to show whether they are burning or extinguished, and when the parser chooses one of several identical matches, it should make very clear which match it has selected. The game must sensibly select and, if necessary, automatically light new matches to carry out a >BURN THING command. The matches must burn for a set number of turns before going out, never to be used again. And finally, the part for which the indexed text will be useful: when several matches go out in the same turn, we want the game to print Four matches go out. rather than A match goes out. A match goes out. A match goes out. A match goes out. This last function appears down in Section 3, if we wish to skip ahead and look at it. "The Cow Exonerated" Section 1 - Simple Burning Understand the commands "light" and "burn" as something new. Understand "burn [something] with [strikable-match]" as burning it with. Understand "burn [something] with [something preferably held]" as burning it with. Burning it with is an action applying to one thing and one carried thing. Understand the command "light" as "burn". A thing can be flammable or impervious. A thing is usually impervious. Check burning something with something (this is the burn only with flaming matches rule): if the second noun is not a strikable-match, say "You can only light things with matches." instead; if the second noun is not flaming, say "[The second noun] needs to be burning first." instead. Check burning something with something (this is the burn only flammable things rule): if the noun is impervious, say "[The noun] cannot be burned." instead. Check burning something with something (this is the burn only things not held rule): say "[one of]It occurs to you to set down [the noun] before burning, just for safety's sake. [or]Again, you decide to put down [the noun] prior to burning. [or]You try setting down [the noun] as usual. [stopping][run paragraph on]"; silently try the player dropping the noun; if the player encloses the noun, stop the action. Carry out burning something with something (this is the simplistic burning rule): remove the noun from play. Report burning something with something: say "You burn up [the noun]." Rule for implicitly taking the second noun while burning something with something which is not a strikable-match: say "You can only light things with matches."; stop the action. Section 2 - Matches The word "matches" is used by Inform to compare snippets of text (see "Reading a command" in the Activities chapter). This can sometimes cause awkwardness if we also have a kind called "match", so for the occasion we will give our matches a more specialized name, never visible to the player: A strikable-match is a kind of thing. The plural of strikable-match is s-matches. A strikable-match has a number called duration. The duration of a strikable-match is usually 3. Rule for printing the name of a strikable-match: say "match". Rule for printing the plural name of a strikable-match: say "matches". Understand "match" as a strikable-match. Understand "matches" as a strikable-match. Flame-state is a kind of value. The flame-states are burnt, flaming, and new. Understand "burning" or "lit" as flaming. Understand "unused" as new. A strikable-match has a flame-state. A strikable-match is usually new. Understand the flame-state property as describing a strikable-match. Before printing the name of a strikable-match while asking which do you mean: say "[flame-state] ". Before printing the name of a strikable-match while taking inventory: say "[flame-state] ". Before printing the plural name of a strikable-match while taking inventory: say "[flame-state] ". Before printing the name of a strikable-match while clarifying the parser's choice of something: if not taking inventory, say "[flame-state] ". After printing the name of a strikable-match (called special-target) while clarifying the parser's choice of something: if the player carries the special-target: say " you're carrying"; otherwise if the special-target is in the location: say " on the ground"; otherwise: say " [if the holder of the special-target is a container]in[otherwise]on[end if] [the holder of the special-target]". Understand "strike [something]" as attacking. Understand "strike [strikable-match]" as striking. Striking is an action applying to one carried thing. Understand "burn [strikable-match]" as striking. Does the player mean striking a new strikable-match: it is very likely. Does the player mean striking a burnt strikable-match: it is unlikely. Check striking a strikable-match (this is the strike only new matches rule): if the noun is burnt, say "[The noun] has already burnt down and cannot be relit." instead; if the noun is flaming, say "[The noun] is already burning." instead. Carry out striking a strikable-match (this is the standard striking rule): now the noun is flaming; now the noun is lit. Report striking a strikable-match (this is the standard report striking rule): say "You light [the noun]." Before burning something with a new strikable-match (this is the prior lighting rule): say "(first [if the player does not carry the second noun]taking and [end if]lighting [the second noun])[command clarification break]"; silently try striking the second noun; if the second noun is not flaming, stop the action. Rule for implicitly taking a strikable-match (called target) while striking: try silently taking the target. Does the player mean burning something with a flaming strikable-match: it is very likely. Does the player mean burning something with a new strikable-match: it is likely. Does the player mean burning something with a burnt strikable-match: it is unlikely. Instead of burning a burnt strikable-match with something: say "[The noun] is completely consumed and cannot be relit." Section 3 - Putting the Matches Out Every turn: let N be 0; [here we track how many matches are being put out during this turn, so that we don't have to mention each match individually if several go out during the same move] repeat with item running through flaming s-matches: decrement the duration of the item; if the duration of the item is 0: now the item is burnt; now the item is unlit; if the item is visible, increment N; if N is 1: say "[if the number of visible flaming s-matches is greater than 0]One of the matches [otherwise if the number of burnt visible s-matches is greater than 1]Your last burning match [otherwise]The match [end if]goes out."; otherwise if N is greater than 1: let enumeration be indexed text; let enumeration be "[N in words]"; if N is the number of visible s-matches: if N is two, say "Both"; otherwise say "All [enumeration]"; otherwise: say "[enumeration in title case]"; say " matches go out[if a visible strikable-match is flaming], leaving [number of visible flaming s-matches in words] still lit[end if]." Section 4 - Scenario Old Chicago is a room. The player carries a flammable thing called a log. Understand "wooden" and "wood" as the log. The player carries two s-matches. The matchbox is an open openable container. It contains five s-matches. The player carries the matchbox. When play begins: now every strikable-match carried by the player is flaming; now every strikable-match carried by the player is lit. Test me with "i / burn match / i / i / burn log with match / burn matchbox with match / i".

Previous | Contents | Next