Currently, this dialogue system is usable, but there are a few things that could be improved. For example, the player can wander about while talking to an NPC. And it might be nice to have a button to automatically load all of the text rather than wait for typewriter to finish.
Let’s start with keeping the player in position while dialogue is running. This is very simple to do – we just need to enable/disable the TileMovement behaviour based on the value of the GameState variable:
Condition
System ▶︎ GameState = “”
Action
Player ▶︎ Set TileMovement Enabled
Condition
System ▶︎ GameState = “Dialogue”
Action
Player ▶︎ Set TileMovement Disabled
So, now that the player can’t move while talking to an NPC, let’s look at tweaking the dialogue itself slightly. The first thing we’ll add is a sprite that appears once the typewriter text has finished signifying that you can progress the dialogue. The sprite can be called and look like whatever you like, I’ve just used a small arrow icon called ContinueDialogue:
Condition
NPCTextBox ▶︎ On typewriter text finished
System ▶︎ GameState = “Dialogue”
Action
ContinueDialogue ▶︎ Set to Visible
If you make sure the object is initially set to invisible when you add it to the dialogue layer, it should only appear when the text has finished displaying. This makes it a nice indicator to the player that they can press Z to continue.
The last tweak we’ll add is to allow the player to press a button to quickly load all the text in a line. This may be more comfortable to use on a gamepad, but it works perfectly well on keyboard:
Condition
Keyboard ▶︎ On X key pressed
Sub-event Condition
Sub-event level: A
Player ▶︎ Is overlapping NPC at offset (8,0)
OR
Player ▶︎ Is overlapping NPC at offset (-8,0)
OR
Player ▶︎ Is overlapping NPC at offset (0,8)
OR
Player ▶︎ Is overlapping NPC at offset (-8,0)
Sub-event Condition
NPCTextBox ▶︎ Is typewriter text running
Sub-event Action
NPCTextBox ▶︎ Finish typewriter
This will immediately finish the typewriting effect and display all the text at once. The player still needs to press Z to carry on to the next line, but you could probably add something to the nested event block for Z key control to change that if you wanted to.
And that’s all for this tutorial! Hopefully, you understand how to implement a basic JSON dialogue system a bit now.
Happy Developing!