BehaviorTree.js icon indicating copy to clipboard operation
BehaviorTree.js copied to clipboard

CooldownDecorator example

Open j-o-phillips opened this issue 2 years ago • 1 comments

Hi, This package is really useful but I'm struggling to implement a cooldown Decorator. Could you advise me why this doesn't work: First i register the task:

BehaviorTree.register(
    "fire",
    new Task({
      run: function (enemyShip) {
        enemyShip.fireMissile();
        return SUCCESS;
      },
    })
  );

Then i decorate it:

const decoratedTask = new CooldownDecorator({
    node: "fire",
    config: 5,
  });

Then I use it in the tree:

const tree = new Selector({
    nodes: [
      new Sequence({
        nodes: [
          new Task({
            run: function (enemyShip) {
              return enemyShip.checkIsInMissileRange(environmentDetails)
                ? SUCCESS
                : FAILURE;
            },
          }),
          new Task({
            run: function (enemyShip) {
              enemyShip.haltToStop(environmentDetails, setTargetShipDetails);
              return SUCCESS;
            },
          }),
          decoratedTask,
        ],
      }),
      new Sequence({
        nodes: [
          new Task({
            run: function (enemyShip) {
              enemyShip.moveToTarget(environmentDetails, setTargetShipDetails);
              return SUCCESS;
            },
          }),
        ],
      }),
    ],
  });

However the task 'fire' is still called for every step of the behaviour tree. Thanks in advance!

j-o-phillips avatar Dec 26 '23 08:12 j-o-phillips

Should it be:

const decoratedTask = new CooldownDecorator({ node: "fire", config: { cooldown: 5}, });

MikalDev avatar Oct 17 '24 04:10 MikalDev

Hiho. Sorry @j-o-phillips for the veeeery late reply, but MikalDev is right with his answer. Also note that the current version of CooldownDecorator is counting seconds, in case you wander what the 5 actually means. So the child node is only called again after 5 seconds (not 5 steps).

Calamari avatar Oct 31 '24 13:10 Calamari