HomeVideos

Build a Godot 4.5 Dialogue System in Under 10 Minutes

Now Playing

Build a Godot 4.5 Dialogue System in Under 10 Minutes

Transcript

212 segments

0:01

Text boxes are the voice of your game.

0:03

Today, we aren't just putting text on a

0:05

screen. We're building a flexible system

0:07

that handles the conversation for you.

0:11

Welcome back to GDO Dev Checkpoint.

0:13

Today, we're tackling one of the most

0:15

requested features, the dialogue system.

0:18

We need a way to freeze the action, show

0:20

a clean UI at the bottom of the screen,

0:22

and cycle through lines of text. Let's

0:25

build the foundation.

0:28

First thing we need is a canvas layer.

0:30

So in our other node, we're going to

0:32

search for canvas layer right here.

0:35

Create. We're going to rename this to

0:38

dialogue manager.

0:41

Dialogue manager.

0:44

And we need a child node. It's going to

0:45

be control

0:47

right there. Then rename this to

0:50

dialogue box.

0:53

May set this anchor preset in the top

0:56

here to bottom wide. Then the inspector

1:02

or layout

1:04

custom minimum sizing Y to 200. Now

1:07

invisible box is not very helpful. So

1:09

let's add a background with the text

1:11

label. Right click the dialogue box. Add

1:13

child node color rect.

1:16

We're going to change the color of that

1:18

to black and the transparency down to

1:22

0.5 in the linear the linear section

1:25

here. Now we're half transparency.

1:28

I also want to go full wctck in the

1:30

anchor. There we go. Now dialog box.

1:34

Right click add child node label. Change

1:37

label to dialogue text.

1:42

The anchor to be full. Put in some

1:45

sample text here. This is the dialogue

1:48

text.

1:50

What we're going to do here, label

1:53

settings, new label settings, font, font

1:56

size, put up to 32. We're also going to

2:00

do is go down to auto wrap mode. We're

2:02

going to turn it to word smart so our

2:04

long paragraphs will wrap properly. So

2:07

in GDO, when we pause the game,

2:08

everything pauses, including this

2:10

dialogue manager. So in order in order

2:12

to cycle through the text we need to go

2:14

to our dialogue manager go to our

2:16

process

2:18

mode

2:19

always. Now setting this to always

2:21

ensures that even when git tree paused

2:23

is true the script keeps running so we

2:26

can actually press through the dialogue.

2:27

All right. Now the bare minimum UI is

2:29

done. Let's make it think a bit. We need

2:31

a script that holds our lines of text

2:33

and knows which line we are currently

2:34

reading. So we're going to attach a

2:36

script to the root manager. Root

2:38

dialogue manager. Attach script. Now

2:40

we're go to a folder, new folder,

2:44

scripts,

2:45

save, open, create. We're going to

2:49

delete the boiler plate code. What we're

2:52

going to do here is we're going to have

2:53

two references to our dialogue box.

2:58

Control drop. And then our dialogue

3:01

text. Control drop.

3:03

And a few variables here. So we need our

3:05

dialogue lines

3:07

which are going to contain an array a

3:10

string

3:13

and then initialize it as empty. Then

3:16

we're going to do our current line index

3:20

and keep track of where we are. Start as

3:22

zero.

3:24

Then we need to say if the dialogue box

3:29

if dialogue active

3:32

bool is false to start it out with and

3:36

we need a ready function. It's a

3:37

function ready. Don't autocomplete enter

3:40

down. What we need to do is make our

3:43

dialogue box visible just so during this

3:46

tutorial it's automatically apparent.

3:50

Then save that script and the scene in a

3:54

new folder called scenes.

3:58

Save. Now we need three core functions.

4:01

One to open the box, one to advance the

4:03

next line, and one to close it all down.

4:06

We'll start with the start dialogue. So

4:08

our new function start dialogue.

4:11

That's going to take in some lines,

4:13

which is going to be the array. It's

4:15

going to be string array.

4:18

and then colon and down. First, we're

4:22

going to pause the game so the player

4:24

can't move.

4:28

You do that by getting the tree and just

4:30

calling paused to make it true.

4:33

And not that whatever that was. Enter

4:36

down. Then we're going to go into our

4:38

dialogue lines

4:41

which are going to be lines what we're

4:44

taking in.

4:46

And then we need to go to our next our

4:49

current line index which is zero.

4:52

Then we're going to make sure the

4:53

dialogue is active. Turn that to true.

4:58

Then we need to make the dialogue box

5:01

actually visible

5:03

so we can see it when you start talking

5:05

to an NPC or something.

5:08

And then we're going to check the text

5:12

text in the inspector

5:15

dialog text.ext and make that equal to

5:17

our diagonal line

5:20

dialog lines and then the current line

5:23

index we want to show.

5:25

Perfect. Let's save that. Enter that a

5:28

couple times. Then we check for the

5:31

input. So if the player has input and

5:34

wants to advance the text.

5:36

So function input event

5:39

and first we need to check if the

5:41

dialogue is even active. So if it's

5:43

active

5:47

and if it's not active return but if it

5:50

is active then we're going to advance

5:52

the actual dialogue. So we're check for

5:56

the is action pressed for now it's just

5:58

going to be the UI accept.

6:02

That's the default GDAU action which

6:05

will be enter or spacebar. And then

6:08

we're write a new function called

6:09

advance dialogue.

6:12

Enter down a couple times. And let's

6:13

write that function advance dialogue.

6:19

Okay. So this one we just need to check

6:21

if the current line index

6:26

there is less than the lines we have

6:29

left. So the size of the array

6:32

minus one because it's exclusive

6:36

and then we need current line index.

6:41

We're going to advance it by one. So the

6:43

next line and we're going to change the

6:45

text to the text box to actually show

6:48

that line.

6:50

So the current line index. There we go.

6:54

Perfect. I'll make this a little bigger.

6:57

There we go. So there's that. else. What

7:00

we need to do if we're done with the

7:04

actual

7:06

dialogue for the NPC or wherever, we

7:09

want to unpause the game when done. And

7:11

we do that just by saying the opposite

7:14

of what we did earlier. Get tree paused

7:17

is false.

7:19

Great. And last but not least in this

7:22

one, we need to say, is the dialogue

7:25

active? No, because we just closed it.

7:28

And we need to hide the box.

7:31

So dialog box visible

7:33

is false.

7:36

So now we have the core foundation. All

7:40

you have to do is when you approach an

7:42

NPC, you need to set up a call to this

7:45

dialogue manager to start the dialogue.

7:48

And we can do it in our ready so we can

7:49

see it right when we start the game. So

7:52

start dialogue.

7:54

And we'll just put a couple lines in

7:56

here. So do an array here. Start

7:59

dialogue. What we're going to do, we'll

8:01

just do freezing

8:03

again. One, two, three. And then we can

8:06

do after that, comma, down, enter, make

8:10

another line, and then say and

8:13

unfreezing

8:15

it now. There we go. So I start the

8:18

game. We're going to start the dialogue.

8:19

Freeze the game. Okay. Advance the

8:21

dialogue after we press our our dialogue

8:25

UI. accept.

8:27

It'll show the unfreezing. Then we press

8:30

it again. We won't have any lines left.

8:32

So then we'll be able to close the

8:34

dialogue box and unfreeze the game. All

8:37

right, let's go and give that a save.

8:39

Then we can run the current scene. Check

8:41

it out.

8:43

So the game is frozen right now. And

8:46

then if I press spacebar or enter, and

8:48

I'm unfreezing it now. So my dialogue

8:50

box still works even though it's paused.

8:52

And then if I press it again, now the

8:54

dialogue box closes.

8:57

And there we go. We have a UI that

8:59

scales to the screen and a script that

9:01

cycles through an array of text strings.

9:03

It's clean, simple, and ready to be

9:05

connected to your NPCs. To expand this,

9:08

we can create an NPC that actually calls

9:10

this function so we aren't just talking

9:11

to ourselves in code. Let me know in the

9:14

comments if that's something you're

9:15

interested in. I'm Spaghetti Zyax and

9:17

remember, always stay curious and I'll

9:20

see you at the next checkpoint.

Interactive Summary

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.

Suggested questions

4 ready-made prompts