ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


Natural Language Game Programming with Inform 7

by Liza Daly
06/08/2006

Part 1 - Introducing Inform 7

Programming games is an activity. A programming language is a kind
of thing.  Inform 7 is a kind of programming language. It is either the
single-most important advance in interactive fiction in a decade, or an
interesting idea doomed to fail.

If you can read that, you can read Inform 7, Graham Nelson's latest version of the most popular programming language for writing interactive fiction (IF), or "text adventures." The preceding paragraph is Inform 7 source code: contrived for this article, yes, but you can compile and run it. If you are a COBOL fan, you will love this language, and if you are not, the main thing you should know is that Inform 7 does not suck. It can do real work in a novel way.

In Choosing a Language for Interactive Fiction, I compared the two most widely used interactive fiction development languages and concluded that real programmers will naturally want to use TADS 3, because it contains all of the features that are so obviously critical to writing IF:

  1. Heavy use of object-oriented programming
  2. An extremely comprehensive standard library that models the real world
  3. A familiar C-like syntax

By comparison, Inform 7 has:

  1. Rule-based semantics in place of classes, objects, and methods
  2. A very lightweight standard library, stripped down even relative to Inform 6
  3. English sentences

OK, so Graham Nelson isn't an ONLamp reader. Interactive fiction languages have tended toward greater complexity and a closer resemblance to traditional programming. What kind of a system do you get when you throw all that away and start from scratch?

Part 2 - Interactive Fiction Games Are A Kind of Book

The first principle of Inform 7 is that interactive fiction is a literary pursuit; IF works are more like short novels than games. In this white paper (PDF), Nelson writes that "the natural language in which to write interactive fiction is natural language," and he believes this statement should apply not only to the language, but to the entire development cycle. As the Macintosh computer did for the desktop metaphor, Inform 7 extends interactive fiction as writing through every aspect of the user experience. This includes not only its unique natural language syntax, but also the integrated development environment, source code organization, and library management.

Inform 7's IDE (currently available for Mac OS X and Windows) presents the developer with two panels positioned like facing pages of a book (Figure 1). There are no references to filenames, header files, or anything platform-specific. Include libraries (called extensions) by naming the library and its author; author name is a requirement because authorship is important in literature. In the default mode, you edit source code in the left pane and all IDE responses appear on the right. Tabs provide access to built-in documentation, the compiler output window, an Inform interpreter, and numerous debugging tools.

the Inform 7 IDE
Figure 1. The Inform 7 IDE

Some of these debugging tools merit special mention. The IDE can track commands issued in a particular game session and replay them after a recompile. The Skein (Figure 2) provides a visual record of these commands and tracks threads in the story. Because the Skein can get quite large, the programmer can prune the tree to include only the main plotline and relevant branches, but then he can click on any node to jump into the game at that point. It's a unique hybrid of debugger and unit testing suite. Outside of the Skein, you can construct simple unit tests by bundling several commands into a single test. In the spirit of Perl's POD, you can even embed inline documentation and testable example code directly into the source.

Inform 7's Skein
Figure 2. The Skein

The IDE also generates a map for each game. The map isn't as aesthetically pleasing as a hand-authored one, and the system can run into trouble with topologically challenging games, but the feature remains quite useful. With natural language source code, it's easy to get lost in pronouns and incorrectly relate objects. The map provides instant feedback in a case where you may have intended the restrooms to be northeast of the Second Floor Hall but instead attached them to a conference room (Figure 3).

A map from the Inform 7 IDE
Figure 3. A map from the Inform 7 IDE

When errors in the source occur, the IDE provides hyperlinks to the offending code rather than line numbers. Error messaging is in full English and attempts to guess what the author was trying to do, although after some repetition the verbose messages can get a little maddening. Error responses in the current version were derived from beta-testing; as Inform 7 matures, the messaging will likely become more nuanced.

Traditional books get published after being written. Inform 7 likewise provides a method for publishing games, tailored to the unique needs of online release. Authors are encouraged to include metadata complete enough to generate a library card to faciliate archiving and searching. You can also designate portions of code, such as debugging routines, as "not for release," and the publisher will exclude them. Inform 7 exports to a standard IF format called Blorb, which can include images for cover art as well as links to external resources such as websites and audio files. If you choose not to create your own website for the game, you can generate one automatically out of a standard template. Inform 7 can bundle all of these resources together--plus generated walkthroughs, game source, and even a customized, annotated version of the games map (in EPS format). These features are built directly into the language.

Part 3 - ...Aimed to Describe A Radically Humanizing Interface for the Writing of Interactive Fiction

Inform 7 is an experiment in strong literate programming (LP). LP with traditional programming languages gives priority to comments and other human-readable text over source code. Nelson suggests that this doesn't go far enough, and Inform 7 closes the distance between source and comments enough to almost obviate the need for comments at all. [If you need them, put them in brackets, just like ordinary editorial sidenotes.]

Authors are encouraged to organize their code into sections, parts, or chapters--whichever makes most sense in the context of their work. Only the IDE uses these sections; they do not appear in the game. Syntatically, they're identical to the headings in this article: the word "Part," a number, a dash, and some descriptive text. You probably didn't notice them much here, which is the point of their inclusion in Inform 7--whenever possible, use existing human conventions that feel natural. There is no other way to divide the source, and especially no way to chunk the game artificially into multiple files; again, there is no concept of files at all.

A common criticism of human-centric languages such as COBOL or SQL is that they can become overly verbose. This is usually true when using natural language to express concepts that have their own specialized, compact syntax already (for example, mathematics, resulting in COBOL's ADD 1 TO INPUT GIVING ANSWER). IF communicates with the player in a human language, and there are huge benefits to tightly coupling the output (English) with the input (English-like source code):

Instead of digging at the spot when the player does not carry the
shovel: say "What, without your shovel? That won't work too well."

This code expresses the condition in plain English, and the response to the player reflects that condition clearly (if a bit testily). The intent of the code is perfectly obvious even at a cursory glance, reducing the likelihood of bugs. A statement such as does not carry also eliminates many common IF-specific errors. In other languages, authors often implement this check as "if the parent of the shovel is the player," which works fine until the player puts the shovel into the backpack. She is still carrying the shovel as a human would understand it, but the game treats possessions no differently from an abstract tree of objects, and the author needs to write code with this in mind.

This is where Inform 7 gets its biggest wins, because these are cases where natural language is more, not less, concise. For example, while there's a construct for loops, natural language inherently minimizes many common types of looping (such as Inform 6's ubiquitous objectloop):

After dropping the priceless fragile vase: 

  say "The vase shatters into a zillion worthless pieces. Scattered amidst the
  shards are [is/are list of things in the vase]." 

  [Text in brackets within quoted strings are not comments but code. This,
  however, would be a comment.]

Note also that there are no procedure names; there is no reason to name a function when you can instead describe the conditions under which to call it.

IF source usually has plenty of tedious counter variables and flags used to track the state of the game world. It's a natural concept to a programmer but a foreign one to an author, so Inform 7 rarely requires them. Instead, it knows how to count:

Instead of examining the player for the third time: say "Look, that
pimple isn't going away just by staring at it."

There is no temporary variable, if-statement, or incrementing necessary. This is part of the language because asking about the historical state of the game world is extremely common in IF.

The other obvious win is that Inform 7 is supremely easy to read, even if it's the first time you've ever seen it. This isn't as handy as it might seem initially, because most IF authors write solo and over a reasonably short period of time, so there's less opportunity to revisit old code (having forgotten how it works). One downside of that readability is it may lull you into thinking that you understand some block of code, but then you may be unable to reproduce it accurately. Inform is more finicky about English than you are, and paraphrasing is often a syntax error. For now, another downside is that only one natural language is available. No doubt someone hackish enough will port the source and an equivalent grammar to another language, but it will be a non-trivial enterprise.

Part 4 - Yes, But Does It Work?

Inform 7 has already seen a great deal of use prior to its release. Two highly respected IF authors, Emily Short and Andrew Plotkin, have done extensive work on the language. Five games with source are part of the initial release, serving both as proofs of concept and as documentation for those who learn best by example. There's no doubt that by the time this year's Interactive Fiction Competition rolls around, there will be many more Inform 7 releases in play.

That said, there's still some awkwardness in the language implementations. Even the example source does not read as fluently as it might:

Instead of sleeping: now the player is asleep; say "You drop
off."

This makes sense in Inform, but not in English. The resemblance to natural language here is probably a liability, rather than an asset.

Inform 7 does not expose some aspects of the underlying Z-machine, but the language includes the ability to drop down into Inform 6 when needed or simply perform some heavy lifting the old-fashioned way. Even pure I7 can resemble deeply-nested C code more than Pride and Prejudice, with line after line of end if; end if; end if;.

With Inform 6 always an option and already proven, the least Inform 7 could do is help. The majority of interactive fiction games are never finished, with most of them never getting further than a few rooms because even the shortest high-quality release requires significant time. Making that time more productive will result not only in more finished games, but better games, because the kind of dynamic responsiveness that delights players can be expressed so naturally. Best of all, implementing just about anything can be fun:

A person can be vampiric or human. A person is usually human.

Visibility rule when the player is vampiric and in darkness:
  say "With your cool new vampire eyes you are able to make out..."; 
  there is sufficient light.

Outdoors is a region. Every turn when the player is vampiric and in the
Outdoors: say "The light is pretty freaking bright out here." 

Instead of examining the player when the player is vampiric: 
 say "You are a delightful porcelain white, free at last of mortal zits."

Vampiric turning is an action applying to one thing. Understand "turn [person]"
and "bite [person]" as vampiric turning. 

Check vampiric turning: 
  if the player is not vampiric then say "But you aren't a vampire!" instead;
  if the noun is vampiric then say "Duh, [the noun] is already a vampire."
  instead.

Carry out vampiric turning:
  now the noun is vampiric.

Report vampiric turning:

  say "You grab [the noun] roughly and sink your teeth into the pulsing, fleshy
  neck.  The rush of power is like nothing else, except maybe when you've been
  the flyer in the basket toss during a homecoming game. [paragraph break]
  Anyway, bored now."

Part 5 - Resources

  1. The Inform 7 site, obviously, is the first place to start. From there you can read up on the theoretical basis for Inform 7 (PDF) and download Inform 7 and the I7 example games.
  2. The interactive fiction portal Brass Lantern is covering Inform 7 in depth, including a comprehensive introductory article to Inform 7 from Stephen Granade, who beta-tested I7, and a long theory piece on I7 by Emily Short.
  3. The IF community is active on the newsgroup rec.arts.int-fiction, and also on the interactive fiction chat room ifMUD.
  4. Although there are numerous interpreters already available, two merit special mention in the context of Inform 7: Gargoyle for Windows and Spatterlight for Mac OS X both focus on rendering IF attractively, with clean typography and good visual use of any bundled graphics--perfect for IF-as-books.

Liza Daly is a software engineer who specializes in applications for the publishing industry. Currently Liza is an independent consultant and the founder of threepress, a platform to produce open source publishing tools. She has been programming interactive fiction since she was nine but no longer recommends Commodore 64 BASIC or the color pink.


Return to ONLamp.com.

Copyright © 2009 O'Reilly Media, Inc.