Skip to content

Instantly share code, notes, and snippets.

@JoshuaGrams
Last active January 12, 2023 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaGrams/6ad4262079f1a7fcedca3d9a6c1bad6a to your computer and use it in GitHub Desktop.
Save JoshuaGrams/6ad4262079f1a7fcedca3d9a6c1bad6a to your computer and use it in GitHub Desktop.

The conversation system from Glass by Emily Short

A quick code walkthrough by Josh Grams

This is actually pretty straightforward: it's just very spread out and there's a lot of boilerplate directing everything you type to the conversation system, so it's easy to miss the important parts.


OK. Chapter 1/Section 1 - Subjects is very short and defines a “subject” and the “suggestion” relation that links subjects in a web that the player and the NPCs can follow. Then Section 2 - Something to Talk About uses the suggestion relation to build the actual web of subjects. This is easy to miss, because it's mostly “understand” statements. But mixed in there you'll also see: The king's health suggests heirs. Heirs suggests the marriage. Marriage suggests the ball. and so on.

At the very end of this section, we define the variable that holds the “target subject” that the NPCs are seeking toward. The NPCs are mostly treated as a single entity: they just use different names randomly. And they only have a single target in each scene: they don't change goals mid-course (although they could).

Section 3 - Non-player Goal-seeking is the core of the NPC conversation code. Each scene has a “conversation set” which is a table of quips that move the conversation from one topic to another. Some quips have a blank “starting entry” which means they can be used regardless of the current subject, and some have a blank “final entry” which means they clear the current subject and someone has to think up a new one.

So we have two actions here: they have very similar loops. One action checks to see if a subject is “fruitful” (“produces conversation”): are there any quips which take us from the current subject to the new one? The other does roughly the same search but prints the quip instead.

These are very straightforward: for each quip, check the starting entry and the final entry to see if they match (or are blank). When we say a quip, blank out that table row so it can't be used again.

Then there's the NPCs' every-turn rule. They can only act once per turn: they can respond to the player OR they can change the subject themselves. There's a footnote which says that allowing them to do both gives them more control over the conversation than the player has, and players don't like that.

Now, the tricky part of the goal seeking is all handled for us: if you define a relation between things (in this case the “suggestion” relation), Inform 7 can pathfind between them.

So the “segue” is the next step on the path toward the target subject. There's a while loop that repeats here, as NPCs are allowed to skip steps if there's no quip matching the first one. Interesting. Note that this uses the “fruitful” check defined above.

Then we use the “relate the subject with the other subject” command defined above to say the quip and update the variables holding the current and previous subjects.

If there's no path, then the conversation dies temporarily and there's an awkward silence and a reminder of what you're talking about.


Chapter 2 is all about handling player input. Sections 1 and 2 are a lot of boilerplate redirecting all sorts of player input through the one “mentioning” action, which is finally defined at the very beginning of Section 3 - Handling Legitimate Remarks.

You say “awwk” and then the noun (or “the noun! The noun!” a third of the time). Unusually, this happens in the “carry out” phase. Usually you print text in the "report phase" so you can perform actions silently if necessary. But here, as with examining, the whole point of the action is to print out text.

Then in the report phase of mentioning, we set the NPCs to passive (they don't get to change the subject this turn), and call our previously-defined "relate the current subject with the noun" action to find and say a quip that changes the topic to what you said (if possible).


I think that's the gist of the conversation system. There is a chapter for each scene, which sets the NPCs' target subject and the available conversation set.

The rest of the code is pretty usual Inform 7 stuff: set up the world, redirects for special cases of player dialogue, custom actions, and the like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment