Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

DIALOGUE SYSTEM

CUSTOM CLASSES, ARRAYS, QUEUES, LOOPS


INTRO

 A dialogue system is an integral component of most modern games and is a common method of reinforcing
narrative, as well as the underlining game mechanics and associated rule sets.
 In this example, we will create a basic structure which allows a series of sentences to be fed back to the user via a
user interface, which can be executed uniquely for a range of interactivity scenarios (objects, NPC’s etc)
 This system can be expanded to incorporate further elements if you wish, such as user responses or even a quest
system, but we will keep this linear to understand the basal principles.
 We will be looking at a couple of ways to execute this, including the use of arrays, lists, loops and queues.
ACTIVITY:

 In a group, discuss the components of a Dialogue system (Objects, scripting and player feedback)
 Draw a structured diagram to represent these components and how they will be required to interact with each
other.
BASE COMPONENTS OF A DIALOGUE SYSTEM

 We need a way to store the text that will be displayed to the user
 We need a way to trigger the text being displayed
 We need a way to process the text uniquely for each interaction point
 We need a way to display the text
STEP 1: THE UI
 We need a simple UI that will
display speech to the player
and allow the user to continue
the dialogue
 Create a canvas (if you do not
already have one)
 create a panel for the speech
area
 create a Text Mesh Pro text
object for the speech
 include a button on the panel
to allow the user to continue.
CLASS

 We have discussed the nature of a class previously.


 Not all classes need to be placed on objects (inherited from MonoBehaviour)
 We can create classes to hold data which can then be used within other MonoBehaviour based scripts for
processing.
 We use non inherited classes for anything that does not need to communicate with engine specific functions, such
as Start(), Update() and which do not need to be added as a specific component to an object but one which we
need to create multiple unique instances of. An example of this could be Item data.
 In this example, we will be working with a simple Dialogue system, where each NPC has their own custom speech
which is dictated by a custom class.
STEP 2A:

 We need a way to store the dialogue for each


unique NPC interaction.
 Lets create a new C# script called “Dialogue”
and open this in Visual Studio
 The script does not need to inherit from
MonoBehaviour, as we do not need to use it as a
component, rather we will be using it as a data
store. We can therefore remove Monobehaviour
from the class declaration.
STEP 2B:
 The script needs to store sentences in a variable
which can be used by interactable objects in our
game.
 Rather than creating a new variable for each
sentence, we can instead create an Array. An Array
stores multiple points of data of the same data type
using an Index. The first point in array starts at
Index [0]
 Lets create a new Array of type String so we can
store our sentences.
 Because this data needs to be accessible by other
scripts and the engine, we must make the class
Serializable
STEP 3A:
 We now need a way to manage these sentences, i.e: how they
will be displayed to the user.
 For systems such as this, its usually best to have a
“Management” script that takes input, processes it and produces
and output, rather than having all the code nested together.
 Because our management script needs to interact with the
engine and other objects in the scene, it needs to inherit from
MonoBehaviour and needs to be placed on a Game Object.
 Management scripts tend to be placed on Empty Game Objects
in the scene with appropriate naming conventions.
 Lets create an Empty Game Object, call it “DialogueManager”
 Create a new C# Script called “DialogueManager” and attach
this to the object
 Open this script in Visual Studio
STEP 3B:

We need to set up some variables first.

We need a reference to the Text Object to print the text to screen

As we are using Text Mesh Pro, we need to import the library

Then we create a variable to store the text object in our scene.

We need a variable to store the Dialogue panel so we can activate it


and disable it when needed.

Make sure to pop into Unity and allocate the text object and
Dialogue Panel to their respective fields.
STEP 4

We now need to implement all the methods that will control the dialogue.

We need to set up the dialogue first by taking in the custom dialogue class.

We could just pass in the sentences from the Dialogue class, but this will
prevent us from expanding the base class in the future.
Lets create a method called: setupDialogue() that takes in Data of type
Dialogue. Later we will then pass in the dialogue from our interactable
objects.

We then need a method which allows us to continue on to the next sentence.

Lastly we need a method to stop the dialogue


HANDLING THE DIALOGUE
 There are a few ways we can store the sentences in the
management script for processing.
 We could use an array and make this equal to the array of
sentences we are passing in.
 We could use a queue, which allows us to put every item in
the sentences array into a FIFO
 We could use a list which operates in a similar way to an
array, but gives us greater control over the length and custom
operations for removal and sorting.
 Either of these ways will be sufficient for our purpose,
however, we will be using an array in this example.
 Extended learning: revisit the dialogue system using a
queue and a List on the Dialogue Manager script.
STEP 5A
 Lets start setting up the methods.
 Firstly, we are going to need a new variable in the
management script to store the sentences that are
being passed in.
 In the setupDialogue method, we need to make
this variable equal to the array of sentences
within the Dialogue class we have passed in.
 We then need to activate the DialoguePanel
 We then need to execute the method
“NextSentence” so we can start to display the
sentences.
STEP 5B
 Lets setup the “nextsentence” method.
 Firstly, we need to make sure that the text object
is empty every time we call this method, which
will either be when we first interact with an
object or when we press the continue button on
the UI.
 If we check an index that falls outside the array’s
scope, we will get an error therefore we need to
check if the length of the array (sentences) is
equal to 0 as a fail safe, if it is, then we can call
the stopdialogue method and return out of the
condition.
STEP 5C
 We now need a way to print the strings from the sentences
array.
 We need to be able to print a specific string from the array
only when the nextsentence() method is called.
 To do this, we need nextsentence() to keep track of the
Array’s index position and increase the index position every
time nextsentence() is called.
 Lets create a variable to hold reference to the index position
and make it equal to 0 (the start of the array)
 We now need to check if this variable is less than the length
of the array, if it is we can make the text object display the
string at that index position, then increase the variable to
trach the index by 1
 If its greater than the length of the array, we can assume
there are no more sentences and we can set the variable to 0
again and stop the dialogue.
STEP 6

 Now all we have to do is trigger the dialogue.


 Create a script called “DialogueTrigger” and place this on an object you want
the player to interact with.
 Create a public variable to hold reference to the Dialogue class so we can add
sentences to pass through to the management script.
 Create a method which is called when our player triggers the dialogue, and
send the dialogue variable to the management scripts “SetupDialogue()”
method.
 Now we just need to call triggerdialogue(), this can be done through lots of
different ways, for instance, key press, distance etc, but we will do this through
collision.
 Create an OntriggerEnter Method and check whether the trigger has collided
with the player, if it has, we can call the triggerDialogue() method.
FINAL STEP
 Select the object in Unity that
you want the player to interact
with.
 You will now see that there is a
public array for adding
sentences.
 Add sentences you want to
display, then play the game
and test whether the player
triggers the dialogue sequence.
VISUAL IMPROVEMENTS

 The sentences currently print out in full, however, it may be more visually appealing if we print out the sentences
letter by letter over a given amount of time.
 Follow the tutor tutorial on coroutines and loops.
EXTENSION ACTIVITY

This therefore means that if


Extend the Dialogue
the player has already
system to only trigger the
interacted with the
dialogue if the player has
dialogue trigger, the
not yet interacted.
dialogue should not appear.

You might also like