Chapter 30: Replacing and Extending the Library
The library provides a comprehensive framework for interactive fiction, but nearly every game needs to customize it. This chapter covers the compiler directives and design patterns that allow a game to replace individual library routines, define new actions and verbs, extend the class hierarchy, intercept library messages, and write reusable library extensions.
30.1 The Replace Directive
The Replace directive tells the compiler that a routine defined later in the
source should take the place of a routine with the same name that would
otherwise be defined in an included system file. It has two forms:
Replace routine_name;
Replace routine_name preserved_name;
30.1.1 Basic Replacement
The first form marks the symbol routine_name with the internal flag
REPLACE_SFLAG (value 2 in the compiler's symbol table). When the compiler
later encounters a routine definition for that name inside a system file (a
file that begins with the System_file directive), it skips the entire
definition - reading and discarding tokens until it reaches the closing ] -
and uses the game's own definition instead.
The key mechanism works as follows:
- The
Replacedirective setssymbols[token_value].flags |= REPLACE_SFLAGon the named symbol. - When the compiler begins parsing a routine definition, it checks whether the
symbol has
REPLACE_SFLAGset and the current file is a system file (viais_systemfile()). - If both conditions are true and there is no rename mapping, the compiler sets
dont_enter_into_symbol_table = TRUEand consumes tokens untilEOF_TTor the closing], effectively discarding the system file's version. - The game's own definition of the same routine - which appears outside any system file - is compiled normally.
The Replace directive must appear before the routine it replaces is
defined. In practice this means it must appear before the relevant Include
directive that pulls in the system file. Placing Replace after the routine has
already been compiled is a compile-time error.
30.1.2 Replacement with Preservation
The two-argument form preserves the original routine under a new name:
Replace Banner OriginalBanner;
This tells the compiler to do everything the one-argument form does, but
additionally to compile the system file's definition under the name
preserved_name rather than discarding it. The replacement mapping is stored in
an array of value_pair_t structures:
typedef struct value_pair_struct {
int original_symbol;
int renamed_symbol;
} value_pair_t;
When the compiler encounters the routine definition in the system file, it finds
the rename mapping via find_symbol_replacement(). It compiles the routine body
under the renamed symbol instead of skipping it entirely. The game can then call
preserved_name() to invoke the original behavior.
30.1.3 Constraints
Several constraints apply to the Replace directive:
- Ordering:
Replacemust appear before the routine is defined. The compiler does not replace routines retroactively. - No duplicate rename targets: With the two-argument form
Replace X Y, giving the same original routine two different preserved names is an error: "A routine cannot be 'Replace'd to more than one new name" (symbols.c:1051). The single-argument formReplace Xcarries no such restriction - the same routine may be declared with a plainReplacemore than once without error. - No self-replacement: The preserved name cannot be the same as the original name.
- System files only: The skip-and-discard mechanism only activates inside system files. Replacing a routine defined in the game's own source has no effect through this mechanism - the compiler will report a duplicate definition.
30.1.4 Example: Replacing Banner
A common use of Replace is to provide a custom banner:
Replace Banner;
Include "Parser";
Include "VerbLib";
[ Banner;
print "The Mysterious Castle^";
print "An interactive fiction by J. Author^";
print "Release 1 / Serial number 260115^";
new_line;
];
Include "Grammar";
The library's Banner routine (included via VerbLib) is skipped because
Replace Banner was issued before the Include. The game's Banner routine is
compiled instead.
30.2 Replacing Library Routines
Building on the mechanics described in §30.1, this section covers practical patterns for replacing library routines.
30.2.1 The Standard Template
The standard source file structure places Replace directives at the very top,
before any library includes:
Constant Story "My Game";
Constant Headline "^An Interactive Example^";
Replace Banner;
Replace DrawStatusLine;
Include "Parser";
Include "VerbLib";
! ... game objects and routines ...
[ Banner;
print (string) Story;
print (string) Headline;
print "Release 1^";
];
[ DrawStatusLine width x;
@set_cursor 1 1;
@split_window 1;
@set_window 1;
@set_cursor 1 1;
style reverse;
width = 0->33;
spaces (width - 1);
@set_cursor 1 2;
print (name) location;
x = width - 14;
@set_cursor 1 x;
print "Score: ", score;
style roman;
@set_window 0;
];
Include "Grammar";
30.2.2 Using the Two-Argument Form
The two-argument form of Replace is especially useful when you want to augment
the original behavior rather than completely replacing it:
Replace Banner OriginalBanner;
Include "Parser";
Include "VerbLib";
[ Banner;
OriginalBanner();
new_line;
print "Visit our website at example.com^";
];
Include "Grammar";
Here OriginalBanner is the library's own Banner routine, compiled under a
different name. The game's Banner calls it first, then appends additional
output. This pattern avoids duplication and keeps the replacement compatible
with future library updates.
30.2.3 The System_file Directive
The System_file directive, placed at the top of a source file, marks that file
as a system file. Only routines defined inside system files can be silently
replaced by the Replace mechanism. The standard library files (Parser.h,
VerbLib.h, Grammar.h, and the files they include) all begin with
System_file.
If you write your own library extension and want its routines to be replaceable
by game authors, place System_file at the top of your extension file (see
§30.6).
30.2.4 Commonly Replaced Routines
Games frequently replace the following library routines:
| Routine | Defined In | Purpose |
|---|---|---|
Banner |
verblib.h |
Prints the game banner at startup |
DrawStatusLine |
parser.h |
Draws the status line (Z-machine) |
PrintRank |
#Ifndef default in grammar.h |
Prints the player's rank based on score |
BeforeParsing |
Stub in grammar.h |
Hook called before each input is parsed |
Initialise |
Game-defined (not in library) | Game setup (typically defined by the game) |
Note that routines declared via Stub in grammar.h (see §30.3) are intended
to be defined by the game directly rather than replaced - since the Stub only
creates a default if the game does not provide one.
30.3 The Stub Directive
The Stub directive creates a minimal do-nothing routine if - and only if - no
routine of that name has already been defined. Its syntax is:
Stub routine_name N;
Here, N is an integer ranging from 0 to 4 that indicates how many local variables the stub routine needs to declare.
30.3.1 Compiler Mechanics
When the compiler processes a Stub directive:
- It checks whether
routine_namealready exists in the symbol table as a defined routine. - If the routine already exists, the
Stubis silently ignored - the existing definition takes precedence. - If the routine does not exist, the compiler:
- Sets
STUB_SFLAG(value 16) on the symbol. - Generates a trivial routine body with
Ndummy local variables nameddummy1throughdummy4(as needed). - Emits a
rfalseinstruction (Z-machine) or areturn 0instruction (Glulx), so the stub always returnsfalse(0). - Marks the dummy variables as used to suppress "unused variable" compiler warnings.
- Sets
The generated code is equivalent to:
! For Stub SomeRoutine 2;
[ SomeRoutine dummy1 dummy2;
rfalse;
];
30.3.2 Purpose in the Library
The primary use of Stub is in the library's grammar.h file, where it
declares default implementations for all the entry point routines that a
game may optionally define. Because grammar.h is included last (after the
game's own source), any routine the game defines will already exist in the
symbol table, causing the corresponding Stub to be skipped. If the game does
not define a particular entry point, the Stub provides a safe default that
returns false.
The complete list of stubs from grammar.h:
Stub AfterLife 0;
Stub AfterPrompt 0;
Stub Amusing 0;
Stub BeforeParsing 0;
Stub ChooseObjects 2;
Stub DarkToDark 0;
Stub DeathMessage 0;
Stub Epilogue 0;
Stub GamePostRoutine 0;
Stub GamePreRoutine 0;
Stub InScope 1;
Stub LookRoutine 0;
Stub NewRoom 0;
Stub ObjectDoesNotFit 2;
Stub ParseNumber 2;
Stub ParserError 1;
Stub PrintTaskName 1;
Stub PrintVerb 1;
Stub TimePasses 0;
Stub UnknownVerb 1;
Stub AfterSave 1;
Stub AfterRestore 1;
[Glulx] Three additional stubs are defined when compiling for Glulx:
TARGET_GLULX;
Stub HandleGlkEvent 2;
Stub IdentifyGlkObject 4;
Stub InitGlkWindow 1;
;
These Glulx-only entry points handle Glk event processing, object identification during save/restore, and window initialization respectively.
30.3.3 Entry Point Summary
Each stubbed entry point is called at a specific moment during gameplay. The following table summarises their purpose and the number of arguments each receives:
| Entry Point | Args | Called When |
|---|---|---|
AfterLife |
0 | After the player dies, before "restart/restore" prompt |
AfterPrompt |
0 | After the command prompt is printed |
Amusing |
0 | When player wins and types AMUSING |
BeforeParsing |
0 | Before each command is parsed |
ChooseObjects |
2 | During disambiguation to rank objects |
DarkToDark |
0 | When the player moves from one dark room to another |
DeathMessage |
0 | To print a custom death message |
Epilogue |
0 | After winning or dying, before "restart/restore" prompt |
GamePostRoutine |
0 | After each action, if not already handled |
GamePreRoutine |
0 | Before each action is processed |
InScope |
1 | To add objects to scope for a given actor |
LookRoutine |
0 | After each room description is printed |
NewRoom |
0 | When the player enters a new room |
ObjectDoesNotFit |
2 | When an object is too large for a container |
ParseNumber |
2 | To handle numeric input during parsing |
ParserError |
1 | To intercept or customize parser error messages |
PrintTaskName |
1 | To print the name of a scored task |
PrintVerb |
1 | To print a verb the library does not recognize |
TimePasses |
0 | At the end of each turn |
UnknownVerb |
1 | To handle a verb the parser does not recognize |
AfterSave |
1 | After a save operation (argument: success flag) |
AfterRestore |
1 | After a restore operation (argument: success flag) |
30.4 Adding New Actions and Verbs
The Verb, Extend, and Fake_Action directives allow a game to define new
commands and actions beyond those provided by the standard library. For related
discussion of how the parser processes grammar at runtime, see §19.
30.4.1 The Verb Directive
The Verb directive defines a new verb and its associated grammar. Its general
syntax is:
Verb 'word1' 'word2' ...
* token token ... -> ActionName
* token token ... -> ActionName
...;
Each quoted word is a dictionary entry that the player can type to invoke the
verb. The grammar lines (beginning with *) describe the patterns of input that
the parser will accept, and the -> arrow specifies which action to generate
when a pattern matches.
A complete example:
[ DanceSub;
"You dance a little jig.";
];
Verb 'dance' 'jig' 'boogie'
* -> Dance;
This defines a verb that the player can invoke by typing DANCE, JIG, or
BOOGIE. The grammar line * (with no tokens) matches the verb word alone with
no further input. When it matches, the parser generates the Dance action,
which the DanceSub routine handles.
The action routine must be named with the suffix Sub appended to the action
name. You can refer to the action value in code as ##Dance.
30.4.2 The meta Keyword
A verb can be declared as meta by placing the meta keyword before the
first dictionary word:
Verb meta 'version'
* -> Version;
Meta actions are "out-of-world" actions that do not consume a game turn. They
are typically used for commands like SAVE, RESTORE, SCORE, and QUIT. A
meta action does not trigger GamePreRoutine, GamePostRoutine, TimePasses,
or the each_turn daemon cycle.
30.4.3 Grammar Tokens
Each grammar line consists of zero or more tokens that describe what the parser should expect after the verb word. The most commonly used tokens are:
| Token | Meaning |
|---|---|
noun |
Any object in scope |
held |
An object held by the player |
multi |
One or more objects in scope |
multiheld |
One or more held objects |
multiexcept |
Multiple objects except the second noun |
multiinside |
Multiple objects inside the second noun |
creature |
An animate object in scope |
topic |
Any sequence of words (for ASK/TELL) |
number |
A numeric value |
scope=Routine |
Objects returned by a custom scope routine |
noun=Routine |
Objects filtered by a custom routine |
'word' |
A literal dictionary word |
attribute |
An object attribute (for debugging verbs) |
Tokens can be combined to form complex grammar patterns:
Verb 'give' 'pay' 'offer' 'feed'
* held 'to' creature -> Give
* creature held -> Give reverse
* 'up' -> Quit;
The reverse keyword after the action name swaps the values of noun and
second when the action is generated, allowing alternative word orders without
separate action routines.
30.4.4 The Extend Directive
The Extend directive adds grammar lines to an existing verb without replacing
its entire grammar. It has several modes:
Default (append last):
Extend 'take'
* 'a' 'bath' -> Bathe
* 'a' 'nap' -> Sleep;
New grammar lines are appended after the verb's existing lines. Since the parser tries lines in order, existing patterns take priority.
Explicit modes:
Extend 'look' first
* 'through' noun -> Search;
The first keyword inserts the new lines before the existing grammar, so they
are tried first during parsing.
Extend 'look' last
* 'behind' noun -> LookBehind;
The last keyword is the default - new lines are appended after existing
grammar.
Extend 'get' replace
* multi -> Take
* 'out'/'off' -> Exit
* multiinside 'from' noun -> Remove;
The replace keyword discards all existing grammar for the verb and
substitutes the new lines. Use this with care, as it removes all standard
behavior for that verb word.
The only keyword:
Extend only 'give' replace
* held 'to' creature -> Give
* creature held -> Give reverse;
The only keyword detaches a single dictionary word from its verb group before
extending it. In the standard library, 'give', 'feed', 'offer', and
'pay' share a single verb entry; Extend only 'give' separates 'give' so it
can have its own independent grammar while 'feed', 'offer', and 'pay'
retain the original.
The / character between quoted words (e.g., 'out'/'off') means "either
word" - the parser accepts either OUT or OFF in that position.
30.4.5 The Fake_Action Directive
The Fake_Action directive creates an action constant without any associated
grammar:
Fake_Action Sneeze;
This allocates an action number for Sneeze (accessible as ##Sneeze) but does
not create a verb or grammar for it. Fake actions are used for internal
signaling between objects - for example, a before handler might respond to
##Sneeze even though the player can never type a command that generates it
directly. Game code can trigger it with:
<Sneeze>;
or the longer form:
action = ;
noun = 0; second = 0;
ActionPrimitive();
The library also uses fake actions for internal actions like ##Receive,
##ThrownAt, ##Order, and others that the parser never generates directly
(see §18.6).
30.4.6 VM Limits on Verbs and Actions
It is important to distinguish between verbs (dictionary words that drive
grammar, like "take" or "drop") and actions (the resulting subroutine
triggered by the parser, like ##Take).
[Z-machine] The Z-machine format limits the game to 255 verb entries. Each
Verb directive or Extend only operation that creates a new verb entry counts
against this limit. (This is because the Z-machine's dictionary format relies on
a single byte to track verb references.) In practice, the standard library uses
approximately 95 verb entries, leaving ample room for game-defined verbs.
However, the limit on actions is much higher. Under Grammar Version 2 (GV2) and Version 3 (GV3), the Z-machine supports up to 1024 distinct actions. (Under the older Grammar Version 1, actions are limited to 256).
[Glulx] Although the VM itself has no architectural limitation on the number
of verbs, the Inform 6 compiler enforces a fixed maximum of 65,535 verbs for
Glulx programs. In practice, this is effectively unlimited. Under GV2 (the only
grammar version used on Glulx), actions are capped at 4096 - higher than the
Z-machine's tighter 1024-action limit for GV2/GV3 builds (verbs.c:604 enforces
the 4096 ceiling; the Z-machine's 1024 cap is enforced separately at
verbs.c:611, because Z grammar tokens store the action number in a 10-bit
field).
30.5 Extending Classes
The language supports single and multiple inheritance through its class system. Games can define new classes, inherit from library classes, and combine behaviors from multiple parent classes.
30.5.1 Defining Classes
A class is defined with the Class directive:
Class Treasure
with value 0,
description "It looks valuable.",
has scored;
Classes serve as templates: any object (or further class) that inherits from
Treasure will receive the value property, the default description, and the
scored attribute.
Objects inherit from a class by naming it in a class segment:
Object -> ruby_ring "ruby ring"
class Treasure
with name 'ruby' 'ring',
value 50,
description "A ring set with a large ruby.";
The ruby_ring inherits the scored attribute from Treasure. It overrides
the class defaults with its own value and description.
30.5.2 Multiple Inheritance
An object or class can inherit from more than one class:
Class StorageBox
has container open,
with capacity 10;
Class Securable
has lockable,
with with_key 0;
Object -> strongbox "strongbox"
class StorageBox Securable
with description "A heavy iron strongbox.",
with_key brass_key;
When you list multiple classes, inheritance proceeds left to right. For non-additive properties, if two classes define the same property, the value from the earlier (leftmost) class takes precedence. Once a property is set, a later class's definition of it is skipped. For additive properties (see §7.4.5), values from all classes and the object's own definition are accumulated.
Attributes are inherited from all listed classes via a union - if any class provides an attribute, the inheriting object receives it.
The compiler tracks class information through an internal classinfo structure:
static int *classes_to_inherit_from;
classinfo *class_info;
Each class entry records the class's prototype object, its property block offset, and its symbol table index. During object construction, the compiler walks the class list, copying properties and attributes in order.
30.5.3 Constraints on Inheritance
- A class cannot inherit from itself. The compiler checks for this and reports an error: "A class cannot inherit from itself".
- Circular inheritance (A inherits from B, B inherits from A) is prevented by the requirement that a class must be defined before it can be used as a parent.
- The number of classes is limited by a fixed compiler constant in Inform 6.44
(
VENEER_CONSTRAINT_ON_CLASSES_Zcaps it at 256 for Z-Machine,VENEER_CONSTRAINT_ON_CLASSES_Gcaps it at 32,768 for Glulx).
30.5.4 Runtime Class Testing
At runtime, two operators test class membership:
ofclass tests whether an object is a member of a given class:
if (obj ofclass StorageBox) print "It's a container.^";
This returns true if obj inherits from StorageBox, whether directly or
through a chain of intermediate classes.
metaclass returns the metaclass of a value:
x = metaclass(obj);
The possible return values are:
| Return Value | Meaning |
|---|---|
Object |
A regular object |
Class |
A class object |
Routine |
A routine |
String |
A string |
nothing (0) |
Not a valid object/routine/string |
Rather than mapping to a single VM instruction, these operators compile, on both
the Z-machine and Glulx, into a call to a small veneer routine that the compiler
links in automatically: OC__Cl for ofclass and Meta__class for metaclass
(see Appendix F).
30.5.5 Overriding Inherited Properties
When an object provides its own value for an inherited property, the object's value completely replaces the class's value (for non-additive properties). For routine-valued properties, this means the object's routine is called instead of the class's routine:
Class Furniture
with description [; "A piece of furniture."; ],
before [;
Take: "It's fixed in place.";
],
has static scenery;
Object -> chair "wooden chair"
class Furniture
with description "A rickety wooden chair.",
name 'wooden' 'chair';
The chair inherits before and the attributes static and scenery from
Furniture, but its own description (a string) overrides the class's
description (a routine).
30.6 Library Contributions and Extensions
A library extension is a reusable source file that provides additional functionality to games that include it. Well-written extensions follow several conventions.
30.6.1 The System_file Directive
An extension that wants its routines to be replaceable by game authors should
place the System_file directive at the top of the file:
System_file;
This marks all routine definitions in the file as belonging to a system file.
Game authors can then use Replace to override individual routines, exactly as
they would with standard library routines (see §30.1).
30.6.2 Include Guard Pattern
To prevent double-inclusion (which would cause duplicate symbol errors), extensions should wrap their contents in a conditional compilation guard:
MY_EXTENSION;
Constant MY_EXTENSION;
System_file;
! ... extension code ...
; ! MY_EXTENSION
If the file is included a second time, the constant MY_EXTENSION already
exists and the body is skipped.
30.6.3 Include Paths
How the Include directive resolves a filename depends on whether any search
directories have been set with the +include_path compiler switch:
- If no
+include_pathdirectory is given, the file is looked for in the current directory only. - If one or more directories are given with
+include_path, they are searched in the order listed on the command line. The current directory is not searched as a fallback unless it is named explicitly: an empty entry in the list (for example, a trailing comma) stands for the current directory and is searched in its listed position.
Multiple include paths can be specified:
inform6 +include_path=./lib,./extensions MyGame.inf
Within source code, Include takes a string filename:
Include "MyExtension";
The compiler appends the appropriate file extension (.h or .inf) depending
on the platform and settings.
30.6.4 Version Checking
Extensions can check which version of the library is present using predefined constants:
| Constant | Type | Example Value | Meaning |
|---|---|---|---|
LIBRARY_VERSION |
Numeric | 612 | Library version number (6.12 = 612) |
LibRelease |
String | "6.12.8" |
Human-readable version string |
LibSerial |
String | "251226" |
Serial number (date-based: YYMMDD) |
LIBRARY_VERSION;
LIBRARY_VERSION >= 612;
! Code that requires library 6.12 or later
;
;
The outer #Ifdef check ensures the code compiles even outside the library
context. The #Iftrue directive performs a numeric comparison.
30.6.5 Extension Best Practices
- Document dependencies: state which library version is required.
- Namespace your symbols: use a prefix derived from the extension name for constants, globals, and routines to avoid collisions with other extensions or the game itself.
- Use
System_file: so game authors can replace individual routines. - Use include guards: to prevent double-inclusion (see §30.6.2).
- Avoid modifying globals at load time: defer initialization to a routine
that the game can call from
Initialise. - Provide
Replace-friendly routines: keep routines small and focused so they can be individually replaced without copying large amounts of code.
30.6.6 Extension Structure Example
A typical extension file:
! myextension.h - Provides the Gizmo class.
! Requires library 6.12 or later.
MYEXTENSION_H;
Constant MYEXTENSION_H;
System_file;
LIBRARY_VERSION;
LIBRARY_VERSION < 612;
Message error "myextension.h requires Inform library 6.12 or later.";
;
;
Class Gizmo
with name 'gizmo',
description "A mysterious gizmo.",
activate [;
give self on;
"You activate ", (the) self, ".";
],
deactivate [;
give self ~on;
"You deactivate ", (the) self, ".";
],
has switchable;
[ GizmoActivateSub;
if (noun ofclass Gizmo)
noun.activate();
else
"That's not a gizmo.";
];
[ GizmoDeactivateSub;
if (noun ofclass Gizmo)
noun.deactivate();
else
"That's not a gizmo.";
];
Verb 'activate'
* noun -> GizmoActivate;
Verb 'deactivate'
* noun -> GizmoDeactivate;
; ! MYEXTENSION_H
The game includes it with:
Include "Parser";
Include "VerbLib";
Include "myextension";
! ... game code ...
Include "Grammar";
30.7 Customizing LibraryMessages
The standard library generates many messages during gameplay - responses to
actions, parser errors, status reports, and so on. The LibraryMessages
mechanism provides a clean way to customize these messages without replacing
entire library routines.
30.7.1 The LibraryMessages Object
A game can define an object named LibraryMessages to intercept library
messages before they reach the default message handler:
Object LibraryMessages
with before [;
Take:
if (lm_n == 1)
print_ret "You grab ", (the) noun, ".";
Drop:
if (lm_n == 3)
print_ret "You toss ", (the) noun, " aside.";
Look:
if (lm_n == 5 or 6)
print_ret ""; ! suppress "You can see..." preamble
];
The LibraryMessages object must be defined in the game source (not inside a
class or contained within another object). It has no parent, no name, and no
attributes - only the before property matters.
30.7.2 Dispatch Mechanism
When the library needs to print a message, the following sequence occurs:
- The library sets the action variable to the relevant action (e.g.,
##Take) and the globallm_nto a sub-message number. - If the game has defined a
LibraryMessagesobject, the library callsLibraryMessages.before(). - If
beforereturnstrue(i.e., it printed a message and usedprint_ret,rtrue, or returned a non-zero value), the library considers the message handled and skips its default. - If
beforereturnsfalse(the entry point was not intercepted, or there is noLibraryMessagesobject), the library callsLanguageLM(n, x1, x2)inenglish.h, which prints the default English-language message.
30.7.3 The lm_n Variable
The global variable lm_n (declared in parser.h) identifies which specific
message within an action is being printed. Each action can have multiple
messages corresponding to different situations. For example, the Take action
defines several lm_n values:
lm_n |
Situation |
|---|---|
| 1 | Successfully taken ("Taken.") |
| 2 | Taking yourself ("always self-possessed") |
| 3 | Taking an unsuitable object ("I don't suppose ... would care for that") |
| 4 | Must get off/out of the object first |
| 5 | Already has the object ("already have it") |
| 6 | Object seems to belong to someone |
| 7 | Object is part of another object |
| 8 | Object is not available |
| 9 | Object (a container) is not open |
| 10 | Object is hardly portable |
| 11 | Object is fixed in place |
| 12 | Carrying too many things already |
| 13 | Putting an object away to make room |
Not every action uses all 13 or more sub-messages; some actions have only one or
two. The exact set of lm_n values for each action is defined by the
corresponding case in LanguageLM in english.h.
30.7.4 Additional Context Variables
The LanguageLM function receives two additional parameters, x1 and x2,
that carry context-specific information. The library also uses the companion
globals lm_o and lm_s (both declared in parser.h alongside lm_n) to pass
additional context in some messages. The exact meaning of these parameters
varies by action and lm_n value; consult the LanguageLM source in
english.h for the specific action.
For example, in some messages x1 may be the object that caused the action to
fail, while in others it may be an item count.
30.7.5 LanguageLM and Internationalization
The default message handler, LanguageLM, is defined in english.h and
contains a large switch statement covering every action that the library can
generate. Each case handles the various lm_n sub-messages for that action:
[ LanguageLM n x1 x2;
Answer, Ask:
print_ret "There is no reply.";
Attack:
print_ret "Violence isn't the answer to this one.";
! ... many more cases ...
];
The relationship between LibraryMessages and LanguageLM is straightforward:
LibraryMessagesprovides game-level customization. A game author uses it to adjust specific messages for their particular game.LanguageLMprovides language-level defaults. A translator replacingenglish.hwith (for example)french.hwould rewriteLanguageLMto produce French-language messages.
30.7.6 Comprehensive Example
The following example customizes messages for several common actions:
Object LibraryMessages
with before [;
Take:
if (lm_n == 1)
print_ret "Taken. (", (the) noun, ")";
if (lm_n == 12) {
print "Your hands are full";
if (noun has pluralname)
print " and you can't carry them all.^";
else
print " and you can't carry it too.^";
rtrue;
}
Drop:
if (lm_n == 3)
print_ret (The) noun, " tumbles to the ground.";
Examine:
if (lm_n == 2) {
print "You see nothing special about ";
print (the) noun, ".^";
rtrue;
}
Search:
if (lm_n == 4)
print_ret "You find nothing of interest.";
Eat:
if (lm_n == 2)
print_ret "You devour ", (the) noun, ". Not bad.";
Inv:
if (lm_n == 1)
print_ret "You are empty-handed.";
if (lm_n == 2)
print "You are carrying";
];
This object intercepts specific messages while allowing all unhandled messages
to fall through to the library defaults. The game author only needs to handle
the lm_n values they want to change.
30.7.7 Replacing vs. LibraryMessages
There are two approaches to customizing library output:
| Approach | When to Use |
|---|---|
LibraryMessages |
Changing the text of specific messages while keeping the library's action-processing logic intact |
Replace |
Changing the logic of how an action is processed, not just its textual output |
In general, prefer LibraryMessages for text changes. It is less invasive, does
not require understanding the internal structure of library routines, and is
more resilient to library version changes. Use Replace when you need to alter
control flow, add new conditions, or completely restructure how a routine works.