From idea to source code

After playing some IF games, many people decide that they'd like to try writing a few of their own. The first step is to translate your ideas into source code. Source code is a set of instructions, written in a computer language, which define such things as what objects are in the game, what they do, how they react when the player character tries to do stuff to them, and so forth. There are many computer languages designed specifically for writing IF, such as TADS, Hugo, and ALAN. But the most widely used IF language as of this writing is called Inform.

Inform is a pretty high-level language, meaning that it's closer to the way humans process information ("If the player character tries to take the cracker away from the parrot, the parrot should bite him") than the way computers do ("001001101110010"). A fragment of Inform code to represent this behavior might look like this:

   Object parrot "Polly the parrot"

      with name 'polly' 'parrot' 'bird',

         before [;

            LetGo: if (noun == cracker) "Polly bites your hand as you

               try to pilfer his cracker! ~Awwwk! Hands off!~ he



      has animate proper;

Even if you don't know Inform, you can look at this code and get a pretty good idea of what it does. It's certainly much easier to render ideas into this form than directly into a string of numbers. (And if you'd like to learn Inform -- which you'll need to do to before continuing on to section two -- a great place to start is the Inform web site, run by the language's creator, Graham Nelson.)

Now, some pieces of a given Inform game will be unique to that game -- not every game has a parrot in it, for instance. But a lot of pieces will be pretty much the same from game to game. For instance, most every piece of IF needs a parser, the part of the game that takes complex commands like "PUT EVERYTHING EXCEPT THE CRACKER IN THE PARROT'S CAGE" and breaks it down into a to-do list for itself ("run the Insert verb on the parrot, the birdseed and the newspaper, but not the cracker, with the cage as the destination"). Writing a parser from scratch is extremely hard -- most "homebrewed" parsers (and a handful show up in each IF competition) are terrible. It's also a waste of time: if one person has coded up a really good one, and you want your parser to do pretty much the exact same stuff, why can't you just use that person's code? Answer: you can. For the parser, as well as the object management system and pretty much every element to interactive fiction that remains the same from game to game, is included in the Inform library.

A library is a group of files, written in the same language you're programming in, that you can include in your program to perform certain functions without having to code them up yourself. In Inform, this is as easy as typing:

   Include "Parser";

   Include "VerbLib";

   Include "Grammar";

Code up your game's unique objects and routines, add those three lines in the appropriate places, and your program is complete. However, no computer can run Inform source code directly: the source code needs to be translated into machine code.

Next section:
From source code to virtual machine code
Or return to the table of contents