This tutorial starts where the Simple NPC tutorial ends – we previously configured a simple NPC dialogue encounter that allows the player to interact with an NPC by clicking on it when in range.
If you have already completed the Simple NPC tutorial, but do not have the scene available, you can follow along using the completed tutorial scene:
|Assets\TurnTheGameOn\NPC Chat\Example Scenes\Simple NPC Tutorial|
Next, we’ll setup structured dialogue with multiple possible conversation paths for this NPC.
Setup A Chat Box For Structured Dialogue
- Duplicate and customize the default chat box currently used by the NPC Chat object; adjust layout and styling of buttons, text, images, etc as required by your design. Depending on the style you’re going for, you may want to create multiple unique chat box objects that are custom designed for each conversation.
In my case, I’ll rename the new chat box to Structured Chat Box, then enable it and adjust the button 0 and 1 positions, scale and font size. I’ll also adjust the Dialogue Text rect size and position.
This chat box will be used to offer the user 2 options; we’ll use these to configure branching dialogue next.
Disable the chat box when finished.
Setup NPC Chat For Structured Dialogue
- Select the NPC Chat object and change the Chat Type to Structured, this will add more configuration options to the inspector and allow us to configure additional conversations.
- Set the number of Conversations to 3 and press update. Press – or + to navigate between conversations.
Structured dialogue uses the NPC Number as a way of indexing the NPC, this index is used by the assigned Chat Manager, NPC Chat Indexes array’s corresponding element index.
With structured dialogue, the Chat Manger is responsible for controlling which Conversation index an NPC Chat object will use when dialogue is started.
This tutorial covers controlling the Chat Manager through the NPC Chat inspector, but since it’s a Scriptable Object, you can also reference it in your own scripts and have other systems control your NPCs through custom methods.
Configure A Branching Conversation
- Assign the Structured Chat Box from step 1 to Conversation index 0.
- Add Title and Dialogue Text, I’ll make this a question that the robot is asking the player.
- Expand Page Buttons, and Enable Elements 0 and 1, these are the Chat Box Buttons we configured in step 1.
- Each of these buttons has a text field, use this to assign the button text. I’ll make the button text the possible player responses.
At this point, we’ve setup Conversation 0 with dialogue and button text, but the buttons don’t do anything. We can use the inspector events to call any script at this point, just like a standard Unity UI Button.
- Configure required button events. More details below. You can also add your own custom events to this event list.
4 event calls are required to be made by a button event for NPC Chat to automatically transition out of the current conversation and into a new conversation.
|ChatManager.SetNPCNumberToEdit(int)||Sets the active NPC Number on the Chat Manager.|
|ChatManager.SetNewConversationForNPC(int)||Sets the new Conversation Index for the active NPC Number.|
|NPCChat.CloseChat||Closes the current NPC Chat Conversation.|
|NPCChat.StartConversation||Starts the new conversation index dialogue.|
Setup The Branch Conversations
- Navigate from Conversation Index 0 to Conversation Index 1 by pressing the + button in the Conversation Index section. Then assign the Default Chat Box and some dialogue – we configured our first button to flow into conversation 1.
- Enable Use Next Dialogue and leave the value at 0. We do this so that when conversation 1 is complete, we want the NPC to use conversation 0 the next time dialogue starts, so that the player can choose the “Yes, I’m ready” option.
- Navigate from Conversation Index 1 to Conversation Index 2 by pressing the + button in the Conversation Index section. Then assign the Default Chat Box and some dialogue – we configured our first button to flow into conversation 2.
By default, the Chat Manager will keep a reference of the NPC’s current conversation index. We can have the Chat Manager reset the NPC’s conversation index on scene start by enabling the option Reset Chat Manager Index On Scene Start on the assigned NPC Chat Options profile. This will make sure that every time our scene loads, this NPC starts at conversation 0.
Press play, walk up to the NPC and click on it.
You have now setup an NPC with structured dialogue using NPC Chat. This modular workflow should allow you to create limitless branching dialogue scenarios in a modular manner.
This tutorial is now complete. Thank you.