项目作者: GabrieleTorini

项目描述 :
A GDScript implementation of a behavior tree for game AI , built through Godot's scene tree editor and nodes.
高级语言: GDScript
项目地址: git://github.com/GabrieleTorini/godot-behavior-tree.git
创建时间: 2021-01-18T16:39:40Z
项目社区:https://github.com/GabrieleTorini/godot-behavior-tree

开源协议:Other

下载


BTLogo

GodotBT: Behavior Tree Framework for Godot 4.x

A flexible, extensible behavior tree framework for Godot 4.x that makes it easy to create complex AI behaviors for your games.

Overview

GodotBT provides a complete behavior tree implementation for the Godot Engine, allowing you to create sophisticated AI behaviors with a clear, modular structure. The framework follows behavior tree design principles with a focus on reusability and separation of concerns.

Key Features

  • Complete Behavior Tree Implementation: Core composite nodes, decorators, services, tasks, and conditions
  • Reusable Behavior Trees: Define a behavior tree once and use it across multiple agents with their own contexts
  • Blackboard System: Flexible data sharing between nodes with typed accessors and change detection
  • Reactive Conditions: Trigger behavior changes based on world events with configurable abort scopes
  • Service System: Run periodic background tasks with customizable frequency
  • Context Separation: Each agent maintains its own execution context with the shared tree
  • Utility Components: Helpers like BTTargetKey for simplified movement and targeting operations
  • Example Implementation: Includes a complete demo with enemies that can patrol, chase the player, and react to events

Core Components

Composite Nodes

  • BTSelector: Runs child nodes until one succeeds or all fail
  • BTSequence: Runs child nodes until one fails or all succeed
  • BTParallel: Runs all child nodes simultaneously
  • BTRandomSelector/BTRandomSequence: Random execution order versions of selector and sequence

Decorators

  • BTInverter: Inverts the result of a node
  • BTRepeater: Repeats a node a specified number of times
  • BTRepeatUntil: Repeats a node until it returns a specific result
  • BTAlwaysReturn: Forces a node to return a specific result

Conditions

  • BTBlackboardBasedCondition: Condition based on blackboard values
  • BTReactiveCondition: Condition that can trigger aborts
  • BTCheckNonZeroBBEntry: Checks if a blackboard value is non-zero/non-empty
  • BTCompareBBEntries: Compares two blackboard entries

Services

  • BTService: Base class for services that run periodically
  • Custom services in examples like BTPlayerDetector, BTPatrolPathFollower

Tasks

  • BTTask: Base class for leaf nodes that perform actions
  • BTWait: Wait for a specified amount of time
  • Example implementations for movement, path finding, and more

Installation

  1. Clone or download this repository
  2. Copy the addons/godot_bt folder into your project’s addons folder
  3. Enable the plugin in Project Settings > Plugins

Basic Usage

Creating a Behavior Tree

  1. Create a new scene with a root node of type BehaviorTree
  2. Add a BTSelector or BTSequence as the first child
  3. Build your behavior tree by adding more composite nodes and tasks
  4. Save the scene

Using the Behavior Tree with Multiple Agents

  1. # Level.gd - Sets up the behavior tree and passes it to agents
  2. extends Node2D
  3. func _ready():
  4. # Find all agents and set their behavior trees
  5. for enemy in get_tree().get_nodes_in_group("enemies"):
  6. enemy.run_behavior_tree()
  1. # Enemy.gd - Each agent handles its own context
  2. extends CharacterBody2D
  3. @export var bt: BehaviorTree
  4. @export var patrol_path: Node2D
  5. var ctx: BTContext
  6. # Called by the level or manager
  7. func run_behavior_tree() -> void:
  8. if not is_instance_valid(bt):
  9. return
  10. ctx = bt.create_context(self, Blackboard.new())
  11. # Set up blackboard values for this agent
  12. if patrol_path:
  13. var patrol_points: Array = []
  14. for patrol_pt in patrol_path.get_children():
  15. patrol_points.append(patrol_pt.global_position)
  16. ctx.blackboard.set_value("patrol_points", patrol_points)
  17. func _physics_process(delta: float) -> void:
  18. if is_instance_valid(bt) and ctx:
  19. bt.tick(ctx, delta)

Example Implementation

The included example demonstrates:

  1. Enemy Patrol System: Enemies follow patrol paths or look in random directions
  2. Player Detection: A service that detects the player within a specified range
  3. Chase Behavior: Enemies chase the player when detected
  4. Navigation: Path finding to navigate around obstacles

To see the example in action, open the example/TestLevel.tscn scene.

Extending the Framework

The GodotBT framework is designed to be highly extensible. You can:

Create Custom Tasks

  1. class_name MyCustomTask extends BTTask
  2. @export var _some_parameter: float = 1.0
  3. func _tick(ctx: BTContext) -> BTResult:
  4. # Your implementation here
  5. if some_condition:
  6. return BTResult.SUCCESS
  7. else:
  8. return BTResult.RUNNING

Create Custom Conditions

  1. class_name MyCustomCondition extends BTCondition
  2. func _tick(ctx: BTContext) -> bool:
  3. # Your implementation here
  4. return some_condition_check

Create Custom Services

  1. class_name MyCustomService extends BTService
  2. func _tick(ctx: BTContext) -> void:
  3. # Your implementation here
  4. ctx.blackboard.set_value("some_key", calculate_some_value())

License

This project is available under the MIT License.

Contributing

Contributions are welcome! Feel free to submit pull requests or open issues for bugs and feature requests.