Download

Behavior Tree Problem

Hi. Im trying to understand the Behavior Tree. I started with the tutorial https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/QuickStart/index.html. Then i noticed that AI behaves oddly, and i checked everything pretty closely.

Then i stripped BT down little bit to understand it better.

But AI still keeps chasing player while he is away from the sphere trace.

65295912fe4b6d9cf5f1eb877cae80bfe891e80d.png

Then i dumped the AIMoveTo and changed it to VInterp, but it doesnt even work a bit.

So what is going wrong with this? Im trying to stop the AI from following when it is out from the reach.

Could you explain in plain text the behavior that you want your NPC to perform?

It cannot work. Receive execute is called every frame, your new tasks will end immediately, with success. AIMoveTo has a little watch icon on it, meaning that OnSuccess is only called when the actual move is completely finished. Until then finish execute is not called, so the BT stays in the same node, so the pawn continues to move frame after frame. Your task will call SetLocation once then call finish execute, so the BT will end too (or loop, whatever). You have to put some condition in your code, like distance to target, then call FinishExecute only when it’s met.

“It cannot work. Receive execute is called every frame, your new tasks will end immediately, with success. AIMoveTo has a little watch icon on it, meaning that OnSuccess is only called when the actual move is completely finished. Until then finish execute is not called, so the BT stays in the same node, so the pawn continues to move frame after frame. Your task will call SetLocation once then call finish execute, so the BT will end too (or loop, whatever). You have to put some condition in your code, like distance to target, then call FinishExecute only when it’s met.”

Oh, now i understand that.

“Could you explain in plain text the behavior that you want your NPC to perform?”

Initially i followed BT tutorial, which is Epics standard tutorial. Whit that tutorial AI never stopped chasing the player, which led to this problem. I want AI to stop the chase when player is out of reach of sphere trace. Somehow blackboard entry (TargetToHunt) never changes back to empty, or if it does, it doesnt matter.

No progress so far. I think BT is working correctly.

When stepping out from the reach.

9f224784441a3728d652933c5cf448713d76c07b.png

But AI Move To never stops walking.

You have condition “TargetToHunt is not set”. Do you clear TargetToHunt somewhere? Also when your BT node is aborted AIMoveTo will continue. You need to abort it in the ReceiveAbort event (I don’t remember what to call to abort it) or you can use MoveTo predefined BT task, it manages all that.

BTW, i was wrong yesterday, “Receive execute” is called just once, tick event is called every frame. But that don’t change anything in this case.

Yes, Target To Hunt is cleared and BB shows there is None when player moves out of the range. MoveTo worked, but i might need to use own movement task to get more customization. That AIMoveTo is pretty scary thing, which leads to write own movement routine (if i sometime get even a little bit skills). And i dont know anything about Event Receive Abort, how and where to use it. If there will be own movement routine, does it mean the AI cant navigate NavMesh same style as AIMoveTo? I post results when i got something.

Ok, now i got it. I dug some Google and found a sample how to use the abort.

I think now it works. Thx for the help!

ReceiveAbort is just another event of BT task, but it’s not shown by default like Event Receive Execute. Left click and search for it. MoveTo is just a wrapper around AIMoveTo, but it handles abort too. AIMoveTo is the function that navigates the navmesh. I don’t know if you can navigate it other way. I believe you can use something like “FindPath” (don’t remember the exact name) then use it to navigate, but it seems to be even more complicated.

AIMoveTo will start the movement then notify you how it goes. OnSuccess/OnFail will be called once the movement is completed. MovementResult struct gives you more info about how it goes.