-
Notifications
You must be signed in to change notification settings - Fork 86
Example Tutorial Blueprint
[[TOC]]
ℹ️ NOTE: The FINAL Example Project is here
ℹ️ NOTE: 🎥 A video tutorial version you can find here in this playlist
The purpose of this tutorial is to teach you how to use the dialogue system and how to integrate it into your own project.
The tutorial starts from the Third Person template project.
At the end the player character will be able to talk to our NPC, Mr. Cube and ask him to change his color. Mr. Cube will be happy to do that at the first few times, then he gets annoyed and won't change his color anymore.
- Create new project using the Third Person Blueprint template project with the name DlgExample.
- Install plugin from marketplace / Get the plugin source from our repository (Click HERE)
- Create a new Blueprint based on
Actor
with the nameBP_MrCube
. - Open the Blueprint, add a
Cube
component. - Go the the
Class Settings
to add theDlgDialogueParticipant
interface to the implemented interfaces list.
- Open the
GetParticipantName
Function - Create a
DialogueParticipantName
Variable ofType
equal toName
on the Blueprint and set the value toMrCube
.
NOTE: Only Used in the UI
- Open the
GetParticipantDisplayName
Function - Create a
DialogueParticipantDisplayName
Variable ofType
equal toText
on the Blueprint and set the value toMr. Cube
.
- Switch to the Content Browser
- Create a Dialogue asset with the name
Dlg_MrCube
- Open the Dialogue
- Drag an edge from the border of the start node and place a
Speech node
.
- With the recently added node selected, set the
Participant Name
toMrCube
- Set the
Text
toI am Mr. Cube! What can I do for you?
- Create a new
Speech node
and place it on the right of our first Node.- Set the
Participant Name
toMrCube
- Set the
Text
toGood to See you again!
- Set the
- Save the Dialogue
- Select the first Node you added
- Add an
Enter Condition
- Set the
Condition Type
toWas node already visited?
- The node index has to match the white number on the top right corner of the selected node
- Make sure that the
Is Node Visited?
checkbox is unchecked
- Add an
This setup will make sure that MrCube
will say his name only the first time the player talks to him.
Observe the fact that the node has an icon on its top left corner - that means that it has an Enter Condition
.
- Add a
Speech Node
child fromNode 0
- Set the
Participant Name
toPlayer
- Set the
Text
toCould you change your color?
- Set the
- Add a
End Node
child fromNode 0
- Make and edge from
Node 1
to the newly created Nodes:Node 2
andNode 3
- On the edges leading to
Node 2
set theText
toAsk him to change his color
- On the edges leading to
Node 3
set theText
toLeave
- Add a
Selector Node
child from Node 2- Set the
Participant Name
toMrCube
- Set the
Selector Type
toFirst
- Set the
- Add a
Speech Node
child from theSelector Node
(`Node 4)- Set the
Participant Name
toMrCube
- Set the
Text
toSure. What color would you like to see?
- Set the
- Add a
Speech Node
child from theSelector Node
(`Node 4)- Set the
Participant Name
toMrCube
- Set the
Text
toI can't keep changing my color all the time! Just leave me alone!
- Set the
This selector node automatically select the first child with satisfied conditions.
- Open the
MrCube
NPC Blueprint - Add a new
Variable
namedColorChangeRequestCount
- Set the
Type
toInteger
- Compile & Save the Blueprint
- Select the Edge from
Node 4
toNode 5
- Add a new
Condition
- Set the
Condition Type
toCheck Class Int Variable
- Set the
Participant Name
toMrCube
- Set the
Variable Name
toColorChangeRequestCount
- Set the
Operation
to<= (Is Less Than or Equal To)
- Set the
Int Value
to3
- Add a new
This will check the ColorChangeRequestCount
variable (you will have to type it the first time you use it).
If it is less than or equal to 3
then Mr. Cube
will be happy to change his color.
This selector node automatically select the first child with satisfied conditions - so each time the player asks Mr. Cube
to change its color it will either say the text of Node 5
or Node 6
depending on the variable value.
This ColorChangeRequestCount
variable will be Modified by the Dialogue in the next step.
- Select the
Speech Node 5
- Add a new
Enter Event
- Set the
Event Type
toModify Class Int Variable
- Set the
Participant Name
toMrCube
- Set the
Variable Name
toColorChangeRequestCount
- Set the
Int Value
to 1 - Make sure that the
Delta
checkbox is checked
- Add a new
This will increase the ColorChangeRequestCount
variable each time the dialogue enters this node.
- Add an
End Node
child from theSpeech Node 6
- Add three
End Node
children fromSpeech Node 5
- Set the Edge
Text
in this order from left to right:Red
,Green
,Blue
- Add an
Enter Event
to all three Nodes- The
Participant Name
should beMrCube
- The
Event Type
should beEvent
- The
- Set the
Event Name
in this order from left to right:ColorToRed
,ColorToGreen
,ColorToBlue
- Set the Edge
- Open the
BP_MrCube
Blueprint
- Create a new Function with the Name
SetCubeColor
- Add a new Parameter named
Color
of typeVector
- Call
SetVectorParameterValueOnMaterials
on the Cube with theColor
input- Set the
Parameter Name
toColor
- Set the
- Add a new Parameter named
- Create a New Material like in the picture below and set it on the
Cube
Component of theBP_MrCube
Blueprint
- In the
BP_MrCube
Blueprint open theOnDialogueEvent
Function - Place the
Switch on Relevant Dialogue Event
node - it lists the event names associated with Mr. Cube and works as a normal switch node.
- Call
SetCubeColor
depending on Event Name
- Open the Player Blueprint (for example
BP_ThirdPersonCharacter
) - Go the the
Class Settings
to add theDlgDialogueParticipant
interface to the implemented interfaces list. - Add the
DialogueContext
Variable ofType
equal toDlgContext
- Open the
GetParticipantName
Function - Add the
DialogueParticipantName
Variable of typeName
and set the value toPlayer
NOTE: Only Used in the UI
- Open the
GetParticipantDisplayName
Function - Add the
DialogueParticipantDisplayName
Variable of typeText
and set the value toPlayer
(or whatever you want)
- Open the
EventGraph
of your Player Blueprint - Add a
Custom Event
with the NameStartDialogueWithNPC
- Set the following inputs
-
Dialogue
of typeDlgDialogue
-
NPC
of typeObject
-
- Start the Dialogue by calling
StartDialogue
- Use the return value of the
StartDialogue
to set theDialogueContext
Variable of the Blueprint
- Open the
EventGraph
of your Player Blueprint - Add a
Custom Event
with the NameSelectDialogueOption
- Set the following inputs
-
OptionIndex
of typeInteger
-
- Call
ChooseOption
(NOTE: we will handle the return value later when we have the UI setup)
- Open the
BP_MrCube
Blueprint - Add a
Sphere Collision
Component as a child of theCube
Component- Set the
Sphere Radius
to300.0
- Set the
- Open the
EventGraph
on theBP_MrCube
Blueprint - Add a Variable with the Name
Dialogue
of TypeDlgDialogue
- Set the value to
Dlg_MrCube
- Set the value to
- Add a
On Component Being Overlap
on theSphere Collision
Component you added in step a. - Cast the
Other Actor
to your Player Blueprint and callStartDialogueWithNPC
(created here)
The UI will be composed from two widgets
-
UI_DialogueOption
- Which is just a button with a text, this represents a dialogue option -
UI_Dialogue
- Which is the main UI widget that containsText
,Participant Name
and the multiple ``UI_DialogueOption` options
ℹ️ Example widgets UI_DialogueOption
and UI_Dialogue
you can copy/look at are in the Example Project
- Switch to the Content Browser
- Create a new Widget Blueprint with the name
UI_DialogueOption
- Open the
UI_DialogueOption
Widget Blueperint - Add a
Button Widget
with the nameButtonX
- Set it as the root of the widget
- Add a
TextBlock Widget
as a child of the Button, with the nameOptionText
- Make them both as
Variables
- Switch to the
Graph
of theUI_DialogueOption
Widget Blueperint - Add a new Variable of
Type
equal toInteger
andName
equal toOptionIndex
- Compile & Save the Blueprint
- Select the
ButtonX
Button and add the following three events:On Clicked
,On Hovered
,On Unhovered
- From the
On Clicked
callSelectDialogueOption
(created here) on your Player Blueprint - From the
On Hovered
events set the color of yourOptionText
- Add a Custom Event with the Name
Update
- Add as input a Parameter with the
Name
equal toContext
andType
equal toDlgContext
- Update the
OptionText
only if theOptionIndex
is Valid and set the widget visibility in each case
- Switch to the Content Browser
- Create a new Widget Blueprint with the name
UI_Dialogue
- Open the
UI_Dialogue
Widget Blueperint - Set a
Canvas Panel Widget
as the root - Add the following
TextBlock Widgets
:SpeakerName
andText
- Add a
VerticalBox Widget
with the Name equal toOptions
- Add four
UI_DialogueOption Widgets
inside this vertical box - Set the
Option Index
for each of them
- Add four
- All these Widgets should be
Variables
- All the
Variables
are anchored to the bottom left
- Switch to the
Graph
of theUI_Dialogue
Widget Blueperint
- On
Event Construct
hide this Widget - Add a Custom Event with the Name
Close
- Which hides this Widget
- And sets the Input Mode and Hides the Mouse
- Add a Custom Event with the Name
Update
- Set the
SpeakerName
,Text
andAvatar
(Optional) - Iterate over all the
UI_DialogueOption Widgets
and callUpdate
(created here) on them
- Add a Custom Event with the Name
Open
- Set this Widget to
Visible
- Set the Input Mode and Show the Mouse
- Call the
Update
event on this Widget you created previously
- Open the Player Blueprint (for example
BP_ThirdPersonCharacter
) - Create a
UI_Dialogue
Variable ofType
equal toUI_Dialogue
(This is the Dialogue Widget you created above) - Open the
EventGraph
and onEvent BeginPlay
Create the new widget and add it to viewport
- Go to the
StartDialogueWithNPC
Event you Created - There at the end
Open
theUI_Dialogue
- Go to the
SelectDialogueOption
Event you Created - There at the end
Update
theUI_Dialogue
- And reset the
DialogueContext
andClose
theUI_Dialogue
ifChooseOption
returnsfalse
- Place the
BP_MrCube
Blueprint in the level - If you start the game and go near him the dialogue should start
- You can ask him to change his color four times before he gets annoyed
- Leaving him and returning again triggers the dialogue again
- Open your
GameMode
Blueprint (for exampleBP_ThirdPersonGameMode
) - On
Event Begin Play
callRegister Dialogue Console Commands
and `Clear Dialogue History - On
Event End Play
callUnregister Dialogue Console Commands