Calamari / BehaviorTree.js

An JavaScript implementation of Behavior Trees.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Condition node implementation request

gsommavilla opened this issue · comments

It seems to me that there is no condition node object.

It would be nice to have it, to allow the user to build a tree like this:

{
  "type": "sequence",
  "name": "the root",
  "nodes": [
    {
      "type": "findBall",
      "name": "ACTION/TASK: look around and try to see a ball"
    },
    {
      "type": "isBallNear",
      "name": "CONDITION: is ball near?",
      "nodes": [
        {
          "type": "graspBall",
          "name": "ACTION/TASK: grasp the ball"
        }
      ]
    }
  ]
}

condition_2018-06-27-15-13-58_1452x517

I understand that a condition can be implemented with a sequence.
The above would be translated like this:

{
  "type": "sequence",
  "name": "the root",
  "nodes": [
    {
      "type": "findBall",
      "name": "ACTION/TASK: look around and try to see a ball"
    },
    {
      "type": "sequence",
      "name": "sequence used to implement a condition",
      "nodes": [
        {
          "type": "isBallNear",
          "name": "ACTION/TASK used as CONDITION: is ball near?"
        },
        {
          "type": "graspBall",
          "name": "ACTION/TASK: grasp the ball"
        }
      ]
    }
  ]
}

sequence_implementing_condition_2018-06-27-15-15-47_1452x927

Although the two trees yield to the same behaviour, I think that having the condition node can be more readable. So I am asking if it is possible to implement the condition node.

What you depicted in your first picture, you can easily do with a Decorator. To decide about if to take an action or not is something, you need outside knowledge, that cannot be provided by a library. (Meaning it is highly dependent of the structure of the blackboard you are using.)

So I would do it kinda like this:

The blackboard you are giving into the BT contains data, you can make a deciscion about, or even a function like .isBallNear(), and only if this is resolving to something positive, run the action behind it. Something along the lines of:

export default class IsBallNearDecorator extends Decorator {
  nodeType = 'IsBallNearDecorator'

  decorate (run, blackboard) {
    if (blackboard.isBallNear())
      return run()
    else
      return FAILURE
  }
}

Hope that brings you on the right track :)

I came back to this issue and yes, your suggestion to use a decorator with the content of the blackboard is simple and useful, thanks!