flame_behavior_tree 0.2.0-dev.1 copy "flame_behavior_tree: ^0.2.0-dev.1" to clipboard
flame_behavior_tree: ^0.2.0-dev.1 copied to clipboard

A bridge package that integrates behavior_tree package with flame.

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.

The behavior tree itself comes from the behavior_tree package, which is re-exported from here. Read its README to learn about the available nodes, how running nodes work and how to share data using the blackboard.

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, and assign a BehaviorTree to behaviorTree:

class Enemy extends PositionComponent with HasBehaviorTree {
  @override
  Future<void> onLoad() async {
    behaviorTree = BehaviorTree(
      Selector([
        Sequence([
          Condition((context) => canSeePlayer()),
          Task((context) => chasePlayer(context.dt)),
        ]),
        Task((context) => patrol(context.dt)),
      ]),
      owner: this,
    );
  }
}
  • The tree is ticked on every update of the component. context.dt in the nodes is the time since the previous tick.
  • Passing owner: this lets nodes get the component with context.owner<Enemy>().
  • The blackboard of the tree is available as blackboard on the component, so the rest of your game can read and write the values that the AI works with.
  • The running nodes of the tree are aborted when the component is removed, or when you assign a new tree.

To tick the tree less often, increase tickInterval. This can be done at any time. The tree is ticked on the first update, and then whenever tickInterval seconds have passed. The dt that the nodes see is the time since the previous tick of the tree.

class Enemy extends PositionComponent with HasBehaviorTree {
  Enemy() {
    tickInterval = 0.5; // Think twice per second.
  }
}

Flame nodes #

Besides the nodes of behavior_tree, this package has two nodes that work with the effects of Flame:

  • PlayEffect(builder) adds an effect to the owner of the tree (or to a target), is running while it plays and succeeds when it completes. The effect is removed if the node is aborted.
  • MoveTo(destination, ...) moves the owner (or a target) to a position, either in a given duration or at a given speed.
Sequence([
  MoveTo((context) => context.get(destination), speed: 100),
  PlayEffect(
    (context) => ColorEffect(
      Colors.red,
      EffectController(duration: 0.3, alternate: true),
    ),
  ),
])

Documentation and examples #

The documentation explains how behavior trees work, with small interactive examples for the nodes and for HasBehaviorTree. They can also be found in the examples app.

See the example for an agent that walks in and out of a house.

Migrating from 0.1.x / 0.2.0-dev #

  • Instead of treeRoot = root, assign behaviorTree = BehaviorTree(root, owner: this).
  • HasBehaviorTree is not generic anymore.
  • The blackboard now belongs to the BehaviorTree. It is always available, uses typed keys and is not assigned on the component. Pass an existing one to BehaviorTree(blackboard: ...) if you need to.
  • Setting tickInterval after the component was loaded now works, and the first tick happens immediately instead of after the first interval.

The nodes themselves changed too, see the migration guide in the behavior_tree README.

2
likes
160
points
14
downloads

Documentation

API reference

Publisher

verified publisherflame-engine.org

Weekly Downloads

A bridge package that integrates behavior_tree package with flame.

Repository (GitHub)
View/report issues
Contributing

Topics

#flame

Funding

Consider supporting this project:

opencollective.com
github.com
patreon.com

License

MIT (license)

Dependencies

behavior_tree, flame, flutter

More

Packages that depend on flame_behavior_tree