PolandIdered/behavior-tree
🌲 Manage React state with Behavior Trees

JavaScript/TypeScript implementation of Behavior Trees.
📕 Read the documentation to learn the API.
Note: This software is in early development stage. It's not production ready. API may change.
Behavior Tree Toolkit
- 🌲 @btree/core - Framework agnostic behavior trees implementation
- ⚛ @btree/react - Hooks and docs how to use BT with React
Quick start
npm install @btree/core
import {nodes} from '@btree/core'
const initialState = {
isLoggedIn: false
}
const AuthBehavior = nodes.root('Auth behavior', () =>
/* Selector runs child one by one until one of them succeeds */
nodes.selector([
/* Sequence runs child one by one and stops if any child return failure status */
nodes.sequence([
/* Condition node run logic checks on current state */
nodes.condition('Is logged in', (state, props) => state.isLoggedIn),
/* Action node is used for side effects and state modifications */
nodes.action('Redirect to dashboard', (state, props) => {
navigate('/dashboard')
}),
]),
nodes.sequence([
nodes.action('Redirect to login page', () => {
navigate('/login')
}),
]),
])
)
// Create instance of tree
const authTree = AuthBehavior(initialState)
// Run tree logic
authTree.tick()
// You can also run it with props
authTree.tick({authKey: 'xyz'})
Why?
Getting app features done is cool but what about future changes? As time passes, you will forgot how given feature works. Writing comments or docs is not something that developers have in mind all the time. This library will handle that for you.
With Behavior Tree notation all your code will be wrapped with nodes - that allows to visualize how it works. Check /packages/react/example for implementation of this logic.

Similar Projects
Discover other similar projects and AI models.
📠 React custom hook for persist state management
freeCodeCamp.org's open-source codebase and curriculum. Learn math, programming, and computer science for free.
owl-bt is editor for Behavior trees. It has been inspired by Unreal engine behavior trees in a way, that it supports special node items like decorators and services. This makes trees smaller and much more readable.
A versatile framework for developing robot behaviors using behavior trees in ROS 2.
The code for a short tutorial on how to create a simple guard AI using behaviour trees in Unity/C# (in text or video format)!