ashblue / fluid-behavior-tree

Behavior trees for Unity3D projects. Written with a code driven approach on the builder pattern.
MIT License
967 stars 110 forks source link

Inject tree in existing tree during runtime #66

Closed Chris1234567899 closed 2 years ago

Chris1234567899 commented 2 years ago

I've seen there is the option to inject a tree in the build process (example in the readme). However, the Splice method also seems to allow injecting a tree during runtime into an existing one.

public void Splice(ITaskParent parent, BehaviorTree tree);

However, how can I get the correct parent node from my existing tree? Is this feasible? Would be nice to have something like "BehaviorTree.GetNodeById(string id)" or alike.

Example use case: I plan to have a behavior tree for units like

My units will be assigned to different jobs during their lives (or no job at all) so at the "work" node I'd like to inject/replace a tree "woodcutterbehavior" or "farmerbehavior" or alike. At runtime, obviously.

Sorry if this is kind of a noob question, I'm only just getting familiar with Behavior Trees.

ashblue commented 2 years ago

Well you usually want spliced trees to be self sufficient with dynamic conditions. I don't store memory references to the nodes as that could be a lot of memory overhead for a project.

I wouldn't try to "edit" trees directly after building, a lot of caching happens under the hood. I think you have these options below to solve your problem. I'll list them in order of how highly I'd recommend them.

  1. During the tree build process create a method wrapper that can inject the sub-tree you want. Make the spliced tree logic self-contained with custom actions that access your MonoBehavior (see custom actions in docs)
  2. Create a custom implementation of Splice (https://github.com/ashblue/fluid-behavior-tree#creating-custom-reusable-nodes) and have it return an ID reference. You'd also have to add a method to a custom tree class wrapper. Seems like a lot of overhead for not much return (but it's doable). I don't recommend this as you'd have to manage the memory reference too

PS For faster feedback we have a Discord to quickly answer questions

Chris1234567899 commented 2 years ago

Thank you very much for the quick answer. I guess I'll give the first one a try.