# Help with behavior trees and spawning enemies?

**URL:** <https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599>\
**Category:** Character & Animation\
**Tags:** Behavior-Tree, decorator, unreal-engine, functions, question\
**Created:** [January 2, 2018, 8:42pm UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599 "2018-01-02T20:42:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sounderdisc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sounderdisc](https://forums.unrealengine.com/u/sounderdisc)\
**Post date:** [January 2, 2018, 8:42pm UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/1 "2018-01-02T20:42:53Z")

</div>

I am trying to make the enemies in my game walk between a set of locations from an array when the blackboard booleans YellowAlert and CanSeePlayer are false, run to the player’s last observed location when YellowAlert is true and CanSeePlayer is false, and attack the player when CanSeePlayer is true regardless of the value of YellowAlert. I also want to spawn enemies at random locations from the same array of locations that they walk between.

I know how to do most of this, but I don’t see any blackboard decorator that goes off of two booleans, don’t know how to run a task on a timer in the behavior tree, and couldn’t find any functions for moving at a certain speed and spawning actors into levels at specified locations.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Vizgin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vizgin](https://forums.unrealengine.com/u/Vizgin)\
**Post date:** [January 2, 2018, 9:11pm UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/2 "2018-01-02T21:11:54Z")

</div>

Hi,

The blackboard decorator only accepts one boolean, however, you can easily put two decorators on the same **Selector** or **Sequence** node. You can also use one boolean variable that stores the result of **AND** operation between your other two booleans (though I don’t recommend this as it might make things messy).

To run a **Task** with a timer at a different rate you have 2 options: Using **Services** or setting a timer in the **Task** itself. **Services** work similar to **Tasks**. They start executing if your decorators are all true and they provide you with an option to change the ticking interval once you add them to your behavior tree. You can override their **Event Receive Tick** and implement the logic you want.

**Tasks** , on the other hand, don’t give you an option to change their ticking interval, however, there are easy ways to make them tick in the interval you want. Instead of using **Receive Tick** event, you would use **Receive Execute** and bind your functionalities to a custom timer. **Receive Execute** works just like **Begin Play** and is called once when the Task gets activated. As a good practice, I recommend that you store this timer handle and call **Clear and Invalidate** on it once your task is finished executing by either using the **Receive Abort** event or another delegate that you have set somewhere else.

This official tutorial [here](https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/SpawnAndDestroyActors/Blueprints/) might help you understand how to spawn actors using blueprints.

If your AI’s are based on **Character** , then you can use the inherited **Movement** component to change their **Max Speed**. if you’re inside your own character BP, simply drag the **Movement** component from the **Components** list to get a reference to it, click and drag from that reference and type “max speed”. You’ll be able to set it and change it to your liking even during gameplay and runtime.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![sounderdisc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sounderdisc](https://forums.unrealengine.com/u/sounderdisc)\
**Post date:** [January 3, 2018, 3:37am UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/3 "2018-01-03T03:37:40Z")

</div>

I was able to spawn everything in as intended, but the enemies’ behavior tree says “Inactive” while playing in the editor and they just stand there. I thought the AI Controller wasn’t executing its begin play which runs the behavior tree, so I made a function to begin it and called it within the controlled pawn’s begin play, construction script, and event tick, none of which made the behavior tree actually do something. The print statements attached as the first nodes did execute. When I place enemies into the level normally instead of through the “SpawnActorFromClass” function they work correctly. What could be the problem?

 ![225770-charactereventscript.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/7/7/5776fe51a702451ba9ae5ddb918d60a9d41d452d.png)

---

<div class="post-metadata">

**Author:** ![Vizgin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vizgin](https://forums.unrealengine.com/u/Vizgin)\
**Post date:** [January 3, 2018, 3:46am UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/4 "2018-01-03T03:46:06Z")

</div>

Hmm… Make sure that when you spawn your enemies you specify the controller class that they should use. In the AIController, you can try overriding **On Posses** event and run the Behavior Tree through that. If none works, please post a screenshot of your AIController and the Behavior Tree so that I can better assist you.

---

<div class="post-metadata">

**Author:** ![Vizgin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vizgin](https://forums.unrealengine.com/u/Vizgin)\
**Post date:** [January 3, 2018, 4:10am UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/5 "2018-01-03T04:10:05Z")

</div>

Don’t make it overcomplicated and don’t try to activate it in your Pawn’s constructor (It might not work if the pawn is still being constructed). Remove all of these and simply use BeginPlay or OnPosses in the AIController to run the behavior tree. But first, make sure that these events fire by printing a debug message. If they do, then check the behavior tree itself. It might be failing at the first decorator (if you have any) and hence not executing at all! If the issue still persists then please post a picture of your behavior tree itself along with the updated AIController blueprint.

---

<div class="post-metadata">

**Author:** ![sounderdisc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sounderdisc](https://forums.unrealengine.com/u/sounderdisc)\
**Post date:** [January 3, 2018, 4:38am UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/6 "2018-01-03T04:38:37Z")

</div>

I didn’t see an input on the SpawnActorFromClass to specify the AI Controller, but I did double check in the pawn’s blueprint

 ![225776-behaviortree.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/f/c/ffca90bd43ee650069d0bd358bdaa7f55e9f34d1.png)

that the correct controller is being used. Neither on posses nor begin play fires. Nothing prints. I unhooked my unsuccessful bypass in the pawn’s script so it doesn’t do anything on construction. Attached are pictures of the behavior tree and AI Controller as well as the script that spawns the enemies.

---

<div class="post-metadata">

**Author:** ![Vizgin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vizgin](https://forums.unrealengine.com/u/Vizgin)\
**Post date:** [January 3, 2018, 4:55am UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/7 "2018-01-03T04:55:36Z")

</div>

Alright. the issue here is that although you’re spawning your AI character, you’re not spawning a controller for it. Add these two nodes to your spawn script and verify that both BeginPlay and OnPossess fire.

 ![225777-spawning-aicontroller.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/d/3/2d32aadeb6d5c08ddad4100bf065661b1e2167b6.png)

---

<div class="post-metadata">

**Author:** ![Chronoknight](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chronoknight/32/621011_2.png) [@Chronoknight](https://forums.unrealengine.com/u/Chronoknight)\
**Post date:** [February 23, 2023, 5:07pm UTC](https://forums.unrealengine.com/t/help-with-behavior-trees-and-spawning-enemies/413599/8 "2023-02-23T17:07:46Z")

</div>

Theres Event receive execute  
and Event receive execute AI  
😃  
Tricky
