Behavior Trees AI Library

ai
opensource
free
library

#1

EDIT: Moved to the Community Resources section so more people can use this

Want a snazzy system for creating convincing AI?
Well do I have the library for you.

Introducing…
Behavior Trees!

A behavior tree is a model for AI that is known for being able to switch between a finite set of tasks in a modular fashion. This is because behavior trees break down complicated tasks into smaller, more approachable ones, without worrying about how these small tasks are processed.

Now you may be wondering (if you’ve ever been at all interested in AI design before)
"But OniiCh_n, why not just use a Finite State Machine, or a Hierarchical FSM for a large system, instead?"

Well, the key difference between a Finite State Machine (FSM) and a Behavior Tree is that while FSMs are centered around switching between states (which may contain a wide variety of actions within a single state), Behavior Trees are focused on switching between tasks that have been broken down into more understandable forms.

"But who actually uses behavior trees?"

Have you ever played Halo? Haven’t you ever noticed how the AI in Halo just feels like they actually know what they’re doing?
That’s a behavior tree at work.
While Halo was not the first implementation of behavior trees in video games, they were pretty darn successful in it and that’s one of the features that made their game stand out.

"So how do I get to work using these ‘AI plants’?"
Feel free to grab this model and explore the library.
Installation is really straightforward, just move BehaviorTree into ReplicatedStorage and then require the module with something along the lines of:

BehaviorTree = require(game.ReplicatedStorage.BehaviorTree.behavior_tree)

Example
--This example creates a new BehaviorTree "Frank", who will complete 20 random tasks

    local BT = require(game.ReplicatedStorage.BehaviorTree.behavior_tree)

    local Frank = BT:new({
      object = {name = 'test'},
      tree = BT.Sequence:new({
        nodes = {
          BT.Task:new({
            run = function(task, object)
              print(object.name .. " looking")
              task:success()
            end
          }),
          BT.Random:new({
            nodes = {
              BT.Task:new({
                run = function(task, object)
                  print(object.name .. " walk left")
                  task:success()
                end
              }),
              BT.Task:new({
                run = function(task, object)
                  print(object.name .. " walk up")
                  task:success()
                end
              }),
              BT.Task:new({
                run = function(task, object)
                  print(object.name .. " walk right")
                  task:success()
                end
              }),
              BT.Task:new({
                run = function(task, object)
                  print(object.name .. " walk down")
                  task:success()
                end
              }),
            }
          }),
        }
      })
    })

    for _ = 1, 20 do
      Frank:run()
    end

Want to learn more about how to use behavior trees in your game or you need some documentation to use this specific library? Check out this GitHub page.

Please note that I did not write this library myself. I merely ported it for use on Roblox as part of an ongoing project and decided that it would be great to share it with all you guys.

Happy coding!
<3, OniiCh_n


#2

I was literally just reading the Gamasutra article you linked on your GitHub page a few days ago and tanema’s pure Lua implementation that you forked from. What a coincidence to find this after! I’ll poke around and see how it goes.


#3

It works like a charm.

Here’s an implementation I’ll be using in a project releasing next week. In this, the tree runs through all the tasks I’ve made in succession.