• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
  • comments
  • views

The Poor Man's Dialogue Tree

Sign in to follow this  
Followers 0


As some of you may know, Lemma has an interactive dialogue system that lets you exchange text messages with an AI character.

I implemented every conversation manually in code (well, scripts) until this week, when I got fed up and decided to automate the process!
Like the last article in this series, my system has all the hallmarks of a Poor Man's solution: developed in-house, tailor-made, simple, and based on free and open source software.

Step 1: What exactly am I doing

This might shock you, but I decided to model conversations as trees. Hold on to your butts, guys.
The big problem with a purely tree-based system is that the number of branches can easily explode to an unmanageable size. When I was scripting conversations manually, I could use tricky code to express complex behavior without explicitly writing out every possibility.
For example: let's say you're making a game about Mexican drug cartels. It's called 4:20 to Yuma. Early in the game, an NPC named Enrique asks you at gunpoint where his money is hidden. You have two choices: tell him where it actually is, or lie.
At that point, technically the entire game splits into two possible outcomes. In our conversation tree model, we would model this as two giant branches.

Now eventually, that choice will have an impact on the game, but in the meantime, Enrique will probably behave the same either way. He'll drag you out to wherever you said the money was. Your choice only has an impact once you reach the destination and Enrique finds out if you told the truth or not.

Let's say it's a long journey and there's a lot of dialogue along the way. That dialogue is the same regardless of your initial choice, yet we have to keep two separate copies of it to maintain our initial branches.

This is less than ideal. A purely tree-based model is clearly too simple for our purposes. On the other hand, I don't want to implement a full-on visual programming language because a) that sounds hard and b) the whole purpose of this system is to avoid convoluted code.
In the end, I decided to add one new construct to our tree-based system: variables. This lets me set a variable, continue on with the rest of my dialogue, and branch based on that variable later, like this:

It's still pretty simple, but much more powerful. An added advantage is that you can easily split the dialogue into multiple files and link them together using variables.

Step 2: Author

I needed a fast, visual way to design dialogue. I immediately ruled out an in-game editor. It would be way too much work to write everything from scratch. I looked at a variety of flowcharting tools before settling on JointJS, an HTML5 flowchart library that integrates nicely with Backbone and jQuery. I dusted off my JavaScript skills and set to work.
I started with this example, which demonstrates how to combine regular HTML elements with the SVG it uses to render the boxes and connections.
JointJS turned out to be an absolute joy to work with. It has clear separation between the model and presentation layers. Each node is a Backbone model, and it's pretty easy to create a new type of node:joint.shapes.dialogue = {};joint.shapes.dialogue.Base = joint.shapes.devs.Model.extend({ defaults: joint.util.deepSupplement ( { type: 'dialogue.Base', size: { width: 200, height: 64 }, name: '', attrs: { rect: { stroke: 'none', 'fill-opacity': 0 }, text: { display: 'none' }, '.inPorts circle': { magnet: 'passive' }, '.outPorts circle': { magnet: true, }, }, }, joint.shapes.devs.Model.prototype.defaults ),});
After a whole lot of JavaScript hacking and CSS styling, I ended up with this:

I came up with five node types:

  • Text - displays a message from the AI character. Can link to one Text, Node, Set, or Branch, or to one or more Choices.
  • Node - does nothing. Can link to one Text, Node, Set, or Branch, or to one or more Choices.
  • Choice - presents a choice to the player. Can link to one Text, Node, Set, or Branch.
  • Set - sets a variable to a value. Can link to one Text, Node, Set, or Branch.
  • Branch - takes one of several paths based on the value of a variable. Each port can link to one Text, Node, Set, or Branch.

    I chose these rules to make it easier for the authoring tool to validate the dialogue tree. They still offer a lot of power. You can start execution at any node other than a Choice and it will make sense.
    The purpose of the Node type is two-fold. First, it allows me to start a conversation by offering Choices to the player without displaying a Text from the AI character. Second, it allows me to link Branches to Choices, which is useful if I want to offer choices A and B in one case and choices C and D in another case.
    The words you see in the Text and Choice nodes are localization markers. I write a few words that describe the gist of the message. Later I write the full message in an Excel spreadsheet and my localization system fills in the correct text (more on that in another article!) The localization marker doubles as a unique identifier that can be accessed in code as well.
    Try the demo yourself! Right-click to show the menu. Source code here. Tested in Chrome and Firefox.

    Step 3: Export

    One great thing about a JointJS graph is that, since it's a Backbone model, it can export and import to and from JSON. It even picks up your custom properties!
    Here's my save code:localStorage[filename] = JSON.stringify(graph);
    And here's how I load it back up:graph.clear();graph.fromJSON(JSON.parse(localStorage[filename]));
    Here's how to export the data to a file and have the user download it:function offerDownload(name, data){ var a = $(''); a.attr('download', name); a.attr('href', 'data:application/json,' + encodeURIComponent(JSON.stringify(data))); a.attr('target', '_blank'); a.hide(); $('body').append(a); a[0].click(); a.remove();}
    The JSON data includes a lot of information about the visual layout of the graph. This is great because the graph will load up exactly how you left it, but all that extra information can make it tough to parse in your game engine. I found it necessary to write a function that goes through the JSON and pulls out only the parts needed in a game engine.

    Step 4: Execute

    Almost done! Now we just parse the JSON data, pick an initial node, and start executing the instructions.
    I used the excellent
    Json.NET library for parsing. Here's the entirety of my execution code:public void Execute(Node node, IListener listener, int textLevel = 1){ string next = null; switch (node.type) { case DialogueForest.Node.Type.Node: if (node.choices != null && node.choices.Count > 0) listener.Choice(node.name, node.choices.Select(x => this[x].name)); next = node.next; break; case DialogueForest.Node.Type.Text: listener.Text(node.name, textLevel); if (node.choices != null && node.choices.Count > 0) listener.Choice(node.name, node.choices.Select(x => this[x].name)); next = node.next; textLevel++; break; case DialogueForest.Node.Type.Set: listener.Set(node.variable, node.value); next = node.next; break; case DialogueForest.Node.Type.Branch: string key = listener.Get(node.variable); if (key == null || !node.branches.TryGetValue(key, out next)) node.branches.TryGetValue("_default", out next); break; default: break; } if (next != null) this.Execute(this[next], listener, textLevel);}
    The IListener interface provides four functions: Text and Choice to display messages and choices to the player, and Get and Set for accessing variables. There's not much more to it, but you can check out the full code here.


    I think the types of conversations you can express with this system are pretty varied, but if it's not enough it would be very easy to add new kinds of nodes. Go forth and dialogue!

    edit: Yes, it's not a tree, it's a directed graph. My bad!

    Mirrored on my blog

Sign in to follow this  
Followers 0


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now