Recursivity?

Nightkin

New member
Hello, sorry if I'm asking a question that has already been answered but I couldn't find an answer in the forum.

I bought BD several years ago but I'm starting to use it only now for my game... which has quite a lot of requirements. One of them being that an NPC should be able to do tasks recursively. Here is a quick example:

1. The NPC walks to a position.
2. There is a closed and locked door blocking its path and it detects it.
3. The NPC knows where the key is.
4. The NPC walks to where the key is stored.
5. Problem is, the key is in a chest that is also locked, so the NPC needs that key too. Call a new tree that starts at step 3.

Step 5 is a recursive call because the key to the door is locked in a chest which key could also be locked in a chest and so on... you get the picture. This is an extreme example but there are many other applications of this case and I don't see how to implement it in Behavior Designer with true recursivity (meaning executing a local tree with its local variables and context switching).

I've experimented with shared variables but that's not good for the purpose since they're global in their own tree. I've looked into external behavior trees but they seem static and consume their references when instantiated so no way to repeat them indefinitely in the same parent tree. I don't really want to instantiate new behavior tree components dynamically in an NPC every time I must call a tree recursively either (even though that might solve the problem since each BD component has its own context after all).

Is there an easy way to achieve true recursivity with BD?

I'd like to point out that I made my own behavior tree engine in another language for a commercial product of mine but it is very different from BD. It is event-driven and relies on textual trees (written like programs in their own simple language), and recursivity is what it is based on so it handles this case above natively and easily. I will port it to C# to use into my Unity game if I have to but it would be a shame since I already bought BD and I like its other features, and I don't really want to spend time on porting code.

Thanks!
 
According to the standard behavior tree implementation there isn't a concept of recursiveness since the tree should be able to be laid out flat. With that said, you could pretty easily create a decorator task similar to the repeater which reruns the same branch for as long as the condition is true.
 
Thank you for your answer, I suspected as much seeing how external behavior trees are instantiated once. If I understand your suggestion, that decorator task would have to hold a stack of contexts to provide all the variables for the child sub-tree, am I correct?

I'm don't know if there is a "standard" behavior tree implementation though, I built my own engine following the ideas exposed in Chris Simpson's Gamasutra blog post (https://www.gamedeveloper.com/programming/behavior-trees-for-ai-how-they-work) which looked pretty standard and straightforward to me. It works pretty well and "recursivity" (or rather recursiveness, sorry English is not my native language) is built at its core. The whole program is divided into small trees that call each other constantly. Plus, when I hear about "trees" I immediately think about recursivit--ness, must be a professional deformation I guess.

Anyway thanks, I'll see how to implement your idea in BD. What I would like to do is create a "call function" action task that packages variables into a context and sends it to another "function" decorator task, possibly either in the same sub-tree or elsewhere in the same tree, which would stack the context and re-run its sub-tree. Just thinking out loud, I'll have to design this a bit more seriously when/if I really need that feature.
 
Maybe with external behavior trees and referencing (executing) that? So a BT referencing (an instance of) itself?
[edit] it’s called a Behavior Tree Reference Task
 
Last edited:
Top