Beehave for Godot

repository·godot-4.x·Indexed 25 days ago

https://github.com/bitbrain/beehave

A Godot Engine addon for creating AI systems using behavior trees. Beehave allows developers to build complex NPC behaviors and adaptive AI by integrating behavior trees directly into the Godot scene tree via the BeehaveTree node. It utilizes a node-based approach with status codes (SUCCESS, FAILURE, RUNNING) and provides specialized leaf nodes such as ConditionLeaf for state checks and ActionLeaf for performing tasks.

Tokens
12K
Snippets
18
Records
93
Agent score
83%

What's inside Beehave

  1. Overview of Beehave for Godot

    godot-4.x
    Beehave is a Godot Engine addon used to build Artificial Intelligence (AI) logic via Behavior Trees. It uses a node-based approach where Behavior Trees are integrated directly into the Godot scene tree. A BeehaveTree node is attached to an actor (the node you want to control), and it runs its logic every frame tick, modifying the parent node based on the tree's execution.
  2. Use the Blackboard to manage data in Behavior Trees

    godot-4.x

    The Blackboard is a centralized repository used to store and share data between multiple nodes in a behavior tree. It prevents the need to pass variables directly to nodes and ensures data consistency across different game characters or objects.

    By default, every BeehaveTree manages its own private blackboard instance. However, you can assign a custom Blackboard node from your scene tree to the blackboard property of a BeehaveTree to allow multiple trees to share the same data source.

  3. Understand Composite Nodes in Beehave

    godot-4.x
    Composite nodes are parent nodes used to create logic flows by combining conditions and actions. They act as the structural foundation of a Behavior Tree, serving as the 'glue' that connects leaf nodes (conditions and actions) into a hierarchy. Common types include Selectors and Sequences.
  4. Understand Leaf Nodes in Beehave

    godot-4.x

    Leaf nodes are the terminal nodes in a Beehave behavior tree. They have no children and represent the actual execution of logic. Unlike composite nodes or decorators that manage flow, leaf nodes interact directly with the game world.

    There are two primary types of leaf nodes:

    • Action Nodes: Perform actions that change the game state (e.g., moving, playing animations).
    • Condition Nodes: Check conditions in the game state (e.g., checking health levels or visibility).

    All leaf nodes must return one of three statuses: SUCCESS, FAILURE, or RUNNING.

  5. Understand Behavior Tree Node Types

    godot-4.x

    Beehave uses a hierarchical tree structure composed of different node types to organize AI logic:

    • Root: The entry point of the tree.
    • Composite Nodes: Manage the flow between multiple child nodes (e.g., deciding which branch to follow).
    • Decorator Nodes: Modify the behavior or result of their single child node (e.g., inverting a result or repeating an action).
    • Leaf Nodes: The terminal nodes that perform actual game actions (e.g., Move to position) or check specific conditions (e.g., Is health low?).
  6. Understand Beehave Node Status Codes

    godot-4.x

    Every node in a Beehave behavior tree returns one of three status codes that determine how the tree progresses:

    • SUCCESS: The node completed its task successfully.
    • FAILURE: The node failed its task or its conditions were not met.
    • RUNNING: The node is still performing its task and requires more time. When a node returns RUNNING, it will be revisited on the next tick until it returns SUCCESS or FAILURE.
  7. Monitor Beehave performance metrics

    godot-4.x

    You can monitor Beehave's performance directly within the Godot editor. Navigate to the Debugger panel at the bottom of the editor, select the Monitors tab, and scroll down to the beehave section.

    Available global metrics include:

    • beehave/tree_id-process_time: The total process time per frame tick for the whole behavior tree.
    • beehave/total_trees: The total number of Beehave nodes in the scene.
    • beehave/total_enabled_trees: The number of active behavior trees in the scene.
  8. Create custom leaf nodes for game mechanics

    godot-4.x
    To implement specific game logic, you should create custom leaf nodes. This allows you to encapsulate game-specific logic into reusable components. While Beehave provides base types, you will typically extend them to create your own Action Leaf or Condition Leaf nodes to handle your specific mechanics.
  9. Implement the Memory Pattern using Blackboards

    godot-4.x

    The Memory Pattern uses the Blackboard to store information between ticks, allowing nodes to make decisions based on past events (e.g., remembering a player's last seen position).

    Structure:

    Sequence
    ├── Condition (Stores result in blackboard)
    └── Action (Uses stored result)
    // SpotAndRememberPlayer.gd
    class_name SpotAndRememberPlayer extends ConditionLeaf
    
    func tick(actor: Node, blackboard: Blackboard) -> int:
        var player = get_tree().get_first_node_in_group("player")
        if player and actor.can_see(player):
            // Remember where we saw the player
            blackboard.set_value("last_seen_position", player.global_position)
            blackboard.set_value("last_seen_time", Time.get_ticks_msec())
            return SUCCESS
        return FAILURE
  10. Use the Simple Parallel node to execute simultaneous actions

    godot-4.x

    The SimpleParallel node is used to execute exactly two children at the same time, following the logic: "While doing A, do B as well".

    Behavior Rules:

    • Child Count: Must have exactly two children.
    • Primary vs. Secondary: The first child is the primary node; the second child is the secondary node.
    • State Reporting: The node always reports the state of the primary node. It continues ticking as long as the primary node returns RUNNING.
    • Secondary Node Execution: The secondary node is executed like a subtree, but its state is ignored by the SimpleParallel node itself.
    • Termination: If the primary node returns SUCCESS or FAILURE, the SimpleParallel node immediately interrupts the secondary node and returns the primary node's result.
    • Delay Mode: If running in delay mode, the node will wait for the secondary node to finish its action after the primary node terminates.

    Best Practices:

    • While SimpleParallel nodes can be nested to create complex behaviors, excessive nesting is not recommended as it can make behavior trees difficult to maintain.