{"id":438,"date":"2021-11-04T02:00:01","date_gmt":"2021-11-04T02:00:01","guid":{"rendered":"https:\/\/globalgoodplay.com\/?p=438"},"modified":"2022-11-07T09:52:51","modified_gmt":"2022-11-07T09:52:51","slug":"enemy-ai-with-behavior-trees-in-unreal-engine","status":"publish","type":"post","link":"https:\/\/globalgoodplay.com\/?p=438","title":{"rendered":"Enemy AI With Behavior Trees In Unreal Engine"},"content":{"rendered":"<div class=\"elementor elementor-17370\" data-elementor-type=\"wp-post\" data-elementor-id=\"17370\">\n<section class=\"elementor-section elementor-top-section elementor-element elementor-element-642fd89e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"642fd89e\" data-element_type=\"section\">\n<div class=\"elementor-container elementor-column-gap-default\">\n<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-4e45978a\" data-id=\"4e45978a\" data-element_type=\"column\">\n<div class=\"elementor-widget-wrap elementor-element-populated\">\n<div class=\"elementor-element elementor-element-78a99faa elementor-widget elementor-widget-text-editor\" data-id=\"78a99faa\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In my previous post about enemy AI we created enemy patrol, chase and attack behavior using Blueprints and C++.<\/p>\n<p>In this post we are going to do the same but we are going to use behavior trees and sensing.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-420ebf2 elementor-widget elementor-widget-heading\" data-id=\"420ebf2\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Download Assets And Complete Project For This Tutorial<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-e7aae43 single-post-download elementor-widget elementor-widget-wc-elements\" data-id=\"e7aae43\" data-element_type=\"widget\" data-widget_type=\"wc-elements.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"woocommerce\">\n<div class=\"single-product\" data-product-page-preselected-id=\"0\">\n<div id=\"product-17461\" class=\"product type-product post-17461 status-publish first instock product_cat-uncategorized downloadable virtual product-type-simple\">\n<div class=\"summary entry-summary\">\n<p class=\"price\">\n<style>\n    .single-product div.product .summary form.cart { display: none!important; }<br \/>\n  <\/style>\n<\/p>\n<\/div>\n<section class=\"related products\">\n<h2 class=\"woocommerce-loop-product__title\">Unity Enemy AI Tutorial<\/h2>\n<p>Buy now!<\/p>\n<\/section>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-92e347b elementor-widget elementor-widget-text-editor\" data-id=\"92e347b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>To follow along with this tutorial, please download the started project by clicking on the green Download assets button above.<\/p>\n<p>In the downloaded folder you will find the finished project, and the started project which I prepared for you to follow this tutorial.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3e8576c3 elementor-widget elementor-widget-heading\" data-id=\"3e8576c3\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Important Information Before We Start<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6e0f7415 elementor-widget elementor-widget-text-editor\" data-id=\"6e0f7415\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>This tutorial is meant for intermediate and advanced Unreal Engine developers. If you are a complete beginner then I recommend that you first go through my C++ tutorial series, then go through my introduction to Unreal Engine tutorial series and after that go through this tutorial.<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4ab267e5 elementor-widget elementor-widget-heading\" data-id=\"4ab267e5\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Blackboards And Behavior Tress<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-74785883 elementor-widget elementor-widget-text-editor\" data-id=\"74785883\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Blackboards and Behavior Trees are two main AI frameworks structures in Unreal Engine. Blackboards are used to store data for the Behavior Tree and the Behavior Tree acts like a brain of the AI.<\/div>\n<div><\/div>\n<div>Combined together, these two components allow us to create advanced AI behavior in Unreal Engine faster and more efficient than in any other way.<\/div>\n<div><\/div>\n<div>Let\u2019s start by creating the components that we need. Open the started project that is provided in this tutorial and then inside the Content -&gt; Blueprints folder, Right Click -&gt; New Folder and name it AI.<\/div>\n<div><\/div>\n<div>Inside the AI folder Right Click -&gt; Artificial Intelligence -&gt; Blackboard:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-64996228 elementor-widget elementor-widget-image\" data-id=\"64996228\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-1-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/6e2e7ab692eb6423c8e596308acf2da3b8d2b594a744f09ff19f06a73ce57ab2.jpeg\" alt=\"Img-1-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-42f5826f elementor-widget elementor-widget-text-editor\" data-id=\"42f5826f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Name the Blackboard BB_EnemyAIData and then Right Click -&gt; Artificial Intelligence -&gt; Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2c9ec40f elementor-widget elementor-widget-image\" data-id=\"2c9ec40f\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-2-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/468c52445718f14f73ec6a576272f91e20ff1d7f920ec686e3f5f08b5f6e7528.jpeg\" alt=\"Img-2-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2261fc19 elementor-widget elementor-widget-text-editor\" data-id=\"2261fc19\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Double click BB_EnemyAIData to open the Blackboard in the editor. You will see two tabs in side the Blackboard editor, one named Blackboard and the other Blackboard Details:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-43c648d7 elementor-widget elementor-widget-image\" data-id=\"43c648d7\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-3-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/4a8711c8dbb75235441b747dd7342ea0d06bfa1a2d6ac41be141c295f774cb04.jpeg\" alt=\"Img-3-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-45f7b21e elementor-widget elementor-widget-text-editor\" data-id=\"45f7b21e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In the Blackboard tab we will see all the keys that we defined which represents the data of the Blackboard, and in the Blackboard Details tab we will see details about specific key we select.<\/p>\n<p>In the Blackboard tab you will see a New Key blackboard icon, this is where we create new keys for the Blackboard.<\/p>\n<p>Click on the New Key icon and create two new keys, one will be a bool named Can See Player, and the other will be a Vector named Random Patrol Location:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-16858736 elementor-widget elementor-widget-image\" data-id=\"16858736\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-4-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/45191b83400e052c9ee9ffdcd823850b78f0bc6df718d66cb7893e57ab3505ba.jpeg\" alt=\"Img-4-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-37c23029 elementor-widget elementor-widget-text-editor\" data-id=\"37c23029\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now select the SelfActor variable and name it Player Target. Make sure that the Key Type is set to Object in the Blackboard Details tab and that the Base Class is set to Actor:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2ca392a2 elementor-widget elementor-widget-image\" data-id=\"2ca392a2\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-5-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/04e590108029af80c1de2eb16c13225436cf14af07fbb0b1ca0e0a4f1715bc12.jpeg\" alt=\"Img-5-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-c8601da elementor-widget elementor-widget-text-editor\" data-id=\"c8601da\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>These are the variables that we will use to shape the enemy AI behavior which will happen in the BT_EnemyAI Behavior Tree.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-44dc8fa5 elementor-widget elementor-widget-heading\" data-id=\"44dc8fa5\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Creating Enemy AI In The Behavior Tree<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-43502fd3 elementor-widget elementor-widget-text-editor\" data-id=\"43502fd3\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The first thing we need to do is open the BT_EnemyAI, click on the Root node, and in the Details tab for the Blackboard Asset select the BB_EnemyAIData:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-710c0a7 elementor-widget elementor-widget-image\" data-id=\"710c0a7\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-6-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d7cc1e1765852331402f371eddfd829fabfdea4146deee6bd1faa0324af7a6ef.jpeg\" alt=\"Img-6-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-180ac4d7 elementor-widget elementor-widget-text-editor\" data-id=\"180ac4d7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This will allow the Behavior Tree to use the data we defined in the BB_EnemyAIData Blackboard.<\/p>\n<p>Now Left Click and drag from the Root node, when you release a pop up window will appear and you are going to click on Selector:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6405055e elementor-widget elementor-widget-image\" data-id=\"6405055e\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-7-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/7e5d6f06922a16412e59b4fad0c3f82dcf7c035f7521358ec28e76cc5da26753.jpeg\" alt=\"Img-7-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1cdd1db6 elementor-widget elementor-widget-text-editor\" data-id=\"1cdd1db6\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can also Right Click anywhere in the editor for the Behavior Tree and click on the Selector:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5ad9216d elementor-widget elementor-widget-image\" data-id=\"5ad9216d\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-8-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a95cc890d791d1e3cb1b520b29a84f60e4f234bf8f1e599af8f49464a93556fa.jpeg\" alt=\"Img-8-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-24713aec elementor-widget elementor-widget-text-editor\" data-id=\"24713aec\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The Selector node executes its children from left to right, and it will stop executing its children when one of their children succeeds. This basically means that the Selector will execute the task nodes we provide, and when that task finishes its execution, the Selector node will start executing again from start.<\/p>\n<p>Let\u2019s create a Sequence node and make the Selector node point to it:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-620be573 elementor-widget elementor-widget-image\" data-id=\"620be573\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-9-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/0ee8bb34d4302a01088a3653f46eeb2f9fc286f4b65477f5b9de416de6f998cf.jpeg\" alt=\"Img-9-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-37ed3461 elementor-widget elementor-widget-text-editor\" data-id=\"37ed3461\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The Sequence node also executes its children from left to right and stops when one of their children succeeds. From this Sequence node, we are going to calculate a random position in space, and then make the AI move towards that position.<\/div>\n<div><\/div>\n<div>To do this, first we need to create a Task which can be done in one of two ways. The first way is to click on the New Task button in the BT_EnemyAI:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7d5ee3ff elementor-widget elementor-widget-image\" data-id=\"7d5ee3ff\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-10-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a7c96d755694ca0821e479790b6644eadfa8b7ab2e5a018ed3f0de7505cf1a58.jpeg\" alt=\"Img-10-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7ccbf9c9 elementor-widget elementor-widget-text-editor\" data-id=\"7ccbf9c9\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Another way is to Right Click -&gt; Blueprint Class, and in the search bar for All Classes inherit BTTask_BlueprintBase:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-576f24c8 elementor-widget elementor-widget-image\" data-id=\"576f24c8\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-11-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/fb25bffb70122b2cee52bae0ed25d0c861e4e388fb99a715a31842a167232fab.jpeg\" alt=\"Img-11-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1b222793 elementor-widget elementor-widget-text-editor\" data-id=\"1b222793\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Before we create a new Task, in the AI folder, Right Click -&gt; New Folder and name it Tasks. Now create a new Task and name it Task_GetRandomLocationPoint then open it in the editor.<\/p>\n<p>Inside the My Blueprint tab under Variables, create a new variable of type Blackboard Key Selector Structure, name it Random Location Key and also make it a public variable:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1b90964c elementor-widget elementor-widget-image\" data-id=\"1b90964c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-12-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/1e060b0063f9f9d5dbe09adeac55f3d95e56cb5fbf2850c3c34690d07084368b.jpeg\" alt=\"Img-12-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-50c30862 elementor-widget elementor-widget-text-editor\" data-id=\"50c30862\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In order to access the Keys we defined in BB_EnemyAIData Blackboard we need to provide the Blackboard Key Selector Structure as the parameter.<\/p>\n<p>Next, we are going to override a function called Receive Execute AI:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-108163b8 elementor-widget elementor-widget-image\" data-id=\"108163b8\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-13-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/3ed96bcb739b0443dba7358044e3a5e41a91818107d92b1a6ed1c1f982ca448c.jpeg\" alt=\"Img-13-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5d3e2fd elementor-widget elementor-widget-text-editor\" data-id=\"5d3e2fd\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>This function will provide us access to the controlled pawn e.g. the pawn that will be controlled by the AI, so that we can access its position and from it generate a random point:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-690ac0bd elementor-widget elementor-widget-image\" data-id=\"690ac0bd\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-14-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/c4958f74fc4c6b057cbab66c53895a774707497952817da644b7da5e023a2b74.jpeg\" alt=\"Img-14-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-73204f53 elementor-widget elementor-widget-text-editor\" data-id=\"73204f53\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>You can copy the nodes from here:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-62ff58c1 elementor-widget elementor-widget-html\" data-id=\"62ff58c1\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/6s_w4d-a\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4b8969a8 elementor-widget elementor-widget-text-editor\" data-id=\"4b8969a8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We need to call Set Blackboard Value as Vector because the Random Patrol Location is a Vector type variable. We also need to call the Finish Execute node to indicate that the Task has finished executing, otherwise the Task will only execute once.<\/p>\n<p>Compile and save the changes we made to Task_GetRandomLocationPoint and open BT_EnemyAI editor. We can either Right Click and under Tasks search for the Task_GetRandomLocationPoint or drag a node from the Sequence node and select the Task_GetRandomLocationPoint:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4e8cfa46 elementor-widget elementor-widget-image\" data-id=\"4e8cfa46\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-15-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/71a9ce33e2942f0daf3945d21f99c43ef8df3564d0077669a845451c1bdf9b15.jpeg\" alt=\"Img-15-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1481981c elementor-widget elementor-widget-text-editor\" data-id=\"1481981c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now select the new task node, and in the Details tab for the Random Location Key, click on the drop down list and select Random Patrol Location variable:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3dee6aa7 elementor-widget elementor-widget-image\" data-id=\"3dee6aa7\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-16-Fixed.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/2100b99bb7ebe466af60b276c9f34b7cc94309e137180437c654a8e6d9bd6c6a.jpeg\" alt=\"Img-16-Fixed.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4a55bc9f elementor-widget elementor-widget-text-editor\" data-id=\"4a55bc9f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This is how we are going to indicate that the Random Location Key variable we defined in the Task_GetRandomLocationPoint is pointing to the Random Patrol Location variable we defined in BB_EnemyAIData Blackboard.<\/p>\n<p>The last step is to call the Move To Task by Right Click -&gt; Tasks -&gt; Move To:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-56774d67 elementor-widget elementor-widget-image\" data-id=\"56774d67\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-17-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/734340dcc69aae684345f2a0e307d507b78ba9156e329b786e6213bd64a842f9.jpeg\" alt=\"Img-17-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-12f59af1 elementor-widget elementor-widget-text-editor\" data-id=\"12f59af1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can also drag a node from the Sequence, but make sure that the Move To task is positioned after the Task_GetRandomLocationPoint because if you remember, the Sequence node will execute its children e.g. tasks, from left to right, so we first need to generate a random location point, and then make the AI move to that location.<\/p>\n<p>The order of execution is also denoted with numbers on each node, the lower the number means the node will be executed first:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-667cd735 elementor-widget elementor-widget-image\" data-id=\"667cd735\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-18-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d5f141303cdd8bfd2630adf96bf4884764f75a8adb0b034d18b9ebfe56979f8e.jpeg\" alt=\"Img-18-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5415376d elementor-widget elementor-widget-text-editor\" data-id=\"5415376d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The last step is to select the Move To node, and in the Details tab for the Blackboard Key select the Random Patrol Location:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4954cb0c elementor-widget elementor-widget-image\" data-id=\"4954cb0c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-19-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/12b0625bacdd7a7e0815396b5ef0be9f7592ff23e8f533f7e4b453153a927a5e.jpeg\" alt=\"Img-19-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5aa337a7 elementor-widget elementor-widget-heading\" data-id=\"5aa337a7\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Running Behavior Tree From AI Controller<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6ec34e28 elementor-widget elementor-widget-text-editor\" data-id=\"6ec34e28\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>To make the Behavior Tree control our enemy character, we need to create an AI Controller Blueprint. Inside the Content -&gt; Blueprint -&gt; AI, Right Click -&gt; New Blueprint Class and inherit from AIController and name the new Blueprint BP_BB_EnemyAIController:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-570db14e elementor-widget elementor-widget-image\" data-id=\"570db14e\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-20-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/8169f2293b4ee34136a940b1567357c33f4108728169a240fa12c3a6b9cdd93b.jpeg\" alt=\"Img-20-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-71eaba46 elementor-widget elementor-widget-text-editor\" data-id=\"71eaba46\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Before we run the Behavior Tree from the AIController Blueprint, open the BP_Enemy_BT located in Content -&gt; Blueprints.<\/p>\n<p>In the Components tab Select the top parent BP_Enemy_BT(self), and in the Details tab under Pawn for the AI Controller Class select BP_BB_EnemyAIController:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5185450a elementor-widget elementor-widget-image\" data-id=\"5185450a\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-21-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/bb1fa782dca52f9fc46be0eb612fb53fbf64496ebfe80e3da7018fb86d21ad88.jpeg\" alt=\"Img-21-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3f46f1d7 elementor-widget elementor-widget-text-editor\" data-id=\"3f46f1d7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now open the BP_BB_EnemyAIController Blueprint in the editor. Create a new variable of type Behavior Tree and name it AI Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5b4f27dd elementor-widget elementor-widget-image\" data-id=\"5b4f27dd\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-22-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/8017588798fadbe7f3817399aadc621e47cbb765c14676dab671aced06ff3e0a.jpeg\" alt=\"Img-22-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2eceb8ea elementor-widget elementor-widget-text-editor\" data-id=\"2eceb8ea\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Next, select the AI Behavior Tree variable, and in the Details tab under Default Value select the BT_EnemyAI:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-21ee82ae elementor-widget elementor-widget-image\" data-id=\"21ee82ae\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-23-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d81f04f84b9f5fe49516613ce460c754b09580a94bdadf32ddce1055ac27f489.jpeg\" alt=\"Img-23-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-414a2627 elementor-widget elementor-widget-text-editor\" data-id=\"414a2627\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>To make the Behavior Tree run, we need to add the following nodes:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2c6693d8 elementor-widget elementor-widget-image\" data-id=\"2c6693d8\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-24-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/46b5e0c9e4e8bef31acd350ebe29a4cf43db91d397626c640147b6446e8d576d.jpeg\" alt=\"Img-24-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4420f364 elementor-widget elementor-widget-text-editor\" data-id=\"4420f364\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can copy the nodes from here:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-71e30944 elementor-widget elementor-widget-html\" data-id=\"71e30944\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/790h0bar\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-719529f0 elementor-widget elementor-widget-text-editor\" data-id=\"719529f0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now let\u2019s run the game and test it out:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-52f4b372 elementor-aspect-ratio-169 elementor-widget elementor-widget-video\" data-id=\"52f4b372\" data-element_type=\"widget\" data-settings=\"{\" data-widget_type=\"video.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-wrapper elementor-fit-aspect-ratio elementor-open-inline\">\n<div class=\"elementor-video\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-54b3e3a2 elementor-widget elementor-widget-text-editor\" data-id=\"54b3e3a2\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Because the BP_BB_EnemyAIController is now controlling the enemy, as soon as the game started and the BeginPlay inside BP_BB_EnemyAIController was executed which has the code to run the Behavior Tree we saw that the enemy started patrolling the level.<\/p>\n<p>We also saw in the BT_EnemyAI the execution flow of the nodes. You can always open the Behavior Tree Blueprint and watch the execution flow for debugging purposes to see if everything works.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-328c5b16 elementor-widget elementor-widget-heading\" data-id=\"328c5b16\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Pawn Sensing Component<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-66587c4 elementor-widget elementor-widget-text-editor\" data-id=\"66587c4\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In the previous post about enemy AI we used collision components to detect the presence of the player, in this post we are going to use PawnSensing component which allows our AI to have human senses like hearing and seeing.<\/p>\n<p>One tip when it comes to setting up the AI system, if you have an AIController like we do in this example, you can attach the PawnSensing component on the AIController.<\/p>\n<p>That way, every enemy in your game that uses that AI controller will have the sensing ability, which is much better than to go in every enemy blueprint and attach a PawnSensing component.<\/p>\n<p>So let\u2019s open BP_BB_EnemyAIController and in the Components tab click on Add Component button and attach the PawnSensing component:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5490aa3b elementor-widget elementor-widget-image\" data-id=\"5490aa3b\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-25-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/1391b994c398593c5c0f10aa4af66a64e3eb3b2090a8424627f90bf8ec1e4f11.jpeg\" alt=\"Img-25-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4b4c5059 elementor-widget elementor-widget-text-editor\" data-id=\"4b4c5059\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Open the Viewport tab and select the PawnSensing component in the Components tab, this what you will see:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4b844e elementor-widget elementor-widget-image\" data-id=\"4b844e\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-26-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/2157b3d5859e75969d32877eea659fb7c14db4d64bb393d7400a316d247514c2.jpeg\" alt=\"Img-26-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-69d8c92d elementor-widget elementor-widget-text-editor\" data-id=\"69d8c92d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In the Details tab we have the settings for the PawnSensing component, specifically the AI settings which is what we are interested in:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2bc0671c elementor-widget elementor-widget-image\" data-id=\"2bc0671c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-27-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a5b6cbc7c8ff28cc33b7a6fbadf1ea659b78453d2737b4b4a4e6a6a769b3280e.jpeg\" alt=\"Img-27-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5125b72c elementor-widget elementor-widget-text-editor\" data-id=\"5125b72c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We are going to use sight sensing to make the enemy see the player in the game. For that we have the Sight Radius settings, which is the radius of the enemy\u2019s sight. We also have the Peripheral Vision Angle which is the view angle of the enemy.<\/p>\n<p>We are going to change the Peripheral Vision Angle to 60:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-16fb5c9e elementor-widget elementor-widget-image\" data-id=\"16fb5c9e\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-28-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/e10c0ea371c7235cfc4d01741a17a0ea1114c4d46f9b72c3f9d4aad1b8b07415.jpeg\" alt=\"Img-28-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-613fcf15 elementor-widget elementor-widget-text-editor\" data-id=\"613fcf15\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You will notice that this has also change the shape of the PawnSinsing component in the Viewport tab:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2d2f1029 elementor-widget elementor-widget-image\" data-id=\"2d2f1029\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-29-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/20062de5c793af6f5b85b88650bb50f061f0ac76e99fd34be718a1150b2339cd.jpeg\" alt=\"Img-29-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-57a290dc elementor-widget elementor-widget-text-editor\" data-id=\"57a290dc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>I am also going to change the Sensing Interval to 0.2:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7241f873 elementor-widget elementor-widget-image\" data-id=\"7241f873\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-30-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/09d671c01cbbf482565d0274726b122aa12992257264a267908c638f5984a7ea.jpeg\" alt=\"Img-30-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4cfb234c elementor-widget elementor-widget-text-editor\" data-id=\"4cfb234c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The Sensing Interval implies how often the system will update the senses, setting the value at 0.2 means every 0.2 seconds the system will update the senses and make the AI \u201csee\u201d in the game.<\/p>\n<p>These are the settings that I am going to use for our example, you are, of course, free to experiment with all the values and see the outcome of the changes you made.<\/p>\n<p>You can make the enemy detect the player on large distances or you can make the enemy detect the player only when he is close enough to the enemy, so be my guest and experiment with different options which is the best way to learn.<\/p>\n<p>Next, I am going to select the PawnSensing component and in the Details tab scroll all the way down until you see the Events settings. Click on the + button for the On See Pawn:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-495cbf1c elementor-widget elementor-widget-image\" data-id=\"495cbf1c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-31-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d12853b1fdd968a9ded3699e53b19558ddd162f615d3ce7c65bf6619df89502c.jpeg\" alt=\"Img-31-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3475c415 elementor-widget elementor-widget-text-editor\" data-id=\"3475c415\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This will create the On See Pawn node in the Event Graph which is called every time the PawnSensing component sees an Actor in the game, and we can use this node to test if the Actor that the PawnSensing component sees is the player character, then we can make the enemy chase the player.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-da18f0c elementor-widget elementor-widget-heading\" data-id=\"da18f0c\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Sensing The Presence Of The Player<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4ed18194 elementor-widget elementor-widget-text-editor\" data-id=\"4ed18194\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Before we add the code that will sense the player\u2019s presence, we are going to create a function that is going to set the values in the Blackboard Blueprint when the player is visible or not.<\/div>\n<div><\/div>\n<div>Inside the BP_BB_EnemyAIController create a new function and name it SetCanSeePlayer:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-77c22a90 elementor-widget elementor-widget-image\" data-id=\"77c22a90\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-32-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/ca84ed18f4f46553f584767dceaf2e61a89a6269409628b9fc2ab3e0141dbd2d.jpeg\" alt=\"Img-32-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-286434d8 elementor-widget elementor-widget-text-editor\" data-id=\"286434d8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This function is going to have two parameters, a bool parameter called Can See Player, and an object parameter called Player Object. We will use these two parameters to denote if we see the player, and if we do, we will also pass a reference to the player actor:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-133a3b00 elementor-widget elementor-widget-image\" data-id=\"133a3b00\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-33-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a5a7db4db71daacefd02b91732049b37779fd2e4fa241aaaf808fa9b06252b79.jpeg\" alt=\"Img-33-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5c77d272 elementor-widget elementor-widget-text-editor\" data-id=\"5c77d272\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can copy the nodes from here:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5021047b elementor-widget elementor-widget-html\" data-id=\"5021047b\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/4yevya0d\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-77c9433f elementor-widget elementor-widget-text-editor\" data-id=\"77c9433f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now that we have the CanSeePlayer function, we can go back in the Event Graph tab and inside the On See Pawn event, the first thing we will do is test if the Pawn the enemy sees, is the player:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3e7b1583 elementor-widget elementor-widget-image\" data-id=\"3e7b1583\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-34-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/3fc504eface3621d9aacf8af0f25c6c48328fcc9106ffdce49da53d5c1d92748.jpeg\" alt=\"Img-34-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7a1f0891 elementor-widget elementor-widget-text-editor\" data-id=\"7a1f0891\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Since the project I am using is the Third Person template project, the player character uses the ThirdPersonCharacter Blueprint, that is why we are casting the Pawn parameter from the On See Pawn event to the ThirdPersonCharacter.<\/p>\n<p>If the cast succeeds, that means the enemy can see player, so we call the SetCanSeePlayer function passing true for the Can See Player parameter and passing ThirdPersonCharacter as the Player Object parameter.<\/p>\n<p>If you remember, we set the Sensing Interval for the PawnSensing component to 0.2, meaning every 0.2 seconds the PawnSensing component will be updated.<\/p>\n<p>This is important because currently we only have the logic to make the enemy see the player, but what if the player escapes the enemy\u2019s sight? In that case we need a logic that will inform us that now the enemy doesn\u2019t see the player anymore.<\/p>\n<p>For that I am going to use a function called Retriggerable Delay:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-474a493d elementor-widget elementor-widget-image\" data-id=\"474a493d\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-35-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/b970e4e432f5c115bf03bc554772ead40e69d9ddb96ea80422e14fbb45aac684.jpeg\" alt=\"Img-35-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-f8ba256 elementor-widget elementor-widget-text-editor\" data-id=\"f8ba256\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can copy the nodes from here:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6ac4fbd2 elementor-widget elementor-widget-html\" data-id=\"6ac4fbd2\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/vo3s2rj4\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-48e7cc51 elementor-widget elementor-widget-text-editor\" data-id=\"48e7cc51\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The Retriggerable Delay function has the same functionality as the Delay function, it will wait for the specified duration and after that it will continue executing.<\/p>\n<p>But the difference is, when you call the Retriggerable Delay function while it is counting down, it will reset the countdown and start from scratch.<\/p>\n<p>For that reason, for the Duration parameter I used the Sensing Interval value of the PawnSensing component, and I have multiplied it by 2, which means when the PawnSensing component sees the player, it will call the Retriggerable Delay function which will start its countdown.<\/p>\n<p>In the meantime, the PawnSensing component will continue to run, and if it sees the player again, it will call the Retriggerable Delay function which will reset its countdown, and since we set the Duration of the delay to be 2 times the Sensing Interval, that means if the PawnSensing component doesn\u2019t see the player for 0.4 seconds, then the Retriggerable Delay function will go through and it will call the SetCanSeePlayer to inform us that the enemy doesn\u2019t see the player anymore.<\/p>\n<p>So now that the enemy can see the player, we need to go in the BT_EnemyAI and add another sequence that will call the Move To Task and make the AI move to the Player Target:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-546b1291 elementor-widget elementor-widget-image\" data-id=\"546b1291\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-36-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/65c580e4424596478d7a8ba9fed1f91d7ee16c2855e9f8a3de3023a2ef62e60c.jpeg\" alt=\"Img-36-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-42271 elementor-widget elementor-widget-text-editor\" data-id=\"42271\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>As you can see I\u2019ve set the Sequence that will make the AI chase the player on the left side, because I want that task to be executed first. When that task is finished then the tree will move to the second Sequence.<\/p>\n<p>Don\u2019t forget to select the Move To node and for the Blackboard Key set the Player Target:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-ad6f8c5 elementor-widget elementor-widget-image\" data-id=\"ad6f8c5\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-37-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/f85d0d7a8c9e6f82c09314b8f443129d9a074a3d4e6bd39e76fb8edc9616910a.jpeg\" alt=\"Img-37-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1ff889f9 elementor-widget elementor-widget-text-editor\" data-id=\"1ff889f9\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We are still not finished because the current set up of the tree doesn\u2019t have any conditions that need to be checked before we perform each task. For example, if we don\u2019t have a reference to the player, we can\u2019t make the AI move to the player. For that we are going to add a decorator to the Sequence.<\/p>\n<p>To do that, simply Right Click on the Sequence and scroll down where it says Decorator:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5d23839c elementor-widget elementor-widget-image\" data-id=\"5d23839c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-38-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d2e00ff4b1e8db3d0cdb1f83a3107a708f078e6664545091de0b47c8e8d85881.jpeg\" alt=\"Img-38-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-d86acf4 elementor-widget elementor-widget-text-editor\" data-id=\"d86acf4\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The Decorator will act like a condition that needs to be met before the Sequence can run, and as you can see we have plenty of options to chose for the condition. We are going to select Blackboard condition:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-69b0ba22 elementor-widget elementor-widget-image\" data-id=\"69b0ba22\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-39-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/29f9115e367b34d3ef078eae3c81a41281f03f9764fe60ee76219a6a54e17321.jpeg\" alt=\"Img-39-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3e3e7458 elementor-widget elementor-widget-text-editor\" data-id=\"3e3e7458\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>When you do that, you will see a blue Blackboard Based Condition icon on the Sequence node:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-24718903 elementor-widget elementor-widget-image\" data-id=\"24718903\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-40-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/644b3a01ad9a7b8cc6d133314a1eff9791bec22cebe937843cad43d15278f6b4.jpeg\" alt=\"Img-40-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-18bc9c48 elementor-widget elementor-widget-text-editor\" data-id=\"18bc9c48\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>When you click on the Blackboard Based Condition in the Details tab you will see the Flow Control and Blackboard settings which is where you set the conditions for this Blackboard.<\/p>\n<p>To make the AI move to the player, we need to make sure that the enemy can see the player, which means the Can See Player value needs to be set:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-43b76068 elementor-widget elementor-widget-image\" data-id=\"43b76068\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-41-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/f04634f1386137029c99ae0dd3307254754aff03c3d669d54c5ab4ca27f893cd.jpeg\" alt=\"Img-41-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2224d0e0 elementor-widget elementor-widget-text-editor\" data-id=\"2224d0e0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>For the Key Query in the Blackboard setting select the Is Set, and for the Blackboard Key select Can See Player, which means Can See Player value needs to be set to true for this condition to evaluate.<\/p>\n<p>I\u2019ve also set the Observer aborts to Both in the Flow Control setting, which means that when the result changes, which is set in the Notify Observer setting for the Flow Control, then the tree will abort this Sequence(node), all of its children and any nodes to the right of this node(Sequence).<\/p>\n<p>For the Sequence that makes the enemy patrol, we are also going to add a Blackboard Decorator condition, and we are going to set the following settings for it:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1205ce19 elementor-widget elementor-widget-image\" data-id=\"1205ce19\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-42-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/8d4f5eaa76d4a309189b6e67d54cc068d5c0c43e31e2cfa51c49706f1625e6a7.jpeg\" alt=\"Img-42-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3974798b elementor-widget elementor-widget-text-editor\" data-id=\"3974798b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The condition for this Decorator is when the Can See Player value is not set, which you can see in the Blackboard setting. And in the Flow Control we set the Observer aborts to Self, which means when the value of Can See Player changes, the tree will abort this Sequence(node) and all of its children.<\/div>\n<div><\/div>\n<div>Let\u2019s run the game and test it out:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4d24ee8f elementor-aspect-ratio-169 elementor-widget elementor-widget-video\" data-id=\"4d24ee8f\" data-element_type=\"widget\" data-settings=\"{\" data-widget_type=\"video.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-wrapper elementor-fit-aspect-ratio elementor-open-inline\">\n<div class=\"elementor-video\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-259fe03 elementor-widget elementor-widget-text-editor\" data-id=\"259fe03\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>As soon as the enemy sees the player it starts moving towards him, and when it gets close to the player it stops moving. You can select the Move To node and in the Details tab change the Acceptable Radius to a higher value, this will make the enemy stop when the distance to player is equal to the value that you set:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-53bc0a18 elementor-widget elementor-widget-image\" data-id=\"53bc0a18\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-43-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/0535156949eccbeb85f668fd59e8ef1ede3e067b489f3bf2798ffb9004158070.jpeg\" alt=\"Img-43-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3b7c4e7c elementor-widget elementor-widget-text-editor\" data-id=\"3b7c4e7c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We also saw the execution flow of the Behavior Tree when the enemy was chasing the player and when the enemy was patrolling the level.<\/p>\n<p>You can always play with the AI settings for the PawnSensing component to make the enemy detect the player when he is far away or when he is close and so on.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4db0822d elementor-widget elementor-widget-heading\" data-id=\"4db0822d\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Attacking The Player<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-470310a1 elementor-widget elementor-widget-text-editor\" data-id=\"470310a1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The last step is to make the enemy attack the player. To make this work we need to create a new Task, name it Task_Attack and open it in the editor.<\/p>\n<p>In the Task_Attack we are going to override the Receive Execute AI event:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-43ca8536 elementor-widget elementor-widget-image\" data-id=\"43ca8536\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-44-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/9ed77fb50ac236d80ccd148cef1a1ab1d4c21cab18b2a19e43c7fe1769b265e0.jpeg\" alt=\"Img-44-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1ef704c6 elementor-widget elementor-widget-text-editor\" data-id=\"1ef704c6\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>From the Receive Execute AI event we are going to get a reference to BP_Enemy_BT and play the attack montage animation:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1136c096 elementor-widget elementor-widget-image\" data-id=\"1136c096\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-45-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/6015dcb191b89e7cb5498a71cafe5888a04711324a8e2f4f940f4b08b39aa634.jpeg\" alt=\"Img-45-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-45498159 elementor-widget elementor-widget-text-editor\" data-id=\"45498159\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>You can copy the nodes from here:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-404489ea elementor-widget elementor-widget-html\" data-id=\"404489ea\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/bzeohyyn\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2a0135cc elementor-widget elementor-widget-text-editor\" data-id=\"2a0135cc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The attack animation is already prepared just select it from the drop down list for the Play Montage node:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5a8ef86 elementor-widget elementor-widget-image\" data-id=\"5a8ef86\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-46-3.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/910a4c549ad151caf0d3a417d06d52043203ac942ab0242886ebb75f95dd91d7.jpeg\" alt=\"Img-46-3.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-71f73f6 elementor-widget elementor-widget-text-editor\" data-id=\"71f73f6\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Same as for the Task_GetRandomPointLocation, for the attack task we also need to call Finish Execute function to inform the tree that the task has finished executing.<\/p>\n<p>One thing that you will notice is that we are calling the Finish Execute function from the On Complete and On Interrupted events for the Play Montage function:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6cec4c35 elementor-widget elementor-widget-image\" data-id=\"6cec4c35\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-47-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/d20499577dbeebd6f2f952358e898be9ed748ca7991f98d3fdb6117b71bc63aa.jpeg\" alt=\"Img-47-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3c838bdf elementor-widget elementor-widget-text-editor\" data-id=\"3c838bdf\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>The On Completed part is clear, when the animation finishes playing, then we will call Finish Execute. As for the On Interrupted, it will be called when the animation is interrupted in any way and didn\u2019t finish playing.<\/p>\n<p>The reason why I am using this is because when the enemy tries to attack the player, the player can run away from the enemy, if that happens I want the enemy to abort the attack and continue chasing the player.<\/p>\n<p>Before we can see that in action, we need to call the Task_Attack in the Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-50cad3e4 elementor-widget elementor-widget-image\" data-id=\"50cad3e4\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-48-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/20584c97204f6c3cb45222417ff7ef41230fc674a168e36e13ef387a9ce22041.jpeg\" alt=\"Img-48-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1b7e6a90 elementor-widget elementor-widget-text-editor\" data-id=\"1b7e6a90\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>With that node, this is the final version of our Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5bdd3d61 elementor-widget elementor-widget-image\" data-id=\"5bdd3d61\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-49-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/462501f6ce4eeec7972657267dbd8a73f63a135a69add3c76c040acc06b02b82.jpeg\" alt=\"Img-49-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2f3710c5 elementor-widget elementor-widget-text-editor\" data-id=\"2f3710c5\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Before we can test this out, we need to do one more thing. Open the BP_BB_EnemyAIController, and in the My Blueprint tab create a new variable and make it type of BP_Enemy_BT:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1217c0f9 elementor-widget elementor-widget-image\" data-id=\"1217c0f9\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-50-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a39a93674211be4b420da06c914d6582029d288aa662993265dba2a2b9c817cf.jpeg\" alt=\"Img-50-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-460c5aed elementor-widget elementor-widget-text-editor\" data-id=\"460c5aed\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>In the BeginPlay after we start the Behavior Tree, we are going to get a reference to the BP_Enemy_BT:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-41dc09a5 elementor-widget elementor-widget-image\" data-id=\"41dc09a5\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-51-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/47680653da11c46e41b3a06233768cd258d4b102c5c578fbad72b745ea5d0414.jpeg\" alt=\"Img-51-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-55674419 elementor-widget elementor-widget-text-editor\" data-id=\"55674419\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can copy the nodes from here:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-38f876ad elementor-widget elementor-widget-html\" data-id=\"38f876ad\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/i_-xmjvt\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3d55359a elementor-widget elementor-widget-text-editor\" data-id=\"3d55359a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Next, in the On See Pawn event, after we call the Retriggerable Delay and the Set Can See Player function, we are going to use the Enemy BT REF to stop the attack animation:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6f90cca elementor-widget elementor-widget-image\" data-id=\"6f90cca\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-52-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/3a936004bd5e5f51f52be5ed8702a3dc6e79023d6e1506d8bc3d90e09926213c.jpeg\" alt=\"Img-52-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-61ff7ba5 elementor-widget elementor-widget-text-editor\" data-id=\"61ff7ba5\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can copy the nodes from here:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-318d4eb3 elementor-widget elementor-widget-html\" data-id=\"318d4eb3\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n<div class=\"elementor-widget-container\"><iframe src=\"https:\/\/blueprintue.com\/render\/h4rcis4y\/\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3135bfaa elementor-widget elementor-widget-text-editor\" data-id=\"3135bfaa\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We already know when the Retriggerable Delay function is called that means the enemy doesn\u2019t see the player, and when that happens we are also going to abort the attack animation because the player has escaped the enemy and we need to chase him again.<\/p>\n<p>When we abort the animation, this will also stop the Task_Attack because we are calling Finish Execute when the animation is interrupted in any way:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-449d01f0 elementor-widget elementor-widget-image\" data-id=\"449d01f0\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-53-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/3b30d03cfb2910d2b48615654fdef8c19fd46c3b8ec8fc0560b708cf48e1af25.jpeg\" alt=\"Img-53-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-40e7cfae elementor-widget elementor-widget-text-editor\" data-id=\"40e7cfae\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Let\u2019s test the game out and see the final outcome:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2a5b0daa elementor-aspect-ratio-169 elementor-widget elementor-widget-video\" data-id=\"2a5b0daa\" data-element_type=\"widget\" data-settings=\"{\" data-widget_type=\"video.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-wrapper elementor-fit-aspect-ratio elementor-open-inline\">\n<div class=\"elementor-video\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-69d94b77 elementor-widget elementor-widget-text-editor\" data-id=\"69d94b77\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now when the enemy reaches the player it attacks him, and when the player escapes the enemy in the middle of the attack the enemy stops the attack and starts patrolling.<\/p>\n<p>Of course, you can add additional logic to test if the player is in a certain distance away from the enemy so that the enemy will start chasing him instead of patrolling right away when the player is not in the enemy\u2019s sight anymore which will make a more realistic enemy AI.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2b013c6b elementor-widget elementor-widget-heading\" data-id=\"2b013c6b\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">C++ Enemy AI Controller<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-49d416f1 elementor-widget elementor-widget-text-editor\" data-id=\"49d416f1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Let us now create the C++ version of our Behavior Tree which starts with creating AI controller class. Inside the C++ Classes -&gt; Enemy_AI folder, Right Click -&gt; New C++ Class and make sure that it inherits from AIController:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-29567208 elementor-widget elementor-widget-image\" data-id=\"29567208\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-53-2.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/8b2d7a6890135ea26556f0edde26a1dc9b5f3d0e38a68597487798f42ebc1564.jpeg\" alt=\"Img-53-2.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-350ce0f7 elementor-widget elementor-widget-text-editor\" data-id=\"350ce0f7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Name the class BTAIController and click Create Class button. Before we proceed to code the AI behavior, we need to add public dependency module names so that we can use things like tasks, AI and navigation system in our code.<\/p>\n<p>Open the Enemy_AI.Build.cs file:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6f51d6d2 elementor-widget elementor-widget-image\" data-id=\"6f51d6d2\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-54.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/e2f94e547296f0f1c7e539c389081328ab2b91f2dcee3a7704c055670b769770.jpeg\" alt=\"Img-54.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4f263fa4 elementor-widget elementor-widget-text-editor\" data-id=\"4f263fa4\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Inside the file, this is how your PublicDependencyModuleNames should look like:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-870e97a elementor-widget elementor-widget-code-highlight\" data-id=\"870e97a\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<pre class=\"highlight-height language-csharp line-numbers\" data-line=\"\"><code class=\"language-csharp\">\n\t\t\t\t\t\n\t\t\t\t<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-71d34512 elementor-widget elementor-widget-text-editor\" data-id=\"71d34512\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>You can simply copy and paste this line of code instead of the same line you have in your project.<\/p>\n<p>Now that we have that out of the way, inside the BTAIController.h file, add the following lines of code:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-38e04726 elementor-widget elementor-widget-code-highlight\" data-id=\"38e04726\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<pre class=\"highlight-height language-csharp line-numbers\" data-line=\"\"><code class=\"language-csharp\">\n\t\t\t\t\t\n\t\t\t\t<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1deea6e elementor-widget elementor-widget-text-editor\" data-id=\"1deea6e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>If you remember, inside the BP_BB_EnemyAIController we got a reference to the Behavior Tree and then we run the Behavior Tree when the game starts, we are going to do the same thing except with C++.<\/p>\n<p>That is why we have the UBehaviorTree variable on line 5 in the code above. Now open BTAIController.cpp file and first we are going to add includes we need at the top of the file:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1ad22547 elementor-widget elementor-widget-code-highlight\" data-id=\"1ad22547\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<pre class=\"highlight-height language-csharp line-numbers\" data-line=\"\"><code class=\"language-csharp\">\n\t\t\t\t\t\n\t\t\t\t<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-12a6ee1a elementor-widget elementor-widget-text-editor\" data-id=\"12a6ee1a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Instead of going back and fort adding new includes whenever we need them, I am going to add all the includes we will need for our logic now.<\/p>\n<p>Run the Behavior Tree, we only need to call one line of code which we will do in the BeginPlay function:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3d3675d7 elementor-widget elementor-widget-code-highlight\" data-id=\"3d3675d7\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<pre class=\"highlight-height language-csharp line-numbers\" data-line=\"\"><code class=\"language-csharp\">\n\t\t\t\t\t\n\t\t\t\t<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7490270 elementor-widget elementor-widget-text-editor\" data-id=\"7490270\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Make sure that you compile the code by going under Build -&gt; Build solution or pressing CTRL + SHIFT + B in Visual Studio, or pressing the Compile button in Unreal Engine editor.<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3c38eceb elementor-widget elementor-widget-heading\" data-id=\"3c38eceb\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">C++ Behavior Tree<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1e284b8 elementor-widget elementor-widget-text-editor\" data-id=\"1e284b8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>To run the Behavior Tree we need to create a Blueprint out of our BTAIController. Inside the Content -&gt; Blueprints -&gt; AI, Right Click -&gt; Blueprint Class, make sure it inherits from BTAIController and name it BP_BB_EnemyAIController_CPP:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6a792278 elementor-widget elementor-widget-image\" data-id=\"6a792278\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-55.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/38203f314999a6ec5423de4a9223f5dfa8cee2e64ca1c367b8ffaae60daf22b7.jpeg\" alt=\"Img-55.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6e7a9ccc elementor-widget elementor-widget-text-editor\" data-id=\"6e7a9ccc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now open BP_Enemy_BT Blueprint located in Content -&gt; Blueprints folder and in the Components tab, select the BP_Enemy_BT(self), then in the Details tab under Pawn settings set the AI Controller Class to the new BP_BB_EnemyAIController_CPP Blueprint that we just created:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-42dd101 elementor-widget elementor-widget-image\" data-id=\"42dd101\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-56.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/b444f1b02d57e97d17a8c4c960c6d953469576b7297a1584234d64af00ea9b77.jpeg\" alt=\"Img-56.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-335e78a4 elementor-widget elementor-widget-text-editor\" data-id=\"335e78a4\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Next, we are going to create a new Behavior Tree that we are going to use with the C++ version of this tutorial. We are not going to create a C++ class for the Behavior Tree because there is no need to do that, instead we are going to create a new Behavior Tree by Right Click -&gt; Artificial Intelligence -&gt; Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-170135c4 elementor-widget elementor-widget-image\" data-id=\"170135c4\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-57.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/a59b17b9ce21d3ae87bb0a74d93df4edfdce5ccc5553dd3d7655317fa2c89021.jpeg\" alt=\"Img-57.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3230fb15 elementor-widget elementor-widget-text-editor\" data-id=\"3230fb15\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Name the new Behavior Tree BT_EnemyAI_CPP and then open it in the editor. For the Blackboard we are going to reuse the same Blackboard we used in the first part of this tutorial:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7f65d80e elementor-widget elementor-widget-image\" data-id=\"7f65d80e\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-58.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/e581c6969edadb59cf655ef495e5ce6f27fbc7f733bea0c24b01ba304eb8f283.jpeg\" alt=\"Img-58.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6d82d5b8 elementor-widget elementor-widget-text-editor\" data-id=\"6d82d5b8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>I am also going to reuse the random patrol logic that we created in the first example:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-32b0459d elementor-widget elementor-widget-image\" data-id=\"32b0459d\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-59.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/0f031aecc9ab69716d52f1e67da654ad49fdfdf608b2d928d84e0017084344c5.jpeg\" alt=\"Img-59.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-23905223 elementor-widget elementor-widget-text-editor\" data-id=\"23905223\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This is only to test if our code for running the Behavior Tree works, after that we will create our own random patrol logic using C++.<\/p>\n<p>Just make sure that you select the correct Blackboard key for both GetRandomLocationPoint and Move To task which is Random Patrol Location.<\/p>\n<p>Open BP_BB_EnemyAIController_CPP in the editor and in the Components tab select BP_BB_EnemyAIController_CPP(self), then in the Details tab for the Behavior Tree select BT_EnemyAI_CPP:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-42f8f49b elementor-widget elementor-widget-image\" data-id=\"42f8f49b\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-60.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/73ef63fa42984a7d1768b449995521e328c0e2345f5a553c769ac989e1d0a845.jpeg\" alt=\"Img-60.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2d67fbff elementor-widget elementor-widget-text-editor\" data-id=\"2d67fbff\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Compile and save the changes to the BP_BB_EnemyAIController_CPP Blueprint and now let\u2019s run the game to test it out:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2abbbcbc elementor-widget elementor-widget-heading\" data-id=\"2abbbcbc\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Creating Behavior Tasks With C++<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2de4abe0 elementor-widget elementor-widget-text-editor\" data-id=\"2de4abe0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now that we see that our initial set up is working, let us create our own task using C++ which is going to generate a random location point.<\/p>\n<p>Inside the C++ Classes -&gt; Enemy_AI folder, Right Click -&gt; New C++ Class. In the pop up window search for the BTTaskNode and inherit from it. Name the new class Task_GetRandomLocation_CPP and create the class:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-42fffe58 elementor-widget elementor-widget-image\" data-id=\"42fffe58\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-61.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/0c379bb1a6af2577285c1b8751d812e91be6bc05a4a9a1890f35175aa7331cc1.jpeg\" alt=\"Img-61.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-272ed015 elementor-widget elementor-widget-text-editor\" data-id=\"272ed015\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Open the Task_GetRandomLocation_CPP.h file and add the following lines of code:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5acce008 elementor-widget elementor-widget-text-editor\" data-id=\"5acce008\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>To generate a random location, we need to have a reference to the navigation system, that is why we need the variable on line 3. The function on line 7, is the override function which will inform the task that it needs to execute and it will be called automatically by the Behavior Tree.<\/p>\n<p>Now open the Task_GetRandomLocation_CPP.cpp file, and first we are going to add all includes we need:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-31f23433 elementor-widget elementor-widget-text-editor\" data-id=\"31f23433\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>And in the ExecuteTask function we are going to get a random location and inform the Blackboard component about the random location we generated:<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5dd87845 elementor-widget elementor-widget-text-editor\" data-id=\"5dd87845\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>First we get a reference to the navigation system on line 4. Then we use the navigation system to generate a random location in the radius of 15000 units from the player pawn on line 9.<\/div>\n<div><\/div>\n<div>What\u2019s important to note is that we need to return the result outcome for the ExecuteTask function. That is why we are testing if we have a reference to the navigation system on line 7, and if we don\u2019t have a reference to it we will return Failed as the task outcome on line 15.<\/div>\n<div><\/div>\n<div>But if we have a reference then we will generate a random location, then we will inform the Blackboard component about the random location we generated on line 19, and lastly we will return Succeeded as the task outcome on line 22.<\/div>\n<div><\/div>\n<div>Not that we are calling SetValueAsVector and we pass the name of the Blackboard Key parameter using FName. This is something that we need to specify so that the Blackboard component will know which value to set.<\/div>\n<div><\/div>\n<div>Now we can open BT_EnemyAI_CPP in the editor, and use our custom task:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3493f7de elementor-widget elementor-widget-image\" data-id=\"3493f7de\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-62.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/b0743e561a0e07f9b53c13bd92e7a69717702e8d6464311a965a019544d8f3ed.jpeg\" alt=\"Img-62.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-7eec3661 elementor-widget elementor-widget-text-editor\" data-id=\"7eec3661\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>This will replace the task node we created using Blueprints and it will use the C++ task node we just created.<\/p>\n<p>Let\u2019s run the game and test it out:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-353a062b elementor-widget elementor-widget-text-editor\" data-id=\"353a062b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>We will not notice any difference in the behavior of the enemy in comparison to the previous example, but now we see that the Behavior Tree is using the task node we created via C++ instead of the Blueprint one.<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-62488b9b elementor-widget elementor-widget-heading\" data-id=\"62488b9b\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Creating PawnSensing Component With C++<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-57050c8a elementor-widget elementor-widget-text-editor\" data-id=\"57050c8a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The next step is to sense or detect the player actor in the game and we are going to do that with the help of PawnSensing component.<\/div>\n<div><\/div>\n<div>Open BTAIController.h file and add the following lines below the UBehaviorTree variable declaration:<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3a9fc7b7 elementor-widget elementor-widget-text-editor\" data-id=\"3a9fc7b7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Now open BTAIController.cpp file and inside the constructor we are going to create the PawnSensing component:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4d70a174 elementor-widget elementor-widget-text-editor\" data-id=\"4d70a174\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Inside the BeginPlay we are going to bind the OnSeePawn function to the OnSeePawn event of the PawnSensing component<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-60efc56 elementor-widget elementor-widget-text-editor\" data-id=\"60efc56\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Inside the OnSeePawn function we are going to test if the pawn that the PawnSensing component sees is the player, if that is the case we will inform the Behavior Tree that we see the player and we will pass the player\u2019s reference to the Behavior Tree:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-817412c elementor-widget elementor-widget-text-editor\" data-id=\"817412c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>The SetCanSeePlayer function contains the code that will inform the Behavior Tree, or better yet, the Blackboard component, that the enemy sees the player:<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-64a150a6 elementor-widget elementor-widget-text-editor\" data-id=\"64a150a6\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>When the SeePlayer parameter is true, we will pass that parameter, or the value of that parameter to the Blackboard component using FName(\u201cCan See Player\u201d) which refers to the same Blackboard key inside the Blackboard component.<\/div>\n<div><\/div>\n<div>We do the same thing with FName(\u201cPlayer Target\u201d) when we pass a reference to the player pawn.<\/div>\n<div><\/div>\n<div>If you remember in the Behavior Tree Blueprint version, we are using Can See Player as the parameter for the Blackboard decorator, which servers as the condition for the Sequence, and we use the Player Target as the parameter for the Move To node which will make the enemy move towards the player pawn:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1c27bd6 elementor-widget elementor-widget-image\" data-id=\"1c27bd6\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-63.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/fb42ebd9fd671f3df14a6067490a7ca6ed689da0b83b0cbc2458b41e9b23df3e.jpeg\" alt=\"Img-63.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2aa44cd4 elementor-widget elementor-widget-text-editor\" data-id=\"2aa44cd4\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>In the code example above, we are doing the same thing, except using C++.<\/p>\n<p>And when the SeePlayer parameter is false, then we only pass that value to the Blackboard component without the player pawn, because if the enemy can\u2019t see the player, it will not run towards him and it will not attack him, thus, we don\u2019t need to pass player pawn to the Blackboard component.<\/p>\n<p>We also need to stop the enemy from attacking the player if the attack is currently under way, and we do that on lines 16 and 17 in the code example above.<\/p>\n<p>First we get a reference to the enemy actor, then we stop his montage animations that are currently playing by calling StopAllMontages(0) same as what we did with the Blueprint version of the Behavior Tree.<\/p>\n<p>The 0 parameter in the StopAllMontages functions will make the enemy animation Blueprint stop the attack animation and it will play another animation based on the condition we set for the animations.<\/p>\n<p>The reason why I am mentioning this is if you set 1 as the parameter, then the enemy will finish the attack animation and then play other animations which is not the behavior we want.<\/p>\n<p>Lastly, we need to code the RunRetriggeranbleTimer function, so add the following lines of code:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4105d365 elementor-widget elementor-widget-text-editor\" data-id=\"4105d365\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Inside the OnSeePawn function we are calling the RunRetriggerableTimer function. With this function we are building the same set up we created in BP_BB_EnemyAIController Blueprint version:<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-78cd1904 elementor-widget elementor-widget-image\" data-id=\"78cd1904\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-64.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/f25d13ae60b80bbcdd60f44dd641fa9ce01a409ff8b43ac79b75725d360c2858.jpeg\" alt=\"Img-64.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-44c5de2f elementor-widget elementor-widget-text-editor\" data-id=\"44c5de2f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<div>Essentially, every time we call the RunRetriggeranbleTimer is called, it will reset its countdown which we are doing on line 3 in the code above. Then it will use the FunctionDelegate variable and its BindUFunction to pass the SetCanSeePlayer function and the two parameters.<\/div>\n<div><\/div>\n<div>Since we are passing false as the parameter meaning the enemy can\u2019t see the player, I am using GetPawn function as the second parameter.<\/div>\n<div><\/div>\n<div>Then we call the timer manager to set the timer. We are using SensingInterval * 2 as the parameter for the wait time, because we are wait twice as long it takes the PawnSensing component to refresh itself, which means if the PawnSensing component doesn\u2019t see the player after SensingInterval \/ 2 seconds, then the timer will execute.<\/div>\n<div><\/div>\n<div>Before we proceed, I am going to leave the BTAIController.h and .cpp file below as a reference, in case you want to copy paste the code or compare it to your own.<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5db5c8fd elementor-widget elementor-widget-text-editor\" data-id=\"5db5c8fd\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>Make sure that you compile the code and then open BP_BB_EnemyAIController_CPP in the editor. You will see the Pawn Sensing component under the Components tab, select it and in the Details tab change the Sensing Inverval value to 0.2 and for the Peripheral Vision Angle set the value 60:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-29e732fe elementor-widget elementor-widget-image\" data-id=\"29e732fe\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-65.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/8d3610ffa28b9ee913bf79e8f3fd62db8dda99c62ace2dfad3e59cba5dcc844a.jpeg\" alt=\"Img-65.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6433400 elementor-widget elementor-widget-text-editor\" data-id=\"6433400\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We already explained what these options mean when we edited the Pawn Sensing component in the Blueprint example.<\/p>\n<p>Now open the BT_EnemyAI_CPP in the editor, and the last setup is pretty much the same as the one we had in the Blueprint version:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-3f38af5b elementor-widget elementor-widget-image\" data-id=\"3f38af5b\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-66.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/34cb23f15f03a4b428bafb045841f9a07e8d420641f766c77f7bb3550557d3bf.jpeg\" alt=\"Img-66.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1db6c156 elementor-widget elementor-widget-text-editor\" data-id=\"1db6c156\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>On the left side we have a Sequence node that will make the enemy move towards the player and attack him. The sequence has a Decorator which has the Can See Player as the condition with the Key Query Is Set, and it aborts both nodes when the result changes:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-72dbb80a elementor-widget elementor-widget-image\" data-id=\"72dbb80a\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-67.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/2ae3942aa618e12844d5411f2de5a6937d8522c946ad0f488831a8a6768da3e2.jpeg\" alt=\"Img-67.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1bcd5c9a elementor-widget elementor-widget-text-editor\" data-id=\"1bcd5c9a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>We already tested the Sequence node on the right which makes the enemy patrol, the only thing that changed is that we added a Decorator which also has the Can See Player as the condition with Key Query Is Not Set:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-e393833 elementor-widget elementor-widget-image\" data-id=\"e393833\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img decoding=\"async\" title=\"Img-68.jpg\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/31d705d2604e9f300a6fda0314d1cae33d28bc7723dfa1b7f22a8db006563c6d\/562ac51b4ba009268fb2a10d2e0a196d0590b9b8d96be7b48579ebff2874fc43.jpeg\" alt=\"Img-68.jpg\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-1f723508 elementor-widget elementor-widget-text-editor\" data-id=\"1f723508\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p>One thing that you will notice is that I have reused the Task_Attack node we created via Blueprints. The reason why I didn\u2019t create an attack task with C++ is, it is complicated to get all the references we need such as the enemy and make him attack and so on, it is a lot easier to do this with Blueprints that is why I reused that node.<\/p>\n<p>And besides, Unreal Engine is all about combining Blueprints and C++, and a lot of games are created purely in Blueprints so throw that Blueprints are not optimized mentality out of the window.<\/p>\n<p>The last thing that is left for us to do is to test the game and see the outcome:<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-40a3492c elementor-aspect-ratio-169 elementor-widget elementor-widget-video\" data-id=\"40a3492c\" data-element_type=\"widget\" data-settings=\"{\" data-widget_type=\"video.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-wrapper elementor-fit-aspect-ratio elementor-open-inline\">\n<div class=\"elementor-video\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post about enemy AI we created enemy patrol, chase and attack behavior using Blueprints and C++. In this post we are going to do the same but we are going to use behavior trees and sensing. Download Assets And Complete Project For This Tutorial Unity Enemy AI Tutorial Buy now! To follow<\/p>\n","protected":false},"author":1,"featured_media":594,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/438"}],"collection":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=438"}],"version-history":[{"count":2,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/438\/revisions"}],"predecessor-version":[{"id":595,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/438\/revisions\/595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/media\/594"}],"wp:attachment":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}