Dialogue System
Create a Dialogue Folder in your content drawer and open up the folder
Create a new Character Blueprint
Name it BP_NPC
Open it up
Apply the mesh settings
Add your NPC into your game
In your Dialogue Folder
Create a new BluePrint but this time you are going to create a Blueprint Interface
Name it BPI_Dialogue
Add a function
Name the fuction Speak
Compile and save
Open up the third person blueprint
Right Click
Seach E Key
Pull out For Each Loop
From the Array in For Each Loop, pull out Get overlapping actors NOT Get Overlapping Actors (capsule component)
We are going to add a Collision Sphere to the NPC we created so we have something to set off the conversation
Name it trigger
Change the radius to 200
In your collision in the details panel, select OverlapAllDynamic in in the Collision Preset
Go back to your event graph and select the NPC you created as the class filter
Pull out the Array Element from For Each Loop and add Does Implement Interface
The interface is the BPI_Dialogue that we created
Pull out a branch from the return value of Does Implement Interface
Connect Loop Body to the Branch
So what is happening here is that if you can interact with the BPI_Dialogue, then it is true
Go back to your NPC Blueprint
Click on Class Settings
Find Interfaces and add Implrmrnted Interfaces
Select your BPI_Dialogue
Open the Third Person Blueprint Event Graph
From the Array Element Pull out and add Speak
Connect your True Branch into the Speak Node
Now your Third Person Player should interact
Open up the NPC Blueprint
Right-click on your Speak Interface and select Implement event
Pull out Print String
Compile, Save, Test
When you run into the collision and press E, you should get the hello on your screen
In your NPC Blueprint, add a new variable
Name it Dialogue and make it a string variable
In the Details panel, chance it to an Array (so we can have a few messages)
Drag in youe Dialogue Variable and select GET
Create a new Integer Variable and name it talkIndex
With your talkIndex Selected, go to your details panel and change the Integer Variable Type to Single
Delete your Print String
Drom your Dialogue Variable, add GET (a copy)
Bring in your talkIndex and select GET then connect to your Get Copy
Pull in another Talk Index (Get) then pull out Incredent by adding ++
Connect Event Speak
Pull out a new print screen and connect the ++
Compile and save so we can start with the integer
With the talkIndex Integer Selected, set the default value to -1
We are setting it to -1 because the array always starts a Zero (remember the different hit animations)
So it starts at -1 and when the 1 get added from the ++, it will fire off at 0
With your Dialogue Variable Selected, add three array elements and add some text
So we have created a bug because once we press E, it adds to the talk indext and we only have three arrays so we need to add some code to fix this.
Break the link between the ++ and Print String
Pull in a talkIndex (GET)
Pull out a less equal node
Pull in a Dialogue Variable (GET)
Pull out Length (get the numbers in the array)
Connect the length to the <=
Make this the condidtion of your branch
In your UI Folder or your NPC Folder, create a new UI Widget Blueprint
Select User Widget
Name it UI_Dialogue
Open your widget
Add canvas panel
Drop in some text
Zize the text box
add your anchors
Change the text
Set it how you wish
In the details panel, make sure your Text is a variable
Go back to your NPC Blueprint
From Print String, pull out Create Widget and select the UI_Dialogue for the class
From the Owning Player, pull out Get Player Controller Node
In the return value of Create Widget, promote to variable (right click on the node)
Name the variable UI
From Set, pull out Add to Viewport
Compile, Save and Test
You can see that it only works with teh first message.
Go back to the NPC BP
Pull in your new UI Variable at GET UI
Right click on the UI Variable
Select Convert to Validated Get
Connect the Print Strint to the Input of Get Variable
Connect the Is Not Valid to the input of the Create UI Dialogue Widget
Now we want to get the different messages
Disconnect the UI from Add to Viewport
From UI, pull out Get TextBlock or whatever you named it
Pull out Set Text
Connect to Output of Add to Viewport
SUPER IMPORTANT
You must also connect the IS Valid output from GET to the input of SelfText
We don't want the text to run through the print string anymore, we want it showing up in the UI so disconect the output of GET and connect it to the SetText Input
Remove orint screen node and connect the true output from the brancg ti the GET input
Bring in a UI Variable (GET) and place it near the False Output of your branch node.
From the UI Variable, pull out Remove From Parent
Connect the False output from the Branch Node to the White Input of the remove from parent