Behavior tree roadblock

So, I’m trying to work on implementing behavior trees for the sake of flexibility in my enemy code. I’ve been trying to follow a tutorial, and it was going pretty well, however since I’ve gotten to the part to make it automatically switch states, it…doesn’t work. Instead of chasing the player upon spotting them, it just freezes up. It won’t let me post the video of what is happening, but it looks like it might be doing this thing where upon detecting the player it keeps rapidly trying to refresh the “attack” section, even once out of sight again, and I’m struggling to figure out what might be causing it. If anyone has any insight as to what I might’ve been doing wrong please help!

All the function the sight nodes are linking up to does is switch states, in case thats relevant too.

I have an image of the behavior tree itself too, but it won’t let me post that here as well.

Hey @Snarkyspider how are you?

I’ve been trying to figure out what’s going on there but I’d need a lot more info to understand and diagnose you problem.

Could you share your Behavior Tree, the related AI Controller, and all you tasks?

In addition, it would be great if you can tell me which tutorial are you following, so I can replicate exactly that!

I’ll be waiting for your answer!

Of course, yeah! I have to make multiple responses to make sure I can send everything, but here’s the part I was on

The attack task

The switcher

The other switcher

The sensor code

This is also whats supposed to make it actually switch on sight

Hello @Snarkyspider ,

I’ll leave you some corrections I noticed based on the images you shared, which should help your character work correctly when switching states.

First of all, in your Behavior Tree you’re missing the FocusTarget and ClearFocus tasks. These are important so the enemy knows who to look at while chasing or attacking, and to properly clear the focus when leaving the combat state. Without this, the behavior may not work correctly when switching branches.
BTT_FocusTarget


BTT_ClearFocus

Additionally, in the CombatState decorator inside the Blackboard, make sure to set Observer Aborts to Self. In this case it’s better to use Self instead of Lower Priority because what you need is to immediately abort the active branch when the state changes, without affecting other branches of the main selector or forcing unnecessary reevaluations. This prevents previous tasks from continuing to run or constantly restarting while the state has already changed.

In your Attack Task, the delay you added at the end is not necessary. Finish Execute should be set to Success and have ticking enabled, so the Behavior Tree can continue correctly without constantly restarting the task.

Also, in the Attack switch you’re missing an input of type Actor, which basically defines who the enemy is attacking. That same actor should be set using Set Value as Object in the Blackboard, which was previously in the Event Graph and can now be removed from there since it should be handled by the state logic.

The passive state is set correctly

In the Switch on Sight, you’re missing all the code related to the complete detection logic, since right now it’s not correctly handling the flow when detecting or losing sight of the player.

Lastly, in On Perception Updated, inside Handle Sense Sight, make sure you’re correctly passing the Array Element to the Actor parameter of Handle Sense Sight.

With these adjustments, the Behavior Tree should stop constantly restarting and the enemy should switch states in a stable way.
If any other node is still failing, you can send it over and we can check that everything is correctly set up.

Also make sure that your Blackboard keys have exactly the same name everywhere they are set or read. Even a very small change is enough to make the Behavior Tree not work as expected, so I recommend using Ctrl+C / Ctrl+V when referencing key names to avoid typing mistakes.

If this is your first time working with Behavior Trees, a good practice is to follow the tutorial step by step, without adding or removing extra logic at the beginning. Once everything is working correctly, you can start removing or modifying the parts you don’t need. This helps a lot when isolating issues.

I’m also leaving you this document, which might be useful if you want to learn a bit more about Behavior Trees and how they work.

Hope it helps and happy coding!

Oh goodness- thank you so much for pointing out everything, I’ll be sure to try it in the morning since its a bit late for me right now but I didnt realize I missed so many steps somehow ^^,! Again, thank you for your time though!

1 Like

I did just go over and try to correct everything as you showed, but for some reason it’s still doing this…it was doing something similar before but now I should have everything in place. I even checked multiple times, I’ll probably check again just in case though.

Hello again,
Don’t worry, these kinds of things happen all the time when you’re learning a new tool. It’s completely normal to miss small details, especially with Behavior Trees, where everything depends on references being properly connected and written correctly.

In this case, the problem is in the Move To node: the Target is ending up as None.

This usually happens because the Blackboard key name isn’t properly assigned when you set the attack state. Check inside your SetStateAsAttack and make sure the variable is using the exact correct KeyName. In your case it should be Player, and in mine it was AttackTarget.

If the name doesn’t match exactly, the Blackboard won’t store the reference and Move To won’t have anything to follow, which is why it gets stuck.

With that, the issue of it getting stuck on Move To should be resolved.

OH oh my goodness that makes SO much sense, thank you so much! It seems like its working now :slight_smile:

1 Like