Build a Godot 4.5 Dialogue System in Under 10 Minutes
212 segments
Text boxes are the voice of your game.
Today, we aren't just putting text on a
screen. We're building a flexible system
that handles the conversation for you.
Welcome back to GDO Dev Checkpoint.
Today, we're tackling one of the most
requested features, the dialogue system.
We need a way to freeze the action, show
a clean UI at the bottom of the screen,
and cycle through lines of text. Let's
build the foundation.
First thing we need is a canvas layer.
So in our other node, we're going to
search for canvas layer right here.
Create. We're going to rename this to
dialogue manager.
Dialogue manager.
And we need a child node. It's going to
be control
right there. Then rename this to
dialogue box.
May set this anchor preset in the top
here to bottom wide. Then the inspector
or layout
custom minimum sizing Y to 200. Now
invisible box is not very helpful. So
let's add a background with the text
label. Right click the dialogue box. Add
child node color rect.
We're going to change the color of that
to black and the transparency down to
0.5 in the linear the linear section
here. Now we're half transparency.
I also want to go full wctck in the
anchor. There we go. Now dialog box.
Right click add child node label. Change
label to dialogue text.
The anchor to be full. Put in some
sample text here. This is the dialogue
text.
What we're going to do here, label
settings, new label settings, font, font
size, put up to 32. We're also going to
do is go down to auto wrap mode. We're
going to turn it to word smart so our
long paragraphs will wrap properly. So
in GDO, when we pause the game,
everything pauses, including this
dialogue manager. So in order in order
to cycle through the text we need to go
to our dialogue manager go to our
process
mode
always. Now setting this to always
ensures that even when git tree paused
is true the script keeps running so we
can actually press through the dialogue.
All right. Now the bare minimum UI is
done. Let's make it think a bit. We need
a script that holds our lines of text
and knows which line we are currently
reading. So we're going to attach a
script to the root manager. Root
dialogue manager. Attach script. Now
we're go to a folder, new folder,
scripts,
save, open, create. We're going to
delete the boiler plate code. What we're
going to do here is we're going to have
two references to our dialogue box.
Control drop. And then our dialogue
text. Control drop.
And a few variables here. So we need our
dialogue lines
which are going to contain an array a
string
and then initialize it as empty. Then
we're going to do our current line index
and keep track of where we are. Start as
zero.
Then we need to say if the dialogue box
if dialogue active
bool is false to start it out with and
we need a ready function. It's a
function ready. Don't autocomplete enter
down. What we need to do is make our
dialogue box visible just so during this
tutorial it's automatically apparent.
Then save that script and the scene in a
new folder called scenes.
Save. Now we need three core functions.
One to open the box, one to advance the
next line, and one to close it all down.
We'll start with the start dialogue. So
our new function start dialogue.
That's going to take in some lines,
which is going to be the array. It's
going to be string array.
and then colon and down. First, we're
going to pause the game so the player
can't move.
You do that by getting the tree and just
calling paused to make it true.
And not that whatever that was. Enter
down. Then we're going to go into our
dialogue lines
which are going to be lines what we're
taking in.
And then we need to go to our next our
current line index which is zero.
Then we're going to make sure the
dialogue is active. Turn that to true.
Then we need to make the dialogue box
actually visible
so we can see it when you start talking
to an NPC or something.
And then we're going to check the text
text in the inspector
dialog text.ext and make that equal to
our diagonal line
dialog lines and then the current line
index we want to show.
Perfect. Let's save that. Enter that a
couple times. Then we check for the
input. So if the player has input and
wants to advance the text.
So function input event
and first we need to check if the
dialogue is even active. So if it's
active
and if it's not active return but if it
is active then we're going to advance
the actual dialogue. So we're check for
the is action pressed for now it's just
going to be the UI accept.
That's the default GDAU action which
will be enter or spacebar. And then
we're write a new function called
advance dialogue.
Enter down a couple times. And let's
write that function advance dialogue.
Okay. So this one we just need to check
if the current line index
there is less than the lines we have
left. So the size of the array
minus one because it's exclusive
and then we need current line index.
We're going to advance it by one. So the
next line and we're going to change the
text to the text box to actually show
that line.
So the current line index. There we go.
Perfect. I'll make this a little bigger.
There we go. So there's that. else. What
we need to do if we're done with the
actual
dialogue for the NPC or wherever, we
want to unpause the game when done. And
we do that just by saying the opposite
of what we did earlier. Get tree paused
is false.
Great. And last but not least in this
one, we need to say, is the dialogue
active? No, because we just closed it.
And we need to hide the box.
So dialog box visible
is false.
So now we have the core foundation. All
you have to do is when you approach an
NPC, you need to set up a call to this
dialogue manager to start the dialogue.
And we can do it in our ready so we can
see it right when we start the game. So
start dialogue.
And we'll just put a couple lines in
here. So do an array here. Start
dialogue. What we're going to do, we'll
just do freezing
again. One, two, three. And then we can
do after that, comma, down, enter, make
another line, and then say and
unfreezing
it now. There we go. So I start the
game. We're going to start the dialogue.
Freeze the game. Okay. Advance the
dialogue after we press our our dialogue
UI. accept.
It'll show the unfreezing. Then we press
it again. We won't have any lines left.
So then we'll be able to close the
dialogue box and unfreeze the game. All
right, let's go and give that a save.
Then we can run the current scene. Check
it out.
So the game is frozen right now. And
then if I press spacebar or enter, and
I'm unfreezing it now. So my dialogue
box still works even though it's paused.
And then if I press it again, now the
dialogue box closes.
And there we go. We have a UI that
scales to the screen and a script that
cycles through an array of text strings.
It's clean, simple, and ready to be
connected to your NPCs. To expand this,
we can create an NPC that actually calls
this function so we aren't just talking
to ourselves in code. Let me know in the
comments if that's something you're
interested in. I'm Spaghetti Zyax and
remember, always stay curious and I'll
see you at the next checkpoint.
Ask follow-up questions or revisit key timestamps.
The video demonstrates how to build a flexible dialogue system in Godot Engine. It details the setup of the user interface, including a Canvas Layer, a Control node for the dialogue box, a ColorRect background, and a Label for displaying text, along with configurations for anchoring, sizing, and font properties. A critical step involves setting the dialogue manager's process mode to "always" to ensure it remains active even when the game is paused. The tutorial proceeds to script essential functions: "start_dialogue" to initiate conversations and display the first line, "_input_event" to detect player input, and "advance_dialogue" to progress through dialogue lines, unpause the game, and hide the UI once the conversation concludes. The system is then successfully demonstrated, showcasing its ability to freeze and unfreeze the game while managing text display.
Videos recently processed by our community