Hey all, So I was trying to figure out how to create events that can trigger at various points in your Dialogue Tree during conversation and I also had someone ask me if I knew how to kill and NPC after you finish talking to them. So to kill two birds with one stone, here’s a short tutorial on how to create a custom event of killing the NPC after you finish talking. This same process would work for any kind of event you might want to trigger through the Dialogue Plugin.
Custom Events with your Dialogue Tree Tutorial
This tutorial assumes you’ve already successfully setup your Dialogue Plugin as per the youtube videos posted by Artemavrin.
Any time you want to create any kind of event that plays during any part of your dialogue tree, you need to create a Behavior Tree Task blueprint and then create your code inside that blueprint.
**Step 1: **
Right click in your Content Browser and choose “Blueprint Class” from the list. Under All Classes search for “BTT” and you’ll see BTTask_BlueprintBase. Highlight this and press the Select button.
Step 2:
Name your new BTTask file “BTT_NPC_Kill” (or whatever you want the event to be). Open the new BTT_NPC_Kill blueprint and right click and search for “Execute” and you’ll see “Event Receive Execute”. Add this node to the event graph and this is how you trigger whatever code you wish to run. Right click again and search for “Finish” and you’ll see “Finish Execute”. Add this node and this is how you end your code sequence. Make sure to check off the Success bool otherwise it won’t work properly. These two nodes are mandatory for your code to work.
If you don’t want to create the ability to kill the NPC after the conversation ends, create the code you want to execute inside your “BTT_Whatever_Name” blueprint and then skip to Step 5:
Step3:
Now open your NPC Blueprint and add a Variable. Name it “KillOnComplete?” and make it a Boolean. In the details panel make sure you check off “Instance Editable” and then write a tooltip like “If yes, Destroy actor after talking to them”. Optionally, you can also create a second variable called “DelayBeforeKill” and make it a float if you want to have the option to put a delay before the NPC is destroyed. Maybe if you want to play an animation or what have you.
Now in the Event Graph, right click and search for “Custom Event” and choose “Add Custom Event”. Name the node KillNPC and then press and hold the B key and left click to add a Branch node. Connect the output from the KillNPC pin to the input of the Branch and then left click on the “KillOnComplete?” boolean in the Variables list and drag it onto the condition pin of the Branch node. This will connect your boolean to the branch. Left click and drag off the True pin from the Branch and search for “Destroy”. Under Utilities you’ll see “Destroy Actor”. Select this and the node is added. Select all the nodes and then press C on the keyboard to add a comment box and name it “Kill NPC”. This is all we need to kill the NPC, but there’s still a bit more to do since we now need to create a way for the game to find the correct NPC to kill. Don’t worry about the Delay variable, we’re not going to use it here.
Step 4:
Place your NPC in your level and select it. In the Details Panel check off the “KillOnComplete?” checkbox. Also if you added the DelayBeforeKill variable, set the time in seconds for the delay. I used 3. Now go back to your BTT_NPC_Kill Behavior Tree Task blueprint and we’re going to add the necessary code to find our NPC. Firstly, we need to use a Get All Actors of Class node to find the NPCs in our level so Right click and search for “Actors Class” and at the bottom choose the “GetAllActorsOfClass” utility. Place this to the right of the Event Receive Execute node and connect it’s pin. In the Actor Class drop down, search for your NPC or if it’s still selected in the world, just hit the Left Arrow. Now we don’t want to kill all of the NPCs in the level who have the KillOnComplete bool set to yes, we just want the one we’re currently talking to. The easiest way I thought of to do this is to get the one closest to the player with a low search radius. This limits you slightly in that you can’t put two NPC who will die right beside each other, but the search radius is only 400 units so they can still be fairly close. Place multiple NPCs who you want to kill at least 800 units apart and you’re fine.
Now drag out from the Out Actors array of the Get All Actors Of Class node and search for ForEachLoop. Add this node and connect its execute pin. Now an inch or two below the Event Receive Execute pin, right click and search for “Get Player Character” and add this node. Then drag off the Return Value and search for “Get Distance To” and add this node. From the return value, drag out and search for “Less” and choose “Float < Float”. In the B area, change the value to 400. Now to the right of the ForEachLoop node, hold the B key and Left click to add a Branch node. connect the Loop Body pin into the input of the Branch and connect the result of our Less Than condition into the Condition of the Branch.
Now drag out from the Array Element on the For Each Loop node and choose Promote To Variable from the top of the list. Name this variable ClosestNPC and connect the True pin into it’s input. Drag out from the Variable’s blue result pin and search for our two variables “Kill On Complete?” and “Delay Before Kill” and add these two on top of each other. Add another Branch node and plug the Kill on Complete bool into its Condition. Drag out from the True pin and search for “Delay” to add a delay node. Plug the result of the Delay Before Kill float into the Duration of the Delay and then once more drag out from the result of our ClosestNPC variable and search for “Kill NPC” and select our custom event from the list. Connect the Completed pin from the Delay node into this and then connect the Kill NPC node into the Finish Execute Pin. Also be sure to drag the False pin of our two Branches into the Finish Execute node as well to make sure it finishes even if those branches are false. Compile and Save the blueprint.
Your whole Event Graph for the BTT_NPC_Kill should look like this:
Step 5:
Now all that’s left to do is open up your Dialogue Tree for the NPC and where ever you want or in the case of this tutorial, just before the Close Dialogue node, we want to add in our new Behavior Tree Task. Right click in the Behavior Tree and Expand the Tasks menu and find our new BTTask in the list.
If you don’t see your new Behavior Tree Task in the list, be sure to hit the File - Save All option and the close and reopen the Dialogue Tree and it should show up. Now just connect it by dragging a line from the last Question Group to it and you’re done.
Hit the Play button and test out your code. In our case, the NPC should disappear from the scene 3 seconds after the conversation ends.