Download

BT Selector - switching between them without leaving the sequence

I have a feeling I either don’t understand this or I have something wrong but I’m trying to flesh out a simple patrol/investigate/chase behaviour tree using AIPerception on my AI

I have a function on the AI controller to run when AI perception is updated - it’ll first decide if the sight trigger was the player, then if so it’ll set a couple of blackboard keys (IsInvestigating is the main one). I also have a branch to either set or unset a blackboard key (targetLocationActor) depending on whether the actor is or isn’t seen

My selector is underneath a simple alert sequence which is entered after I fail out of the patrol sequence (by setting IsInvestigating to true) and then within this sequence I have a selector with two routes. Route 1 is if targetLocationActor IS set (set by the AIPerception event) and route 2 is if targetLocationActor ISN’T set (again set by AIPerception)

I have no issues getting into the alert sequence, and getting into route 1. If the enemy lose sight then I have no issues getting to route 2. If I then let it finish naturally (it’s a loop event) then the next behaviour sequence fires and eventually I go back to the beginning. HOWEVER if I get into the alert sequence and have targetLocationActor set (so I’m in selector route 1) and then the enemy lose sight of me (into route 2) if I then am seen again I expect that I’d auto revert to route 1 again. Since all other conditions to be in the sequence are still set I shouldn’t be leaving it, but as the AIPerception has done what it did at the beginning (setting targetLocationActor) then selector route 2 should fail but selector route 1 should pick up again

It doesn’t. If the AI see’s me again after losing sight the whole thing falls over and reverts straight back to my patrol route. I don’t understand why

Am I missing something in how selectors work? Even if I am, bearing in mind that on AIPerception updated the first time I get into the correct BT sequence and down the right BT selector, even if the whole BT where to kill the sequence and try to loop through if I’m re-spotted, surely all the conditions that caused me to get into the alert sequence the first time should all still be met exactly the second time

Does all that make sense? Because the logic behind why this isn’t work doesn’t make sense to me at the moment

Hi, can you show some images of your behavior tree?

Generally speaking keep in mind that behavior trees are good for sequential behavior (e. g. the patrol behavior itself), but bad for cyclic one (like switching between patrol/investigate/chase). So trying to do cyclic behavior (the switching logic between your behaviors) with then will be more complicated than needed.

I’ll post some screenshot in the morning when I’m back on the work pc. I’m also in the middle of stripping out a few bits from the tree to try and narrow down the issue

Interesting response though. Every tutorial and help document I’ve watched or read all point to behaviour trees being the exact thing I’d want to use in this instance. Is there a reason you disagree? How would you achieve this instead? Also worth bearing in mind that I have multiple AI on screen that will all be using the same behaviour as well as communicating between each other to also trigger different behaviour

Even if I were to put this on the most basic of outputs though it doesn’t seem to work. Let’s say I have a single selector with two routes. Both routes simply wait for 20 seconds and route 1 needs target actor set, route 2 needs it cleared (which works fine via my AI controller on perception update event)

So AI sees me, selector route 1 is therefore active. The AI then loses sight and therefore route 1 fails and route 2 takes over. Now my assumption of a selector is that it’ll succeed if one of its children succeeds so if the AI now sees me again while on route 2 then shouldn’t route 2 fail but route 1 is now succeeding again so I stay in the selector?

That’s what I expect but not what I’m seeing

EDIT: As an example, UE4s own Web document shows an example behaviour tree of patrol and chase player to let the AI choose what to do. I’m essentially just taking this example one small step further by including thd selector on the chase sequence such that I can go between seeing and not seeing the player as many times as happens whild staying in the chase sequence

So AI sees me, selector route 1 is therefore active. The AI then loses sight and therefore route 1 fails and route 2 takes over. Now my assumption of a selector is that it’ll succeed if one of its children succeeds so if the AI now sees me again while on route 2 then shouldn’t route 2 fail but route 1 is now succeeding again so I stay in the selector?

Selector will execute its children from left to right until one of them succeeds or they all have failed. In your case it sounds to me that first the first route fails, then the second route fails. So at that point the selector is done and has failed and it will continue the behavior tree, it will not go back to the first route of the selector.

Is there a reason you disagree?

This here from about minute 20 to minute 38 AI Arborist: Proper Cultivation and Care for Your Behavior Trees - YouTube

How would you achieve this instead?

What I use is behavior trees for the actual behavior, sort of priority based state machine for choosing the behavior to execute (instead of polluting the behavior tree with that) and optional transition behavior trees (for executing stuff when AI enters a new behavior). For me that keeps things cleaner and easier to build on than just using behavior trees for everything =)

So does this therefore mean that if a selector branch fails it can’t succeed again until the whole sequence fails and starts again? Or another way to like ag it is that a selector wouldn’t loop through its branches, Ince it gets to the end then it finishes?

Because the way the AIPerception is set up is that when it’s updated (player is seen) then target actor is set, and when it’s updated again (player is lost) then target actor is cleared. Bases on this i would therefore say that the selector cannot ever fail because the target actor will always be either set of unset - it can’t be both and it cant be neither

This also still does t explain how my behaviour tree can’t loop round and immediately reenter the alert sequence because at the termination of the selector, all the conditions that are needed to leave patrol and enter alert are all still set, therefore if the selector does end it should try to enter patrol, fail and reenter alert

So does this therefore mean that if a selector branch fails it can’t succeed again until the whole sequence fails and starts again?

It will only execute again when the behavior tree finishes/aborts and therefore restarts and again comes to that selector.

For debugging you could:

Ah OK, so I think that’ll answer one question, as I was expecting the behavior tree to remain in a sequence if a selector was going back and forth, I guess therefore it’s not going back and forth and therefore my second route on the selector fails if thd player is seen again, so that would finish the selector, then also finish that particular sequence

I guess that just leaves me with the question of why the behaviour tree doesn’t just loop back and find its way back into the same alert sequence given that all the relevant variables remain set after the alert sequence ends due to seeing the player (namely the variable that prevents the patrol sequence)

The AI debug via apostrophe I use quire a bit but that visual logger is a new one for me and definitely sounds like a good shout

Thanks man, all your help is greatly appreciated