The Inform 6 Programmer's Guide

Chapter 17: The World Model

The standard library models the interactive fiction world as a tree of objects connected by containment relationships. Rooms form the geography, items populate the landscape, and attributes and properties give objects their physical characteristics - light, openability, wearability, and so on. This chapter describes the world model that the library builds on top of the object system.

Attributes and properties appear throughout this Part wherever the system they serve is discussed; §17.2 explains how the library declares them, and Appendix B is the complete catalog of every one.

17.1 The Object Tree

Every object in a program exists in a single global object tree. The tree is rooted at a virtual object 0 (the "root object"), which is never referenced directly. Each object has exactly one parent (or no parent, meaning it sits at the top level) and may have children and siblings.

Four of the compiler's eight tree-navigation system functions (§7.3.1) do most of the work in world-model code:

Function Returns
parent(obj) The object that contains obj, or nothing
child(obj) The first child of obj, or nothing
sibling(obj) The next sibling of obj, or nothing
children(obj) The number of direct children of obj

Two statements modify the tree:

move lantern to kitchen;    ! Make lantern a child of kitchen
remove lantern;             ! Detach lantern (set its parent to nothing)

After remove, the object has no parent and is effectively out of play. It still exists and can be moved back into the tree at any time.

17.2 Attributes, Properties, and the Library

The library's vocabulary for describing objects consists of attributes (single-bit boolean flags) and common properties (value slots present on every object). This section explains how the library declares them; the individual attributes and properties are then discussed throughout this Part, each in the section covering the system it serves, and Appendix B (§B.1.1 and §B.2.1) tabulates every one with its number, default, and flags.

17.2.1 Attributes

Attributes are single-bit flags stored on each object. The language provides two operations for working with them:

  • give - sets or clears an attribute on an object.
  • has / hasnt - tests whether an object has or lacks an attribute.
give lamp light;          ! Set the 'light' attribute
give lamp ~light;         ! Clear the 'light' attribute

if (lamp has light)       ! Test for the attribute
    print "The lamp is lit.";

if (room hasnt visited)   ! Test for absence
    print "You haven't been here before.";

The Z-machine supports up to 48 attributes per object; Glulx has no practical limit. The library defines 31 standard attributes (plus one conditional attribute, infix__watching, which exists only in Infix debugging builds - see §34.2), leaving room for game-specific attributes.

Attributes are declared in linklpa.h with the Attribute directive:

Attribute animate;
Attribute open;
Attribute container;

The library, the parser, and the action routines in verblib.h all inspect these attributes to determine how objects behave. Setting the right combination of attributes is how the game author tells the library what kind of thing an object is - a container, a door, a wearable item, a light source, and so on.

17.2.2 Common Properties

Common properties are declared by the library using the Property directive in linklpa.h. Because they are common properties (as opposed to individual properties defined with with), every object in the game has a slot for each one, initialized to a default value.

Key points about common properties:

  • Each common property has a default value, typically 0, NULL, or a specific constant such as "a" or 100.
  • Some properties are declared additive with Property additive. When a class defines an additive property and a subclass or instance also defines it, the values accumulate rather than replace each other. This is essential for action-handling properties like before and after.
  • A property value can be a constant, a string, an object, a routine (embedded or named), or an array of values. The library interprets each property according to its own conventions.
! From linklpa.h - examples of property declarations:
Property additive before NULL;      ! Additive, default NULL
Property article "a";               ! Non-additive, default "a"
Property capacity 100;              ! Non-additive, default 100
Property short_name 0;              ! Non-additive, default 0

A property whose default is NULL (the VM-specific all-bits-set value) is treated differently from one whose default is 0. The library tests for NULL to determine whether a property has been set at all, while 0 is often used as a valid "no value" or "use the default behavior" sentinel.

17.3 Describing Rooms and Objects

Several properties supply the text the library prints when describing the world. (The machinery that prints names and articles, and that lists objects - short_name, article, invent, list_together, and their relatives - is covered with the rest of the output system in §21.2 and §21.4.)

17.3.1 description

A string or routine printed when the player examines the object (the Examine action). For rooms, description is printed as part of the Look action's room description.

Object  painting "oil painting" gallery
  with  name 'oil' 'painting',
        description [;
            print "A large oil painting of a stormy sea.";
            if (magnifying_glass in player)
                " In the corner you notice a tiny signature.";
            "";
        ];

17.3.2 initial

A string or routine printed in room descriptions when the object is in its initial state - before the player has picked it up (before the moved attribute, §17.14.2, is set). For doors and containers, the library uses when_open/when_closed instead, based on whether the object currently has the open attribute.

Object  letter "sealed letter" desk
  with  name 'sealed' 'letter',
        initial "A sealed letter lies prominently on the desk.",
        description "A letter sealed with red wax.";

17.3.3 when_open and when_closed

Strings or routines printed in room descriptions for containers and doors, depending on whether the object currently has the open attribute.

Object  wardrobe "oak wardrobe" bedroom
  with  name 'oak' 'wardrobe',
        when_open   "An oak wardrobe stands here, its doors flung wide.",
        when_closed "An oak wardrobe stands solidly against the wall.",
        description "A handsome oak wardrobe.",
        capacity 10,
  has   container openable;

These properties take priority over initial for doors and containers.

17.3.4 when_on and when_off

Strings or routines printed in room descriptions for switchable objects, depending on whether the object currently has the on attribute.

Object  radio "transistor radio" lounge
  with  name 'transistor' 'radio',
        when_on  "A transistor radio is playing softly.",
        when_off "A transistor radio sits silent on the shelf.",
        description "A battered transistor radio.",
  has   switchable;

These properties take priority over initial for switchable objects.

17.3.5 describe

A routine called by the library when it wants to include this object in a room description. Additive, default NULL.

If describe returns true, the library considers the object described and does not print any default text. If it returns false, the library falls back to initial, when_open/when_closed, or the standard listing.

Object  cat "tabby cat" garden
  with  name 'tabby' 'cat',
        describe [;
            if (self in kitchen) "A tabby cat sits on the counter.";
            if (self in garden)  "A tabby cat lounges in a sunny patch.";
            rfalse;
        ],
  has   animate;

17.3.6 inside_description

A string or routine printed when the player is inside this object (an enterable container or supporter, §17.7.1).

Object  wardrobe "wardrobe" bedroom
  with  name 'wardrobe',
        inside_description
            "It's dark and cramped, surrounded by coats.",
        capacity 5,
  has   container enterable openable open;

17.4 Rooms and the Map

Rooms are the top-level containers of the world model. A room is simply an object with no parent - it sits at the root level of the object tree. Everything the player can interact with is ultimately contained within a room (or within something contained within a room).

17.4.1 Direction Properties

Direction properties connect rooms. The library defines twelve standard directions:

Property Direction Property Direction
n_to North s_to South
e_to East w_to West
ne_to Northeast nw_to Northwest
se_to Southeast sw_to Southwest
u_to Up d_to Down
in_to In out_to Out

A direction property can hold several types of value:

  • A room object - the player moves to that room.
  • A door object - the library consults the door's door_to property.
  • A routine - called at travel time; if it returns an object, the player moves there; if it prints a message and returns true, travel is blocked; if it returns false or nothing, the default "can't go" message is printed.
  • A string - printed as a refusal message (travel is blocked).
  • 0 (false) - no exit in that direction.
Object  kitchen "Kitchen"
  with  description "A tidy kitchen with a door to the north.",
        n_to  dining_room,
        s_to  "The wall is solid brick.",
        e_to  [;
            if (player in wheelchair) "The doorway is too narrow.";
            return conservatory;
        ],
  has   light;

17.4.2 Customizing "You can't go that way"

When the player tries to go in a direction with no exit, the library prints a default message. The cant_go property on a room overrides this:

Object  cliff_edge "Cliff Edge"
  with  description "A narrow ledge overlooking the sea.",
        cant_go "One wrong step and you'd plummet into the waves.",
        n_to  path,
  has   light;

cant_go may also be a routine, which is useful for providing direction-sensitive responses:

cant_go [;
    if (noun == d_obj) "It's a sheer drop!";
    "Dense undergrowth blocks your way.";
],

17.5 The Player Object

The library defines a default player object called selfobj:

SelfClass selfobj "(self object)";

selfobj is an instance of the internal SelfClass, which provides defaults for short_name (YOURSELF__TX, typically "yourself"), capacity (100), and narrative_voice (2, for second person). During initialization, selfobj.capacity is set to MAX_CARRIED (which also defaults to 100), and the global variable player is set to selfobj. Game code normally refers to player, not selfobj.

17.5.1 Reassigning the Player

The player variable can be reassigned to a different object, typically via ChangePlayer(obj). A game that starts with a custom player object sets it in Initialise:

Object  captain "Captain Marsh"
  with  name 'captain' 'marsh',
        short_name "Captain Marsh",
        description "A weathered sea captain.",
        capacity MAX_CARRIED,
  has   animate proper transparent;

[ Initialise;
    move captain to bridge;
    ChangePlayer(captain);
];

17.5.2 location vs real_location

The library maintains two global variables for the player's position:

  • real_location - always holds the actual room the player is in.
  • location - normally the same as real_location, but set to thedark when the player is in an unlit room.

Game code that needs the true room should use real_location. The location variable is what the library uses for description printing and scope calculations, so in darkness it reflects the thedark pseudo-room.

17.6 Light and Darkness

The library determines whether the player can see by calling OffersLight, which recursively checks whether any object in the player's environment provides illumination. This section covers the world-model concepts; §23.2 documents the light-propagation routines in detail.

17.6.1 The light Attribute

Any object with the light attribute provides illumination. A room with light is always lit. A portable object with light (such as a torch) illuminates whatever containment chain it is part of.

Object  torch "brass torch"
  with  name 'brass' 'torch',
        description "A sturdy brass torch.",
  has   light;

17.6.2 How Light Propagates

OffersLight starts from the player's immediate parent and works outward: an object provides light if it has the light attribute or contains a light source (checked via HasLightSource), and light escapes a container only when it is open or transparent. As a result, a lit torch inside an open box on a table in a dark room illuminates the room, but a lit torch inside a closed opaque chest does not. See §23.2.2 for the full OffersLight algorithm.

17.6.3 The thedark Object

When OffersLight returns false, the library sets location to the special thedark object - a pseudo-room whose name is DARKNESS__TX (default: "Darkness") and whose description is ##Miscellany message 17 (default: "It is pitch dark, and you can't see a thing."). The object itself is defined in §23.3.

17.6.4 AdjustLight

At the end of every turn - and whenever the player moves, via PlayerTo - the library calls AdjustLight to re-evaluate lighting, switching location between real_location and thedark and performing a <Look> or printing a darkness message as appropriate. See §23.2.5.

17.6.5 The DarkToDark Entry Point

When the player moves from one dark room to another dark room, the library calls the optional DarkToDark entry point. This is commonly used to kill the player for stumbling in the dark:

[ DarkToDark;
    deadflag = 1;
    "You stumble in the darkness and fall into a chasm!";
];

If DarkToDark returns false (or is not defined), the library passes control to LibraryExtensions.RunAll(ext_darktodark).

17.7 Containers and Supporters

Two attributes give objects the ability to hold other objects physically:

  • container - other objects can be placed inside (with the Insert action, "put X in Y").
  • supporter - other objects can be placed on top (with the PutOn action, "put X on Y").

An object should not normally have both container and supporter simultaneously; the combination is not meaningful, though the library does not enforce this as a hard prohibition.

17.7.1 Related Attributes

Attribute Effect
open Container is currently open (contents accessible)
openable Container can be opened and closed by the player
locked Container is locked shut
lockable Container can be locked and unlocked
transparent Contents visible even when closed
enterable Player can enter the container or get on the supporter

The library manages open through the Open and Close actions for openable objects (game code can also set it directly); without openable, attempts to open the object are refused. Likewise, a locked object cannot be opened until it is unlocked with the correct key (specified by with_key, §17.8.1), and without lockable, the Lock and Unlock actions are refused. For an enterable object, the player gets inside a container and on a supporter; the inside_description property (§17.3.6) then describes the interior.

17.7.2 The capacity Property

The capacity property limits the number of objects a container or supporter can hold. The default value is 100. For the player object, capacity limits how many objects can be carried (set to MAX_CARRIED, which defaults to 100).

Object  wooden_box "wooden box" kitchen
  with  name 'wooden' 'box',
        description "A sturdy wooden box with a hinged lid.",
        capacity 5,
  has   container openable open;

Object  mantelpiece "mantelpiece" parlor
  with  name 'mantelpiece' 'mantel',
        description "A stone mantelpiece above the fireplace.",
        capacity 3,
  has   supporter scenery;

17.7.3 Scope and Containment

Containment affects scope - the set of objects the parser considers available. Objects inside a closed opaque container are not in scope: the player cannot refer to them. If the container is transparent or open, the contents are in scope.

17.8 Doors

A door connects one room to another. The player interacts with it as a physical object (opening, closing, locking) and travels through it as a map connection.

17.8.1 Door Attributes and Properties

Attribute/Property Purpose
door attribute Marks the object as a door
openable The door can be opened and closed
open The door is currently open
lockable The door can be locked
locked The door is currently locked
with_key The object required to lock/unlock
door_to The destination room (see below)
door_dir The direction property this door occupies
found_in The two rooms the door appears in

The with_key property names the key object the Lock and Unlock actions require the player to be holding. If with_key is 0 (unset; the default when not provided), the Lock and Unlock actions always fail - the library treats a missing with_key as "no key fits", so the player cannot lock or unlock the object at all without a custom before rule. The same applies to lockable containers (§17.7.1).

17.8.2 A Complete Door Example

A two-way door must determine which room the player is going to, based on which room they are coming from:

Object  oak_door "oak door"
  with  name 'oak' 'door',
        description [;
            print "A heavy oak door";
            if (self has open) ". It stands open.";
            if (self has locked) ". It is locked.";
            ". It is closed.";
        ],
        when_open  "An oak door stands open here.",
        when_closed "A heavy oak door bars the way.",
        door_to [;
            if (location == hallway) return study;
            return hallway;
        ],
        door_dir [;
            if (location == hallway) return n_to;
            return s_to;
        ],
        found_in hallway study,
        with_key brass_key,
  has   door openable lockable locked static;

The rooms reference the door in their direction properties:

Object  hallway "Hallway"
  with  description "A long hallway stretching east to west.",
        n_to oak_door,
  has   light;

Object  study "Study"
  with  description "A book-lined study.",
        s_to oak_door,
  has   light;

17.9 Clothing

The player can wear objects with the clothing attribute.

Attribute Meaning
clothing The object can be worn
worn The object is currently being worn

The Wear action puts on a piece of clothing (adding the worn attribute); the Disrobe action removes it. When the player tries to drop, give, put, insert, throw, or otherwise dispose of a worn item, the library performs an implicit Disrobe first (via ImplicitDisrobe) and then continues with the requested action; the player does not have to take the item off explicitly.

Object  raincoat "yellow raincoat" cloakroom
  with  name 'yellow' 'raincoat' 'coat' 'rain',
        description "A bright yellow raincoat.",
  has   clothing;

17.10 Edible and Switchable Objects

17.10.1 Edible Objects

The player can eat an object with the edible attribute. The Eat action removes the object from play:

Object  apple "red apple" kitchen
  with  name 'red' 'apple',
        description "A crisp red apple.",
  has   edible;

After >EAT APPLE, the apple is removed from the object tree. A before rule on the Eat action can customize or prevent this.

17.10.2 Switchable Objects

An object with the switchable attribute has an on/off state controlled by the SwitchOn and SwitchOff actions.

Attribute Meaning
switchable The object can be switched on and off
on The object is currently switched on

The when_on and when_off properties (§17.3.4) provide room-description text for switchable objects:

Object  desk_lamp "desk lamp" study
  with  name 'desk' 'lamp',
        description [;
            print "A green-shaded desk lamp";
            if (self has on) ". It casts a warm glow.";
            ". It is off.";
        ],
        when_on  "A desk lamp casts a warm glow here.",
        when_off "A desk lamp sits dark on the table.",
  has   switchable;

A switchable object can also provide light that depends on its state. Combine switchable with a before rule:

Object  flashlight "flashlight" toolshed
  with  name 'flashlight' 'flash' 'light' 'torch',
        description "A heavy-duty flashlight.",
        before [;
          SwitchOn:  give self light;
          SwitchOff: give self ~light;
        ],
  has   switchable;

17.11 Scenery and Static Objects

Three attributes control whether objects are listed in room descriptions and whether they can be picked up.

17.11.1 scenery

An object with scenery is considered part of the room's scenery. It is:

  • Not listed separately by Look (it should be mentioned in the room's description text instead).
  • Not takeable - attempts to take it print a refusal message.

Scenery objects are still in scope and can be examined or otherwise interacted with; they are not listed automatically.

Object  fountain "marble fountain" courtyard
  with  name 'marble' 'fountain',
        description "Water cascades down tiers of carved marble.",
  has   scenery;

17.11.2 static

An object with static cannot be taken, but is otherwise listed normally by Look. Use static for heavy or fixed objects that should still appear in the room's object list:

Object  anvil "iron anvil" forge
  with  name 'iron' 'anvil',
        description "A massive iron anvil, far too heavy to move.",
  has   static;

17.11.3 concealed

An object with concealed is not listed by Look but can be referred to by name. This is useful for hidden objects that the player might discover through interaction:

Object  hidden_switch "small switch" hallway
  with  name 'small' 'switch' 'hidden',
        description "A small switch, barely visible.",
        before [;
          Push: give oak_door ~locked;
                "Click! Something unlocks nearby.";
        ],
  has   concealed scenery;

17.12 Animate Objects and NPCs

17.12.1 The animate Attribute

The animate attribute marks an object as a living being. Animate objects differ from inanimate ones in several ways:

  • The parser accepts commands directed at it ("guard, drop the sword").
  • Actions like Take are refused ("I don't suppose the guard would care for that.").
  • The life property intercepts actions performed on the NPC.

17.12.2 Gender Attributes

Four attributes specify how the library refers to animate objects:

Attribute Pronoun Example
male he/him a king
female she/her a queen
neuter it a robot
pluralname they the guards

At least one gender attribute should be set on every animate object. Neuter pronouns are the default for inanimate objects, but neuter should be set explicitly on animate objects that are not male or female.

17.12.3 The life Property

The life property handles actions directed at the NPC (such as attacking, kissing, or giving). It receives the triggering action in the reason_code variable - the value a bare case label such as Attack: switches on; §18.4 specifies the full set of actions that trigger it and its return-value contract.

Object  guard "burly guard" gatehouse
  with  name 'burly' 'guard',
        description "A burly guard in chain mail.",
        life [;
          Attack: "The guard parries your blow effortlessly.";
          Kiss:   "The guard recoils in horror.";
          Give:
            if (noun == gold_coin) {
                remove gold_coin;
                "The guard pockets the coin and nods.";
            }
            "~I don't want that,~ the guard growls.";
        ],
  has   animate male;

17.12.4 The orders Property

The orders property handles commands given to the NPC (such as "guard, go north" or "guard, take the sword"). It receives action as the requested action; see §18.4 for the contract and §18.2 for where orders processing sits in the pipeline:

Object  robot "service robot" lab
  with  name 'service' 'robot',
        description "A battered service robot.",
        orders [;
          Go:     "The robot whirs but doesn't move.";
          Take:   move noun to self;
                  print "The robot picks up ", (the) noun, ".^";
                  rtrue;
          Drop:   if (noun notin self) "The robot isn't carrying that.";
                  move noun to parent(self);
                  print "The robot puts down ", (the) noun, ".^";
                  rtrue;
          default: "The robot bleeps uncomprehendingly.";
        ],
  has   animate neuter;

17.12.5 The talkable Attribute

The talkable attribute allows the player to address an object in conversation even if it is not animate. This is useful for objects like telephones, intercoms, or talking statues. An object with talkable can receive the conversation actions Ask, Answer, Tell, AskFor, and AskTo (the actions accepted by CreatureTest) and can have an orders property:

Object  intercom "intercom" airlock
  with  name 'intercom',
        description "A wall-mounted intercom.",
        life [;
          Ask: "A voice crackles: ~I can't help you with that.~";
          Tell: "The voice says: ~Understood.~";
        ],
  has   talkable static;

17.12.6 Conversation Actions

The library provides three actions for NPC conversation:

  • Ask - "ask guard about sword" (noun = guard, second = topic)
  • Tell - "tell guard about dragon" (noun = guard, second = topic)
  • Answer - "say yes to guard" (noun = topic, second = guard)

These are typically handled in the NPC's life property.

17.13 Found-In (Floating Objects)

Some objects should appear in multiple rooms - the sky, background sounds, or a companion who follows the player. The found_in property automatically makes an object "float" into the correct room.

17.13.1 Using found_in

The found_in property takes a list of rooms, a class, or a routine:

! Appear in specific rooms
Object  sky "sky"
  with  name 'sky',
        description "A clear blue sky stretches overhead.",
        found_in  garden courtyard cliff_edge,
  has   scenery;

! Appear in all outdoor rooms (using a routine)
Object  birdsong "birdsong"
  with  name 'birdsong' 'bird' 'song' 'singing',
        description "A pleasant chorus of birdsong.",
        found_in [;
            if (location has light && location provides outdoor
                && location.outdoor == true) rtrue;
            rfalse;
        ],
  has   scenery;

When found_in is a routine, it should return true if the object should be present in the current location, or false otherwise. When found_in is a list, each entry may be a room object or a class (the object floats into any room that is an instance of that class).

17.13.2 How MoveFloatingObjects Works

Each time the player moves, the library calls MoveFloatingObjects. This routine iterates over every object in the game. For each object with a found_in property (that does not have the absent attribute):

  1. If found_in is a routine, call it and check the return value.
  2. If found_in is a list, check whether location matches any entry.
  3. If the object should be present, move it to location.
  4. If not, remove it from the tree.

17.13.3 The absent Attribute

Giving a floating object the absent attribute (aliased as non_floating) suppresses its floating behavior - MoveFloatingObjects removes any object with absent, regardless of found_in:

before [;
  Take:
    give sky absent;
    "You reach up and... no, you really can't take the sky.";
];

17.14 State Tracking and Author-Use Flags

Several attributes track world state automatically or provide free-use flags for the game author; the number property serves the same free-use role for numeric values.

17.14.1 general

The general attribute has no built-in meaning. It is reserved as a free-use boolean flag on any object. Each object has its own independent general flag:

Object  lever "rusty lever" engine_room
  with  name 'rusty' 'lever',
        description [;
            if (self has general) "The lever is pulled down.";
            "The lever is in the upright position.";
        ],
        before [;
          Pull:
            if (self has general) "It's already pulled down.";
            give self general;
            "You pull the lever down with a grinding screech.";
          Push:
            if (self hasnt general) "It's already upright.";
            give self ~general;
            "You push the lever back into its upright position.";
        ],
  has   static;

17.14.2 moved

The moved attribute is set automatically by the library when the player picks up an object. NoteObjectAcquisitions (called at the end of each turn) gives moved to any newly held object. Additionally, when an object with both moved unset and scored set first enters the player's inventory, the library adds OBJECT_SCORE to the total score.

The library uses moved to select description properties: an object without moved is described using its initial property (§17.3.2); one with moved is listed by the standard inventory lister instead:

Object  letter "sealed letter" desk
  with  name 'sealed' 'letter',
        initial "A sealed letter rests on the desk.",
        description "A cream envelope with a wax seal.",
  has   ;

After the player takes and drops the letter, Look lists it as "You can see a sealed letter here" rather than using the initial text.

17.14.3 visited

The visited attribute is set on a room when the player first enters it. ScoreArrival (called from PlayerTo) checks:

if (location hasnt visited) {
    give location visited;
    if (location has scored) {
        score = score + ROOM_SCORE;
        places_score = places_score + ROOM_SCORE;
    }
}

After the first visit, the room is marked visited and subsequent arrivals skip scoring. The Look action also uses visited to decide whether to print a verbose or brief room description.

17.14.4 scored

The scored attribute interacts with the library's automatic scoring system:

  • On a room: when the player first visits a room with scored, the library adds ROOM_SCORE (default 5) to the total score.
  • On an object: when the player first picks up an object with scored, the library adds OBJECT_SCORE (default 4) to the total score.
Object  treasure_room "Treasure Room"
  with  description "Gold glitters from every surface.",
  has   light scored;

Object  diamond "diamond" treasure_room
  with  name 'diamond',
        description "A flawless diamond, catching the light.",
  has   scored;

The score constants can be overridden before including the library:

Constant ROOM_SCORE   10;
Constant OBJECT_SCORE  5;

17.14.5 The number Property

The number property is the numeric counterpart of general: a general-purpose property with no predefined meaning. Default: 0. Game code can use number to store any integer value - a locker number, a score, a counter, or an internal ID. The library does not inspect or modify number itself; it exists purely for the game author's use. For objects needing multiple states, use number or a custom property rather than overloading the single general flag.