As we saw in the previous video, we prepared a script named
DialogueTree.gd
with a dialogue
variable to
save you typing.
The dialogue
variable holds nested dictionaries
representing our dialogue tree.
This data structure is the foundation of our dialogue system, so let’s take a moment to break it down.
To create a branching dialogue system, you want to give each node of your dialogue tree a unique ID (identifier). Using this ID, you can retrieve and display the corresponding line.
With a dictionary, it is easy to store the dialogue’s data and jump to different dialogue branches.
Take the first dialogue line’s key-value pair.
var dialogue = {
0: {
"text": "Hey, [i]wake up![/i] It's time to make video games.",
"expression": "neutral",
"buttons":
{"Let me sleep a little longer": 2,
"Let's do it!": 1,
}
},#...
}
For this project, we decided to use unique numbers for the dialogue line IDs (keys) and to map those to dictionaries with all the data we need to display (values).
This first line has a unique ID of 0
.
We map the ID 0
to a dictionary containing all the
information about this dialogue node:
We decided to use a dictionary to represent the buttons as it lets us map text to the unique ID of another line of dialogue, like so.
"buttons":
{"Let me sleep a little longer": 3,
"Let's do it!": 2,
}
Once we finish coding the system, the dictionary above will result in the following display.
Pressing a button will jump to the corresponding line.
How do you access dictionaries inside of other dictionaries? With square brackets, like before.
Take this example.
var dialogue_line := dialogue[0]
The dialogue_line
variable accesses the value mapped to
0
in dialogue
and stores the following
dictionary.
{"text": "Hey, [i]wake up![/i] It's time to make video games.",
"expression": "neutral",
"buttons":
{"Let me sleep a little longer": 2,
"Let's do it!": 1,
} }
We can then access the text
value by writing
dialogue_line["text"]
and the expression
value
by writing dialogue_line["expression"]
.
Your turn: how would you access the buttons?
To access the dictionary containing the buttons’ data, you would
write dialogue_line["buttons"]
.
We could also access this text directly from the
dialogue
variable by chaining the square brackets, like so:
dialogue[0]["text"]
.
Your turn: how would you access the buttons directly from the
dialogue
variable?
To access the buttons’ data from the dialogue
variable
directly, you would write dialogue[0]["buttons"]
.
Open the practice The credits screen.
When you complete the practice, you should end up with the following result.
If you make a dialogue-heavy game, you do not want to write dictionaries by hand.
In this lesson, we show you the kind of data structure you would use to make a branching dialogue system work.
But in a large project, you would only work directly with this kind of data for prototyping or fixing bugs.
You would then use a dialogue editor to write the bulk of the content.
You can write your own editor tailored to your game. Or you can save time by using one of the open-source plugins for Godot.
We recommend Dialogic: one of the most powerful dialogue editors.
Still, it’s helpful to know how to set up the data structure as the techniques used here are not specific to dialogue trees.
We’ll start displaying text and character portraits in our dialogue box in the next lesson.