I am looking into implementing a behaviour tree for the AI in my game. I am trying to get a feel for what most people do. How do you personally implement these systems in your games? Do you use a 3rd party asset, if yes which, or do you make it yourself?
I tried to code my animal AI first only using visual scripting. That worked, so I never bothered with anything more complex.
You’ll probably get more helpful answers if you can spell out what you want your AI to accomplish. My hunch is that most generic solutions will saddle you with tons of bloat and complication that you might not need.
A lot of the benefit behavior tree gives you is visual. You can just as well use any sort of graphic software like figma or w/e to draw out your system so that you can easily visualize state transitions, rather than trying to figure that out looking at text.
I’ve seen some people explain how you can use behavior trees to more easily get complex rule sets where AI agents can make multi-branched weighted decisions and such but… when I see it in action, I dunno, seems like something you could just code using standard toolsets and a bit of planning.
Anyway, that is all to say, I think you’ll be better served first by defining the behavior you want in as much detail as possible - like get as far as you can coding it with whatever tools and skills you have right now, then if you are facing some problem point, it will be easier to determine if there is a specific tool that will help.
If you search for general tools before you have clear vision, probably waste more time learning a bunch of stuff you’ll not end up using.
Node Canvas is a solid, mature tool proven in many notable productions such as The Long Dark, Kingdom series, Ghost of a Tale and many others. It also includes FSM and you can combine both concepts. I believe this is the only asset offering this unless you implement systems capable of the same yourself.
Behaviour Designer is also solid. More performant than Node Canvas but Node Canvas is much nicer to work with - better API and workflow.
I’ve seen some people implementing their own solutions using GraphView API with some UI Toolkit in the mix. Unity themselves recommend using GraphView for programming graph based tools right now.
Generally speaking, if I actually do use behaviour trees in my game at all, I use the aforementioned Behaviour Designer, usually expanded with some custom options. That said, I’ve kinda moved away from using them at all at this point. I’ve been using a custom variation on STRIPS for pretty much everything that requires even modestly complex AI at this point and if I need something less complex than that I just use a limited depth minimax.
I’ve tried Behaviour Designer too, and that seems pretty solid. You can script your own nodes and also send messages back and forth between the tree and your own scripts, so it’s pretty extensible and versatile.
I want to kind-of mirror what BIGTIMEMASTER said- I found behavior trees to have a bit of a learning curve to work with as far as designing the logic. I found that simple FSMs were far easier for simple things. Looks like NodeCanvas does either one, though.
Edit: I’ve also wanted to try Panda BT, one day. Before dropping a bunch of money, it’s probably worth-while to look at the free options.
And you implement those systems/algorithms yourself every time, or how, if I may be so curious?
I get the impression most people don’t really implement this themselves right? I am looking at the available tutorials and they all seem pretty basic and just a tad away from an actually usable system.
I’m not sure what you mean by that, but a behavior tree is mostly just a tool that an AI agent uses to decide which behavior to perform under what conditions. So that means you are still expected to implement the actual individual behaviors yourself. To a certain degree you can use the behavior tree package as a visual scripting tool, but (for example) the developer of Behavior Designer advises against this.
So if what you’re saying is that the behavior tree package that you’re looking at does not seem like a full, ready-to-go AI system, that’s probably true and by design. A behavior tree is just to provide an over-arching framework.
No I mean that the tutorials don’t provide enough of a complete solution to actually set up this tree with most of the functionality that will be required.
There are just a number of basic nodes I want to implement and the tutorials don’t really cover these. Wait nodes, random selectors, repeaters etc, it’s all basic stuff but IMO not all the implementation is trivial.
For example:
Seems quite limited, how would you for example implement wait nodes with this?
Same with this one:
https://medium.com/geekculture/how-to-create-a-simple-behaviour-tree-in-unity-c-3964c84c060e
(Actually I think this implementation of the sequence node doesn’t actually work as intended with regards to running nodes)
From my point of view, any state machine with more than 3 states starts becoming complicated to have an overview when they are defined just in code.
Try to understand a complex state machine reading the code when you were not the one programming it is hard.
I use an asset called visual state machine for the core of the states and the rest I do with scripts. Its very simple and lightweight, but I don’t want heavy assets with tons of features that I don’t need.
STRIPS comes down to basically a list of available actions and their cost, then it’s basically just A* traversal to determine the best course of action to figure out the goal. The fundamental basis is just a state machine that determines what it’s doing in the middle of its decision making process. Because this can be so easily abstracted away from the core of the game itself, all I’m really writing are the actions that go in that list. Minimax is also fairly simple, with the only thing I have to write that’s project specific is what specifically gets looked at.
Write once, export to a package, dump that package in whatever project I’m working on and use it as the basis for my AI.
For non behavior trees solutions some might be also interested in Utility AI.
That’s essentially simple. In both tutorials the action has something equivalent to “still-running” and “completed” state I think, so if you want to wait x seconds then keep adding up Time.deltaTime in a variable. If it’s still less than x return “still-running”. When you get a number greater then x return “completed”.
For years I was using Behavior Designer which I really enjoy. But now I am using my own Utility Theory implementation and I’m really impressed how so much more flexible it is over behavior trees.
I was more thinking about how to properly reset it when it is done, but maybe that is more something for the sequence node. You don’t want to complete one step and then get back to it immediately. However the unity tutorial implementation I find a bit odd because the sequence node only switches to the next leaf after each tick, which would mean checks all cost a frame. I also see some challenges for when to reset the sequence node when you switch to another branch, because I don’t think you want to continue a sequence once you did something else in between.
I decided to go with Banda BT (Free for now) I bought nodecanvas but I didn’t really like the way the blackboard was used because of the lack of encapsulation and how everything had to be tied together in the visual scripting editor made be really reconsider. I think a text based solution is way more suited for my needs. It meshes better with version control, is easier to copy/maintain and it is easier to force encapsulation. I don’t really see what visual scripting adds.
When the solution is good, it doesn’t. Similar to how you’re already using C#, .NET and Unity’s library and tools. You just don’t use what you don’t use and you don’t even know about it.
No. Behavior tree isn’t visual. A specific plugin and/or implementation might provide a UI to create behavior trees, but behavior tree is a concept, like a state machine, list, graph or a tree. We can have visual UI tools for those, but they are(even the graph, yes) aren’t visual in it’s core. You can have a code based behavior tree implementation without UI.
No, again, you’re confusing the visual representation with the concept and the design pattern. Drawing pictures on figma isn’t the behavior tree, those are just pictures. If you draw a “behavior tree” in figma and then go and implement that in visual scripting with 9000 if statements, you don’t have a behavior tree. You just have a bad procedural solution.
Yes, you always can. But they’re much harder and longer to get right and much harder and clunky to work with. Again, even if you’re going for only code solution, you will want to go for state machines, behavior trees, GOAP or some other design pattern for any even slightly non-trivial AI, unless you’re mad and you go for 50 bool variables and 1000-lines long update function in a 5000 lines long class.