# Behavior

## Status setup

Now that we know, perception is working. We need to get our status system set up. \
In the newly created AI controller, you will find  Status\_0 and GetAllStatusOptions functions \
Duplicate the Status\_0 function twice and name it Status\_Alert and Status\_SearchForTargets.

{% hint style="info" %}
To duplicate a function, you can use the CTRL+D shortcut.
{% endhint %}

\
![](https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FFzJDEb3upSnz5MJOoLm4%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media\&token=b81355d5-52ed-4ba8-a058-c599e6365bd1)\\

In Status\_SearchForTargets. Set float value in select float node to 2 on A.

Call function "Get Targets Heard In memory"> "Arraylength">"MoreThan0" and connect it to select node\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2Fia1uVgvpkP307DjIjQ7y%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=8d685d1f-049f-4d01-b3f2-153d668cff11" alt=""><figcaption></figcaption></figure>

Select gameplay tag for S\_Status Structure

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FF1w41XE3LWZGxLRiWPRZ%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=2de8e750-f446-431b-a266-abb7892e2c27" alt=""><figcaption></figcaption></figure>

For Status\_Alert, add "PriorityTarget">"IsValid" and connect to select float. Select the gameplay tag. Set float value to 3 for A on select float node.\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FUZl9OG1ExiKLZW5aiirT%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=6de75f7f-f06f-4eff-a17a-f82cdbb9a252" alt=""><figcaption></figcaption></figure>

Now let's go back to Get all Status options function

And add calls to those functions and connect them to make array function. \\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FMZM22x8XRIFTe38NKglS%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=f901ee22-19a4-4c13-af0e-d28e2fa14877" alt=""><figcaption></figcaption></figure>

## Behavior tree setup

In a controller, select the Behavior tree with your system's name.

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FG5XC1pGayHR95ZPAGvgw%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=b02454b1-5cc5-4507-93c1-5dfb68a99ab0" alt=""><figcaption></figcaption></figure>

In the Behavior tree asset, select Blackboard with your system's name.

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FviCpHMM6Zp5zCidHHki0%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=438ff736-9fa6-4c80-8356-43cec80fad14" alt=""><figcaption></figcaption></figure>

### Fire weapon service

With robust weapon systems, you would not need to do a lot here. But since we have simple line trace instead of our weapon, we need to make sure AI will not rapid-fire.&#x20;

[Create ](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/ArtificialIntelligence/BehaviorTrees/BehaviorTreeNodeReference/BehaviorTreeNodeReferenceServices/)a new Behavior tree service and name it BTService\_Fire \
In this service, overwrite **Receive Activation AI** and **Receive Deactivation AI**

If you are not using the [character ](https://squad-a.gitbook.io/combat-ai-framework/building-custom-ai/set-up-character)we set up, you will have to figure this part out yourself. For those who were following along, create a cast for your character and save the result in a variable like this:

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FiqPNV1B1nLOEsZurULIL%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=1f15dca0-4f98-4c7a-b9d1-2bd992d9412e" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For the actual project, you will need to replace the cast with an interface.&#x20;
{% endhint %}

Next, create a timer event, save its handle, and create the event of it just like this:\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2F8EHh6qvFvEIg8i8brJYV%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=f51c7b7b-da8a-418b-b80c-1ee0da81737d" alt=""><figcaption></figcaption></figure>

Time float would regulate the time between shots. \
On Event, Receive Deactivation AI, grab the Timer handle, and deactivate the timer.\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FrQDne3I84zz7RgnoYAby%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=75832db0-cf8a-4e15-abcb-9c97b1c716a7" alt=""><figcaption></figcaption></figure>

### Tree

In the Behavior tree asset, add a selector node, and connect it to the root. Add two sequence nodes and add the BTD\_GameplayTag decorator on top of each sequence. \\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FeWrRHnrYjOr6zJRArj2V%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=4e46473d-0daf-4f6d-b629-11bff5b5f601" alt=""><figcaption></figcaption></figure>

In Decorator properties, select tag AI.Status.Alert for left decorator and AI.Status.SearchingForEnemy\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FdxfXSsyiwgKz4kgQumEZ%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=aff1add7-7b8d-40b9-a41f-390bd3affd41" alt=""><figcaption></figcaption></figure>

For the left sequence, add the "Fire" service&#x20;

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2F6qPhUvSOhtOs34DaCAms%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=8157e546-992f-404b-934a-8fe3abe283d3" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Right Click on a node to open the context menu
{% endhint %}

{% hint style="warning" %}
Select Decorator aborts as "self". Click on decorator and find this ![](https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FdpEYDAMhb0GF60lGSM5W%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media\&token=4418ea7d-43f1-4f16-bc6d-d58da46900db)\
Otherwise AI will keep going in direction of gunshot even after spotted player
{% endhint %}

In addition, add default focus service and select Active target key select then connect "move to" node to left sequence. \
For the right sequence, connect "move to" and select the sound location as a key.

\\

<figure><img src="https://1732704790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8NeAffCvPebTKR2CvQJ0%2Fuploads%2FuAr5H5MRJYrNtKbVzZBI%2F%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%BD%D1%8F.png?alt=media&#x26;token=188a453d-5f12-4d1c-97e8-46fd3d4d0036" alt=""><figcaption><p>Final result for behavior tree</p></figcaption></figure>

## Testing

{% hint style="info" %}
Make sure there is nav mesh on the level before testing
{% endhint %}

As expected AI:

* [x] Fight's enemy&#x20;
* [x] searches for the enemy If it does not have an enemy in sight.&#x20;

{% embed url="<https://youtu.be/yMsp30yDd9k>" %}

It will also react to damage if you have a health system.&#x20;
