KoBeTree is a powerful and flexible Kotlin library designed specifically for building behavior trees. Behavior trees are widely used in AI and game development to create complex, decision-driven behaviors for characters, NPCs, and more.
- DSL API: KoBeTree offers an intuitive and expressive Domain-Specific Language (DSL) for constructing behavior trees with ease.
- Composite Nodes: Implement complex decision-making logic using composite nodes like
SelectorandSequence. - Decorator Nodes: Fine-tune behaviors with decorator nodes like
InverterandRepeater. - Task Nodes: Create custom task nodes using Kotlin lambdas for dynamic and specialized behaviors.
- Blackboard: Share data and information across nodes using a centralized blackboard.
You can easily integrate KoBeTree into your Kotlin projects by adding the following dependency:
Add the repository to your project's settings.gradle.kts or settings.gradle file:
dependencyResolutionManagement {
...
repositories {
...
maven {
url = uri("https://maven.pkg.github.com/pererikbergman/github-packages")
}
}
}dependencies {
implementation("com.rakangsoftware.kobetree:kobetree:0.1.0") // Replace with the latest version
}<dependencies>
<dependency>
<groupId>com.rakangsoftware.kobetree</groupId>
<artifactId>kobetree</artifactId>
<version>0.1.0</version> <!-- Replace with the latest version -->
</dependency>
</dependencies>Here's a simple example of how to create a behavior tree using KoBeTree:
import com.rakangsoftware.kobetree.core.BehaviorStatus
import com.rakangsoftware.kobetree.core.Blackboard
import com.rakangsoftware.kobetree.dsl.condition
import com.rakangsoftware.kobetree.dsl.selector
import com.rakangsoftware.kobetree.dsl.sequence
import com.rakangsoftware.kobetree.dsl.task
fun main() {
// Create a blackboard to share data between nodes
val blackboard = Blackboard().apply {
put("health", 100)
put("enemyInSight", true)
}
// Define a behavior tree using the DSL
val tree = selector(blackboard) {
sequence {
condition { blackboard.get<Int>("health")!! > 50 } // Healthy enough to fight?
condition { blackboard.get<Boolean>("enemyInSight") == true } // Enemy in sight?
task {
println("Attack the enemy!")
BehaviorStatus.SUCCESS
}
}
sequence {
condition { blackboard.get<Boolean>("enemyInSight") == true } // Enemy in sight?
task {
println("Run away!")
BehaviorStatus.SUCCESS
}
}
}
// Execute it (call execute() again on every tick)
val result = tree.execute()
println("Behavior tree execution result: $result")
}Every node's execute() returns a BehaviorStatus of SUCCESS, FAILURE, or RUNNING.
task { } lambdas must return one of these, and condition { } lambdas return a Boolean.
Available DSL builders: selector, sequence, inverter, repeater(times), task, and condition.
Each builder has a root form that takes a blackboard and a nested form for use inside another builder:
val patrol = repeater(blackboard, times = 3) {
sequence {
task { walkToNextWaypoint() }
inverter { condition { blackboard.get<Boolean>("enemyInSight") == true } }
}
}TBA
KoBeTree is released under the MIT License for maximum flexibility and compatibility with your projects.
We welcome contributions from the community! If you have ideas for improvements, bug reports, or new features, please open an issue or submit a pull request.
KoBeTree is inspired by the work of many AI and game development enthusiasts who have contributed to the field of behavior trees.