Using a JSON dialogue system with NPCs

13

Index

Features on these Courses

Attached Files

The following files have been attached to this tutorial:

.c3p

basicjsondialogueexample.c3p

Download now 167.69 KB

Stats

6,285 visits, 13,287 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.

.C3P

basicjsondialogueexample.c3p

Download now 167.69 KB

Welcome to another tutorial on using a JSON-based dialogue system. As mentioned above, this follows on from a previous tutorial, but where that one demonstrated a continuous dialogue system, the dialogues in this example are tied to specific NPC objects and are only shown when the player interacts with them.

The example file is made up of two NPC objects, the player and the Dialogue layer which is only shown when we play dialogue. The NPCs have several instance variables to help track the dialogue:

  • SceneID – this dictates which scene array in the JSON file to draw dialogue from
  • SceneID2 – this allows us to store a second SceneID to call under different conditions
  • TimesSpokenTo – this counts the number of times the player has talked to the NPC
  • MultiLine – a Boolean to check whether or not the NPC has more than one available line of dialogue

You’ll also need some global variables:

  • curDialogue (number)
  • curScene (number)
  • GameState (string)
  • PlayerName (string)

It’s probably also worth creating a group to hold your functions, because you’ll be using a few of them! Just keeps things a bit tidier in your event sheet. There’ll be four functions in total, all of which are the same as in the previous tutorial:

  • DisplayDialogue
  • DialogueFormat
  • GetNumLines
  • EndDialogue

There is a small addition to the DialogueFormat function in this example project however. Previously, the function was used just to apply formatting to the text, but you can use the same replace expression to insert variables into the text! Adding the following action to the function:

Set Line to replace(Line,”PlayerName”,PlayerName)

Allows you to replace the string “PlayerName” within one of the JSON lines with the string from the PlayerName global variable! You could probably do the same with other variables, or even dictionary keys! If you do give it a go, let us know how you get on in the comments!

In the next section, we’ll start looking at some of the events specific to this example project.

  • 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.