Back to Flame

README

packages/flame_behavior_tree/README.md

1.37.02.6 KB
Original Source
<!-- markdownlint-disable MD013 --> <p align="center"> <a href="https://flame-engine.org"> </a> </p> <p align="center">This is a bridge package that integrates the <a href="https://github.com/flame-engine/flame/tree/main/packages/flame_behavior_tree/behavior_tree">behavior_tree</a> dart package with <a href="https://flame-engine.org/">Flame engine</a>. </p> <p align="center"> <a title="Pub" href="https://pub.dev/packages/flame_behavior_tree" ></a> <a title="Test" href="https://github.com/flame-engine/flame/actions?query=workflow%3Acicd+branch%3Amain"></a> <a title="Discord" href="https://discord.gg/pxrBmy4"></a> <a title="Melos" href="https://github.com/invertase/melos"></a> </p>
<!-- markdownlint-enable MD013 -->

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:

bash
flutter pub add flame_behavior_tree

Usage

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

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

dart
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.
dart
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.