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.