The Poor Man's Dialogue Tree
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 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:
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.
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:
And here's how I load it back up:
Here's how to export the data to a file and have the user download it:
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:
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!
If you enjoyed this article, try these: