Help undertstanding do while and if

I've just started using behaviour designer and I really like it, but I have a really hard time wrapping my head around how to create simple if-statements and "loops".

My AI tries to find a resource, if it finds one then go to it and gather, else go and idle at the workplace. When gathering the resource I would write code like below inside a coroutine or some sort of loop:

if(currentCarry >= maxCarry) GoStash()
else ChopWood();

Basically telling my AI to chop wood as long as he can carry more, else go stash.

How do I do something like this in BD? You can see my questionmark in red, where I assume the logic would go. If gather wood fails, I want to rerun the tree (finding a new resource since it fails if the current resource is depleted))

1631210685992.png
 
It helps to rename the Sequences (in the task inspector in the BD editor) into "DO" and the Selectors into "EITHER". That way you read sequences as "DO x THEN y THEN z" ("THEN" being taken in the sense of "IF... THEN" but also in the sense of "AND" or "ALSO") and you read selectors as "EITHER x OR y OR z". Makes it sound more natural that way. That being said, behavior trees are not really like classic programs, they're to be thought as plans for the AI to do stuff. Typically it says "Do this then that and if anything goes wrong, here are your backup plans".

To answer your question I would do something like this (this tree is untested, this is just an example) :

2021-09-10_005012.jpg

It looks more complicated than it is. Most of the sequence (the "DO" node) is straightforward except the selector (the "EITHER" node) because I've made the tree take into account the possibility of all resource points being occupied by other gatherers, in which case the agent must wait. But just so it doesn't wait forever, there's a 60 second timeout handled by the Parallel Complete node. That node returns as soon as one of its children returns something other than Running so it is perfect in this case.

It goes like this :
- Do I need to gather resources? Then either find some free resource or wait (in a safe place) for a point to be freed
- After waiting, if I haven't waited for too long, go back to finding a resource
- Once I have a resource set for me, go there, gather, wait until I'm done gathering then go to the drop point to unload
- Because of the "until failure" node on top, start over until I fail a task and its contingency plan(s)

Hope this helps.
 
It helps to rename the Sequences (in the task inspector in the BD editor) into "DO" and the Selectors into "EITHER". That way you read sequences as "DO x THEN y THEN z" ("THEN" being taken in the sense of "IF... THEN" but also in the sense of "AND" or "ALSO") and you read selectors as "EITHER x OR y OR z". Makes it sound more natural that way. That being said, behavior trees are not really like classic programs, they're to be thought as plans for the AI to do stuff. Typically it says "Do this then that and if anything goes wrong, here are your backup plans".

To answer your question I would do something like this (this tree is untested, this is just an example) :

View attachment 7099

It looks more complicated than it is. Most of the sequence (the "DO" node) is straightforward except the selector (the "EITHER" node) because I've made the tree take into account the possibility of all resource points being occupied by other gatherers, in which case the agent must wait. But just so it doesn't wait forever, there's a 60 second timeout handled by the Parallel Complete node. That node returns as soon as one of its children returns something other than Running so it is perfect in this case.

It goes like this :
- Do I need to gather resources? Then either find some free resource or wait (in a safe place) for a point to be freed
- After waiting, if I haven't waited for too long, go back to finding a resource
- Once I have a resource set for me, go there, gather, wait until I'm done gathering then go to the drop point to unload
- Because of the "until failure" node on top, start over until I fail a task and its contingency plan(s)

Hope this helps.

Thank you so much, it helps a lot :) I ended up with this which seems to work rly well:

1631267983085.png
 
Last edited:
Top