# AI Behavior Tree

**URL:** <https://forums.unrealengine.com/t/ai-behavior-tree/1318604>\
**Category:** Blueprint\
**Tags:** ue4-editor, unreal-engine, question, Blueprint\
**Created:** [October 1, 2023, 10:48am UTC](https://forums.unrealengine.com/t/ai-behavior-tree/1318604 "2023-10-01T10:48:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GlasyaLabolasHun1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GlasyaLabolasHun1](https://forums.unrealengine.com/u/GlasyaLabolasHun1)\
**Post date:** [October 1, 2023, 10:48am UTC](https://forums.unrealengine.com/t/ai-behavior-tree/1318604/1 "2023-10-01T10:48:11Z")

</div>

I have created an enemy Ai controlled by a behavior tree.

 ![treeb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/e/76e69c22595e58806044673f041604a7d4b75a55.png)

Basically it patrol randomly but when it see the player run towards and attack it, it works fine when I have one enemy on the level and sometimes with multiple enemies, but most of the time I got an error message with multiple enemies.

 ![messa](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/d/2/5d29f3b7cf033a93a70f33ed23ae98ac7036c8c4.png)

The enemy has a sphere collision which if the player walks in it can see the player.

 ![enemy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/4/6/d46ad8cf281811ef10c7942352b4aea9968052fa.png)

I think the problem is with the SeePlayer boolean because it tries to set it multiple times but I can’t fix it.  
I tried to add a Branch / Is Valid nodes to first check if the SeePlayer already set or not before it set it, but still got the message.

---

<div class="post-metadata">

**Author:** ![isikdev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/isikdev/32/991020_2.png) [@isikdev](https://forums.unrealengine.com/u/isikdev)\
**Post date:** [October 2, 2023, 9:12pm UTC](https://forums.unrealengine.com/t/ai-behavior-tree/1318604/2 "2023-10-02T21:12:25Z")

</div>

Hey there!

Here is what I’d do to receive the results that you want:

First of all, I like to use multiple Behavior Trees for differing AI States (for e.g - Roaming/Attacking/etc.). So, I’d go ahead and create an Enumerator containing these AI States.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/7/6/7767bf562e08377114f2a10b67a2f3cf180bb7cf.png)

Then create a Blueprint Interface called BPI\_ChangeAIState, which will contain just one function. Add the enumerator we’ve created to the INPUT of the function:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/4/0/640fad7b654a98f7b0c6e5f6796ee51ed35d7e5e.png)

To continue, create an AI Controller and assign it to the AI character:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/1/4/214192fcf0de5d366a7fa17a374942643e2dc89a.png)

Inside the AI Controller, go to Class Settings and assign the Blueprint Interface:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/0/4/70424c68136893f095314155624e6a79a7d96475.png)

Then go ahead and create your Behavior Trees & Blackboard. Be sure that the Blackboard is selected correctly in the Behavior Trees. In this case, I’ve created 2 behavior trees - one for Roaming and one for Attacking:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/1/8/918db8a18efc544c70e3125e27b358fcdf172dec.png)

In the AI Controller, create a variable - “CurrentAIState” and set its variable type to be the enumerator we’ve created:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/c/0/1c074359e6b77814fb62546ee081735d32e5e6ff.png)

Under the functions tab, locate Interfaces and implement the “Change AI State” function by right clicking. Then, you can do something like this to run multiple trees according to the AI’s state:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/a/1/da1a61675a5dc93eccd7a0a7bb4dd457ba9b59d2.png)

Next, hop into the Blackboard and create 2 variables - “PlayerTarget” Object & “RandomLocation” Vector:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/6/7/d67712b05b0ed49c3cb0516d627d94c30d92b941.png)

Set the “PlayerTarget” object variable base class to “Actor”:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/6/4/1643fa358a230bc7c55e756540e40bb4486783c9.png)

Now that we’ve got that part done, we can now add an AIPerception Component to the character itself:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/1/4/0140c29e488286e25cc8cf6115358874f1fd58e2.png)

Next, setup the component like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/e/d/6ed95f43e6d221dd4b95e0d1b976210c5eb46bc6.png)

And then right-click the AIPerception component and override “On Perception Updated”. Here, you can check what targets the AI sees, and accordingly, set blackboard values (in this case, we set “PlayerTarget”):

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/7/c/17c58ab9e63ff4254ca66ba394837a3223b6b88c.png)

Quickly whip out some basic logic in the Roaming behavior tree:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/2/1/721006b6b9ba7bec5bfd16c8abb5130a441921ca.png)

The BTT\_ChangeAIState task (create a variable “NewAIState” which is instance editable with the type of the enumerator we made at the first step):

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/c/c/2cc06bbdb295287d6e09007e894b906d6c3e029a.png)

The BTT\_GetRandomPoint task (promote the radius to a variable & also create a blackboard key selector variable which is instance editable):

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/0/8/808237b8ab098fc05c6ce13fa20892fd1753c2f8.png)

Next up, the Attacking behavior tree:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/8/5/38567264f40be61bcdfa241b4b3a9d45baac0063.png)

This should work properly! You can tweak out the AIPerception settings to your liking. Also in the “OnPerceptionUpdated” event, you can check if the perceived actor has a specific tag (such as “Player”) before setting the blackboard value for more accurate results.

Also, you can use a timed event to clear the “PlayerTarget” blackboard value when the AI loses sight, so it feels a bit more smarter 🙂

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/c/79c07d7857c4bc272d94ae336c2a396bf3bfaf45.png)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![GlasyaLabolasHun1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GlasyaLabolasHun1](https://forums.unrealengine.com/u/GlasyaLabolasHun1)\
**Post date:** [October 17, 2023, 2:07pm UTC](https://forums.unrealengine.com/t/ai-behavior-tree/1318604/3 "2023-10-17T14:07:18Z")

</div>

Sorry for the late answer. It works as I wanted. Thank you for the help.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 16, 2023, 2:07pm UTC](https://forums.unrealengine.com/t/ai-behavior-tree/1318604/4 "2023-11-16T14:07:37Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
