Using a JSON dialogue system with NPCs

13

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

basicjsondialogueexample.c3p

Download now 167.69 KB

Stats

7,246 visits, 14,894 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 11 Dec, 2019.

A lot of the core events to control the dialogue in this scenario are the same as the previous tutorial, but with a different control system and a little extra bit of control over the dialogues called.

The first thing to set up is the button control to open the dialogue. The Z key acts as the interaction key and is used for initiating and progressing the dialogue. This event block includes quite the cascade of sub-events, so I’ll break down the explanation slightly. Plus, in order to help demonstrate which level each sub-event is on underneath the main condition, I’ll add a letter, A, B, C or D. Then there’ll be a screenshot of the event block at the end of the explanation.

To begin with:

Condition

Keyboard ▶︎ On Z 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)

These first two levels simply tell the game that something needs to happen when the Z key is pressed, but only if the player is overlapping one of the NPC objects. Carrying on, we need a pair of sub-events on the same tier to co:

Sub-event Condition

Sub-event level B

System ▶︎ Game State = “”

Sub-event Condition

Sub-event level C

NPC ▶︎ Is NOT MultiLine

Sub-event Action

System ▶︎ Set curScene to NPC.SceneID

System ▶︎ Set GameState to “Dialogue”

Sub-event Condition

Sub-event level C

NPC ▶︎ Is MultiLine

Sub-event Condition

Sub-event level D

NPC ▶︎ TimesSpokenTo = 0

Sub-event Action

System ▶︎ Set curScene to NPC.SceneID

System ▶︎ Set GameState to “Dialogue”

Sub-event Condition

Sub-event Level D

NPC ▶︎ TimesSpokenTo > 0

Sub-event Action

System ▶︎ Set curScene to NPC.SceneID2

System ▶︎ Set GameState to “Dialogue”

The above sub-events check a few things before the dialogue begins, including whether or not an NPC has multiple sets of dialogue available, and how many times the NPC has been spoken to. Once the Scene is set, the GameState changes to allow Dialogue to begin. Next, we need to define what happens when the GameState is Dialogue, still continuing from the previous nested event block:

Sub-event Condition

Sub-event level B

System ▶︎ GameState = “Dialogue”

NPCTextBox ▶︎ Is NOT running typewriter text

Sub-event Action

System ▶︎ Add 1 to curDialogue

Functions ▶︎ Call DisplayDialogue (dialogueID: curDialogue, sceneID: curScene)

Sub-event Condition

Sub-event level C

System ▶︎ curDialogue = Functions.GetNumLines+1

Sub-event Action

Functions ▶︎ Call EndDialogue (NPCUID: NPC.UID)

This is finally the end of the nested event block for the Z key. In this last bit, the game checks how to progress with the dialogue. If there are still lines left, it waits until the current one has finished displaying before loading the next. If there are no lines left, the dialogue is ended.

The final event block should look like this:

At this point, the dialogue system is functional. Text is displayed, each NPC shows different text depending on what instance variables you’ve assigned to them and once they’ve said everything, the dialogue layer is rehidden. But there are some tweaks we can make to improve this slightly, which will be covered in the next section.

Next Tutorial In Course

Extending the JSON Dialogue System 07:09

This tutorial expands on the JSON system used in part 4.2 of this course. It includes more text formatting using the format function as well as calling functions and timelines from the dialogue.

  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Thanks so much! This helped me out a ton!

  • Thanks so much for making this, I couldn't have done it on my own, but now I understand.

    Also a lot more elegant and flexible than some other solutions out there, good work

  • I'll take a look at this compared to my current XML implemenation of the Dialog system. Looks pretty similar in scope, but the ability to call in fuctions via dialogues to effect the entire game would be quite handy like allowing for the player to choose their own name.