§18.5. New activities

Activities are all about influencing the standard mechanisms which Inform uses, so it might at first seem that there is no need to create new activities: but on further reflection, quite a lot of the writing of interactive fiction involves creating new and systematic ways to do things, and as soon as we have a general rule, we will want to have exceptions. Inform therefore allows us to create our own activities, giving us ways to influence the operation of our own mechanisms.

There are two kinds of activity: those which relate to a specific value (usually an object but not necessarily), and those which do not. Here are some examples of activities being created:

Assaying is an activity.
Analysing something is an activity.
Announcing something is an activity on numbers.

Inform looks for the clue "something" (or "of something") after the activity's name to see if it will work on a value: so analysing and announcing will do, but assaying won't. If we don't specify a kind, Inform assumes the value will be an object, as if we had written:

Analysing something is an activity on objects.

As always in Inform, the names of activities are themselves values.

"assaying activity" has kind activity on nothing
"analysing activity" has kind activity on objects
"announcing activity" has kind activity on numbers

Creating an activity is like creating an action: it automatically makes new rulebooks - "before analysing", "for analysing" and "after analysing" - but they start out empty, so the activity does nothing yet. Just as it does for rulebooks, Inform defines the adjectives "empty" and "non-empty" for activities to test this state:

if the analysing activity is empty, ...

will be true only when all three of its rulebooks are empty.

A newly created activity never happens unless we take steps to make it do so. We can make an activity happen at any time by writing phrases like so:

carry out the (activity) activity

This phrase carries out the given activity, which must be one not applying to any value. Example:

carry out the assaying activity;

carry out the (activity on values) activity with (value)

This phrase carries out the given activity, which must apply to a kind of value matching the one supplied. Example:

carry out the analysing activity with the pitchblende;
carry out the announcing activity with the score;

To make the activity do something useful, we need to put a rule into its "for" rulebook:

Rule for announcing a number (called N): say "Ladies and gentlemen, [N]."

The last for assaying rule:
    say "Professionally, you cast an eye around mineral deposits nearby, noticing [list of rocks in the location]."

"The last" is a technicality about rulebooks (see the next chapter) which, put briefly, guarantees that this rule comes last among all possible "for assaying" rules. This is good form because the whole point of an activity is to make it easy for further rules to interfere - so we deliberately hang back to last place, giving precedence to anybody else who wants it.

The "for" rulebook is one where rules stop the activity, by default, when they take effect - in the same way that the "instead" rules stop actions by default. If this causes problems, we can use:

continue the activity

This phrase should be used only in rules in activity rulebooks. It causes the current rule to end, but without result, so that the activity continues rather than stopping as a result of the rule. This is useful for rulebooks (like the "for" rulebook of an activity) where the default is that a rule does stop the activity.

Activities are more useful than they first appear. Every new one provides a context which other activities can observe. We could, for instance, define

Rule for printing the name of a rock while assaying: ...

so that during assays more technical names are used.


arrow-up.pngStart of Chapter 18: Activities
arrow-left.pngBack to §18.4. While clauses
arrow-right.pngOnward to §18.6. Activity variables

**ExampleAARP-Gnosis
An Encyclopedia set which treats volumes in the same place as a single object, but can also be split up.

***ExampleCrusoe
Adding a "printing the description of something" activity.

The built-in behavior of Inform is to print a line after a device is examined, saying whether the item is on or off. This is often inappropriate, and we could simply turn off that behavior in general by instructing Inform to ignore the "examine described devices rule" (see the chapter on rulebooks).

Perhaps, though, we would like continue to have a short passage about the action of any switched on device; we'd just like a little more control over what it says from time to time. And in that case, we might change the rule to give a new activity control over that portion of the description:

paste.png "Aftershock"

Section 1 - Showing actions

Showing action of something is an activity.

Rule for showing action of something (called item):
    if the item is switched on, say "[The item] is switched on.";
    otherwise say "[The item] is switched off."

Borrowing from the rulebooks chapter, we can replace the standard "examine described devices" rule with something that uses this activity.

The new described devices rule is listed instead of the examine devices rule in the carry out examining rules.

This is the new described devices rule:
    if the noun is a device:
        carry out the showing action activity with the noun;
        now examine text printed is true.

Thus far we have essentially replicated the original behavior, but we've made it possible to write specialized behavior for devices, and to invoke that behavior in other places:

Report switching on something:
    say "You flip a switch. ";
    carry out the showing action activity with the noun instead.

This might be useful for an electric lamp kind:

Section 2 - Electric Lamps

An electric lamp is a kind of device.

Rule for showing action of an electric lamp (called item):
    if the item is switched on, say "[The item] is lit[if the number of visible lit things is greater than 1], competing with [the list of visible lit things which are not the item][end if].";
    otherwise say "[The item] is dark."

Carry out switching on an electric lamp: now the noun is lit. Carry out switching off an electric lamp: now the noun is unlit.

Section 2 - The Scenario

The time of day is 3:47 AM. When play begins, now the right hand status line is "[time of day]".

The Downstairs Hallway is a dark room. "The only room in the house with no furniture and almost nothing on the walls. At times like this you always notice the crack in the plaster, originating near the light fixture and running almost all the way to the wall."

A plastic jug of filtered water is in the Downstairs Hallway. The description is "Five gallons, not that that will last you very long, hot as it has been lately."

The crack is scenery in the Hallway. The description is "No, the ceiling isn't going to fall on you today."

The light fixture is an electric lamp in the Hallway. It is switched on, lit, and scenery. The description is "A plain globe of frosted glass containing the light bulb. Nothing special, and you never think about it except when, as now, you are forced to spend hours in this room."

The flashlight is an electric lamp carried by the player. The description is "A shiny red flashlight." The portable radio is a device carried by the player. The description is "A small battery-operated radio which you received for free with your subscription to US News & World Report. It has served you well through many earthquakes past."

And with our activity, we can override the flashlight's electric lamp behavior with new behavior:

Rule for showing action of the flashlight:
    if the flashlight is switched on, say "A strong, narrow beam of light shines from the flashlight.";
    otherwise say "It is currently switched off."

...or give special actions for the radio:

Rule for showing action of the radio:
    if the radio is switched on, say "Through the static, you pick up pieces of discussion: a 6.7 on the Richter scale, epicenter... something about Topanga... but it crackles out again.";
    otherwise say "The radio is silent. You're saving the batteries."

Instead of listening in the presence of the switched on radio:
    carry out the showing action activity with the radio instead.

Test me with "examine light / switch light off / switch flashlight on / switch radio on / examine radio / examine flashlight".

***ExampleAftershock
Modifying the rules for examining a device so that all devices have some specific behavior when switched on, which is described at various times.

The built-in behavior of Inform is to print a line after a device is examined, saying whether the item is on or off. This is often inappropriate, and we could simply turn off that behavior in general by instructing Inform to ignore the "examine described devices rule" (see the chapter on rulebooks).

Perhaps, though, we would like continue to have a short passage about the action of any switched on device; we'd just like a little more control over what it says from time to time. And in that case, we might change the rule to give a new activity control over that portion of the description:

paste.png "Aftershock"

Section 1 - Showing actions

Showing action of something is an activity.

Rule for showing action of something (called item):
    if the item is switched on, say "[The item] is switched on.";
    otherwise say "[The item] is switched off."

Borrowing from the rulebooks chapter, we can replace the standard "examine described devices" rule with something that uses this activity.

The new described devices rule is listed instead of the examine devices rule in the carry out examining rules.

This is the new described devices rule:
    if the noun is a device:
        carry out the showing action activity with the noun;
        now examine text printed is true.

Thus far we have essentially replicated the original behavior, but we've made it possible to write specialized behavior for devices, and to invoke that behavior in other places:

Report switching on something:
    say "You flip a switch. ";
    carry out the showing action activity with the noun instead.

This might be useful for an electric lamp kind:

Section 2 - Electric Lamps

An electric lamp is a kind of device.

Rule for showing action of an electric lamp (called item):
    if the item is switched on, say "[The item] is lit[if the number of visible lit things is greater than 1], competing with [the list of visible lit things which are not the item][end if].";
    otherwise say "[The item] is dark."

Carry out switching on an electric lamp: now the noun is lit. Carry out switching off an electric lamp: now the noun is unlit.

Section 2 - The Scenario

The time of day is 3:47 AM. When play begins, now the right hand status line is "[time of day]".

The Downstairs Hallway is a dark room. "The only room in the house with no furniture and almost nothing on the walls. At times like this you always notice the crack in the plaster, originating near the light fixture and running almost all the way to the wall."

A plastic jug of filtered water is in the Downstairs Hallway. The description is "Five gallons, not that that will last you very long, hot as it has been lately."

The crack is scenery in the Hallway. The description is "No, the ceiling isn't going to fall on you today."

The light fixture is an electric lamp in the Hallway. It is switched on, lit, and scenery. The description is "A plain globe of frosted glass containing the light bulb. Nothing special, and you never think about it except when, as now, you are forced to spend hours in this room."

The flashlight is an electric lamp carried by the player. The description is "A shiny red flashlight." The portable radio is a device carried by the player. The description is "A small battery-operated radio which you received for free with your subscription to US News & World Report. It has served you well through many earthquakes past."

And with our activity, we can override the flashlight's electric lamp behavior with new behavior:

Rule for showing action of the flashlight:
    if the flashlight is switched on, say "A strong, narrow beam of light shines from the flashlight.";
    otherwise say "It is currently switched off."

...or give special actions for the radio:

Rule for showing action of the radio:
    if the radio is switched on, say "Through the static, you pick up pieces of discussion: a 6.7 on the Richter scale, epicenter... something about Topanga... but it crackles out again.";
    otherwise say "The radio is silent. You're saving the batteries."

Instead of listening in the presence of the switched on radio:
    carry out the showing action activity with the radio instead.

Test me with "examine light / switch light off / switch flashlight on / switch radio on / examine radio / examine flashlight".

The built-in behavior of Inform is to print a line after a device is examined, saying whether the item is on or off. This is often inappropriate, and we could simply turn off that behavior in general by instructing Inform to ignore the "examine described devices rule" (see the chapter on rulebooks).

Perhaps, though, we would like continue to have a short passage about the action of any switched on device; we'd just like a little more control over what it says from time to time. And in that case, we might change the rule to give a new activity control over that portion of the description:

paste.png "Aftershock"

Section 1 - Showing actions

Showing action of something is an activity.

Rule for showing action of something (called item):
    if the item is switched on, say "[The item] is switched on.";
    otherwise say "[The item] is switched off."

Borrowing from the rulebooks chapter, we can replace the standard "examine described devices" rule with something that uses this activity.

The new described devices rule is listed instead of the examine devices rule in the carry out examining rules.

This is the new described devices rule:
    if the noun is a device:
        carry out the showing action activity with the noun;
        now examine text printed is true.

Thus far we have essentially replicated the original behavior, but we've made it possible to write specialized behavior for devices, and to invoke that behavior in other places:

Report switching on something:
    say "You flip a switch. ";
    carry out the showing action activity with the noun instead.

This might be useful for an electric lamp kind:

Section 2 - Electric Lamps

An electric lamp is a kind of device.

Rule for showing action of an electric lamp (called item):
    if the item is switched on, say "[The item] is lit[if the number of visible lit things is greater than 1], competing with [the list of visible lit things which are not the item][end if].";
    otherwise say "[The item] is dark."

Carry out switching on an electric lamp: now the noun is lit. Carry out switching off an electric lamp: now the noun is unlit.

Section 2 - The Scenario

The time of day is 3:47 AM. When play begins, now the right hand status line is "[time of day]".

The Downstairs Hallway is a dark room. "The only room in the house with no furniture and almost nothing on the walls. At times like this you always notice the crack in the plaster, originating near the light fixture and running almost all the way to the wall."

A plastic jug of filtered water is in the Downstairs Hallway. The description is "Five gallons, not that that will last you very long, hot as it has been lately."

The crack is scenery in the Hallway. The description is "No, the ceiling isn't going to fall on you today."

The light fixture is an electric lamp in the Hallway. It is switched on, lit, and scenery. The description is "A plain globe of frosted glass containing the light bulb. Nothing special, and you never think about it except when, as now, you are forced to spend hours in this room."

The flashlight is an electric lamp carried by the player. The description is "A shiny red flashlight." The portable radio is a device carried by the player. The description is "A small battery-operated radio which you received for free with your subscription to US News & World Report. It has served you well through many earthquakes past."

And with our activity, we can override the flashlight's electric lamp behavior with new behavior:

Rule for showing action of the flashlight:
    if the flashlight is switched on, say "A strong, narrow beam of light shines from the flashlight.";
    otherwise say "It is currently switched off."

...or give special actions for the radio:

Rule for showing action of the radio:
    if the radio is switched on, say "Through the static, you pick up pieces of discussion: a 6.7 on the Richter scale, epicenter... something about Topanga... but it crackles out again.";
    otherwise say "The radio is silent. You're saving the batteries."

Instead of listening in the presence of the switched on radio:
    carry out the showing action activity with the radio instead.

Test me with "examine light / switch light off / switch flashlight on / switch radio on / examine radio / examine flashlight".