Back to Flame

README

packages/flame_behavior_tree/behavior_tree/README.md

1.37.02.1 KB
Original Source
<!-- markdownlint-disable MD013 --> <p align="center"> <a href="https://flame-engine.org"> </a> </p> <p align="center"> This package provides a simple and easy to use <a href="https://en.wikipedia.org/wiki/Behavior_tree">behavior tree</a> API in pure dart. </p> <p align="center"> <a title="Pub" href="https://pub.dev/packages/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 -->

Behavior tree is a very common way of implementing AI behavior in game and robotics. Using this, you can break-down a complex behavior of an in game AI, into multiple smaller nodes.

Features

  • Nodes
    • Composite
      • Sequence: Continues execution until one of the children fails.
      • Selector: Continues execution until one of the children succeeds.
    • Decorator
      • Inverter: Flips the status of the child node.
      • Limiter: Limits the number of ticks for child node.
    • Task
      • Task: Executes a given callback when ticked.
      • AsyncTask: Executes an async callback when ticked.
      • Condition: Checks a condition when ticked.

Getting started

Add this package to your dart project using,

bash
dart pub add behavior_tree

Usage

  • Create a behavior tree.
dart
final treeRoot = Sequence(
  children: [
    Condition(() => isHungry),
    Task(() => goToShop()),
    Task(() => buyFood()),
    Task(() => goToHome()),
    Task(() => eatFood()),
  ]
);
  • Tick the root node to update the tree.
dart
final treeRoot = ...;
treeRoot.tick();