flame

This is a bridge package that integrates the behavior_tree dart package with Flame engine.


Features

This package provides a HasBehaviorTree mixin for Flame Components. It can be added to any Component and it takes care of ticking the behavior tree along with the component's update.

Getting started

Add this package to your Flutter project using:

flutter pub add flame_behavior_tree

Usage

  • Add the HasBehaviorTree mixin to the component that wants to follow a certain AI behavior.

    class MyComponent extends Position with HasBehaviorTree {
      
    }
    
  • Set-up a behavior tree and set its root as the treeRoot of the HasBehaviorTree.

class MyComponent extends PositionComponent with HasBehaviorTree {
  Future<void> onLoad() async {
    treeRoot = Selector(
      children: [
        Sequence(children: [task1, condition, task2]),
        Sequence(...),
      ]
    );
    super.onLoad();
  }
}
  • Increase the tickInterval to make the tree tick less frequently.
class MyComponent extends PositionComponent with HasBehaviorTree {
  Future<void> onLoad() async {
    treeRoot = Selector(...);
    tickInterval = 4;
    super.onLoad();
  }
}

Additional information

When working with behavior trees, keep in mind that

  • nodes of a behavior tree do not necessarily update on every frame.
  • avoid storing data in nodes as much as possible because it can go out of sync with rest of the game as nodes are not ticked on every frame.

Libraries

flame_behavior_tree
A bridge package that integrates behavior_tree package with flame.