Interactive Fiction_11 – Variables

Variables

Simple example:  Sometimes I find the use of a simple variable is the only way I can figure out how easily control certain actions. 

The restaurant is a room.
The customer is a person in the restaurant.
The meal is in the restaurant.  The meal is edible.
The money box is a container in the restaurant.  The money box is locked.
The wages is in the money box.

x is a number that varies.
x is 1.

Instead of giving the meal to the customer:
    say “well done, now you can get your wages.”;
    now the customer carries the meal;
    now x is 2.

Every turn:
    If x is 1:
        Now the money box is locked;
    If x is greater than 1:
        Now the money box is unlocked.

Complex example:

The cabin is a room.. The cabin is dark.
 
Instead of looking for the second time:
say “The cabin is rustic, and you must be quick and find the oil can, as your lamp is low on fuel.”.
 
The player carries a lamp. The lamp is lit. The lamp has a number called fuel-remaining. The fuel-remaining of the lamp is 3.
Every turn:
if the fuel-remaining of the lamp is greater than 0:
decrease the fuel-remaining of the lamp by 1;
if the fuel-remaining of the lamp is 0:
say “The lamp flickers and then goes out.”;
now the fuel-remaining of the lamp is -1;
now the lamp is not lit.
 
The fuel can is here. The fuel can can be full or empty. The fuel can is full.
Refilling is an action applying to one thing. Understand “fill [something]” and “refill [something]” as refilling.
Check refilling:
say “[The noun] can’t be refilled.” instead.
 
Instead of refilling the lamp:
if the fuel can is enclosed by the location:
if the fuel can is full:
now the fuel can is empty;
increase the fuel-remaining of the lamp by 50;
say “You drain the fuel can into the lamp[run paragraph on]”;
if the lamp is not lit:
now the lamp is lit;
say “, and quite magically the lamp’s mantle begins glowing brightly again[run paragraph on]”;
say “.”;
otherwise:
say “The fuel can appears to be empty.”;
otherwise:
say “You have nothing to fill the lamp with.”.

Counters

The Library is a room. “Thousands of books line the shelves. An old-fashioned lamp with a green lampshade sits on the table. Mounted on the wall beside the door is a light switch.”

The Library is dark.
Library darkness count is a number that varies. Library darkness count is 0.

The oak table is a supporter in the Library. The old lamp is on the table.

Rule for printing the description of a dark room:
    if the location is the Library and the library darkness count is greater than 2:
        say “At last, — as you grope around in the darkness, your fingers find a switch on the wall.”;
        now the lamp is lit;
    otherwise:
        increase the library darkness count by 1;
        say “It is pitch dark, and you can’t see a thing.  Perhaps if you keep looking around in the dark you might find the light switch”.

 These examples are from Jim Aikin’s manual – http://www.musicwords.net/if/InformHandbook.pdf  If the link isn’t active, you can download it here: InformHandbook