Parallel Execution in Behavior Tree

Hi guys

I am creating a simple behavior for a cannon.

The desired behavior is:

  1. Aim the cannon at the player, and simultaneously
  2. Fire and Reload on cool down.

I currently have the following Behavior Tree implemented:

69592-bt.png

Predictably, this results in sequential execution of Aim, Fire and Reload.
This causes rather ‘jerky’ behavior for the cannon since Reload task has a certain delay built into it.

How do I modify this to achieve the desired behavior? I want it to constantly run Aim task and Fire & Reload when these abilities come off cool down.

Thanks for your help

1 Like

Ok - after spending 2 nights bashing my head on the wall. I finally worked it out.
I tried Simple Parallel composite but I could not get it to work. For some reason it would always run the main task but it would never run the other tree. Here is the working Behavior Tree:

When I tried this before, it would only run Aim task and never run Fire and Reload.
The reason for that behavior is very subtle. From the [documentation][2]: You can think of the Simple Parallel node as “While doing A, do B as well.”

The reason why it did not work for me before is because I had Finish Execute node in the Aim Task. As a consequence, it would finish Aim when conditions were met and never execute the Fire and Reload sequence. Simple Parallel only works if the primary task does not finish.

This is not very obvious guys - could we please make it a little clearer in the documentation?

Thank you

4 Likes

Thanks, for this, Nick! You saved me from pulling out what’s left of my hair.

1 Like

I signed in to applaud you, Nick Jackson, as i was having this same problem and not much documentation on it!!

Hi,

I’m trying something similar.

I want to make an actor run around while shooting from time to time at a target.

However, as soon as I attach my Fire Task the main task isn’t running anymore. It is running, though, if I add a Finish Execution node to the main task, but then the secondary task isn’t executed anymore.

Are there any specific constraints on a secondary task? My Fire Task so far is just a “Print Hello”-Node and the option of a finish execution node (doesn’t work no matter it’s connected or not)

If I connect the finish execution node in my primary task, the primary is executed like intended, but my secondary task isn’t executed anymore, as you have mentioned it.

Sorry, wanted to post as comment but I think, my image would vanish.

okay, without the finish execute node, I assume, the primary task is executed only once, is that true? Do I need a while(true)-loop inside the task to keep it running?

turns out, you can’t have a while(true) loop in a task as it throws an error.

thanks, this made the note “finish with main task” more clear…now it makes sense. i was using simple parallel and wondering why the branch was never executing. had Finish Execute on the task too.

Just started on AI days ago, so my approach might not be the best. I tested this tree. it will move to the Player Location and fire on sight, its a little more complicated than yours. Tell me if you run into any issue. Its important that you set the notify observer, that way it will work as a trigger and refresh the tree.
If assume your waypoint picker is an roaming EQS, i would add that as a 2nd branch next to the simple parallel, so when it fails, the Pawn starts to roam around.

Have you tried making aim loop? Main task: sequence (with loop decorator) aim + maybe wait with a low time. Can make it loop indefinitely and if any other action is needed it can be forced out by an observer.

You can have a loop decorator though.

Okay, I gained some new insights on this:

your main task can indeed execute the Finish Execution node, however your simple parallel node needs its execution flow mode set to “wait for sub tree”

If it’s set so, then the execution of the tasks is pretty much straight forward and as one would expect.

with Wait for sub Tree you mean Finish Mode to “Delayed”?

yes (strange, you have to write at least 10 chars to answer)

The problem with this solution is that it has the opposite problem; once you finish the main task, it will never try to execute it again.

Sir, thank you Sir! :slight_smile:

For anyone new getting till here and still finding it difficult to understand. Simple Parallel is really a misnomer. Probably the biggest UE documentation flop.

Anyway:
It seems the main task is expected to be a long running task that’ll probably only Finish Execution after a while.

Finish Mode is set to immediate and immediate completing main task:
If it finishes execution immediately it’ll repeat over and over and the background won’t really trigger because it’ll get cancelled over and over.

Finish Mode set to delayed and immediately completing main task:
Main task finishes immediately and then the background task runs - which will appear as only the background task running.

Furthermore, I believe that adding e.g. a cooldown decorator on the node will cause both to stop running when the cooldown is activated. So I built a delay into my task directly, that’ll only Finish Execution after the delay.

I hope that makes sense.

1 Like

All these years later and this post is still good. This is what my problem was. My AI were stuttering and once I removed the Finish Execute all 300 AI runs and attacks smoothly now.

Thank you!