Calamari / BehaviorTree.js

An JavaScript implementation of Behavior Trees.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failing to run the examples in README.md

gsommavilla opened this issue · comments

Failing to run the examples in README.md

I installed behaviortree with

npm i --save behaviortree

Then I copied and pasted the following code (taken from README.md) into a file called bt_test.js.

import BehaviorTree, { Sequence, Task, SUCCESS, FAILURE } from 'behaviortree'
BehaviorTree.register('bark', new Task({
  run: function(dog) {
    dog.bark()
    return SUCCESS
  }
}))
 
const tree = new Sequence({
    nodes: [
      'bark',
      new Task({
        run: function(dog) {
          dog.randomlyWalk()
          return SUCCESS
        }
      }),
      'bark',
      new Task({
        run: function(dog) {
          if (dog.standBesideATree()) {
            dog.liftALeg()
            dog.pee()
            return SUCCESS
          } else {
            return FAILURE
          }
        }
      }),
 
    ]
  })
});

const dog = new Dog(/*...*/); // the nasty details of a dog are omitted
 
const bTree = new BehaviorTree({
  tree: tree,
  blackboard: dog
})
 
// The "game" loop:
setInterval(function() {
  bTree.step();
}, 1000/60)

Then I tried to run it with

node bt_test.js

Firstly the compiler complained about }); on line 33, which I removed.

Secondly, I had problems with the import keyword.

Running bt_test.js gave me the following error:

$ node bt_test.js
(function (exports, require, module, __filename, __dirname) { import BehaviorTree, { Sequence, Task, SUCCESS, FAILURE } from 'behaviortree'
                                                                     ^^^^^^^^^^^^
SyntaxError: Unexpected identifier
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)

After reading the documentation at https://nodejs.org/api/esm.html, I
ranamed the file from bt_test.js to bt_test.mjs and added the
--experimental-modules option to the run invocation.

Still I had an error:

$ node --experimental-modules bt_test.mjs
(node:19929) ExperimentalWarning: The ESM module loader is experimental.
file:///home/giacomo/DEV_experimental/bt_nodejs/bt_test.mjs:1
import BehaviorTree, { Sequence, Task, SUCCESS, FAILURE } from 'behaviortree'
                       ^^^^^^^^
SyntaxError: The requested module 'behaviortree' does not provide an export named 'Sequence'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:89:21)

I tried both nodejs version 9.11 and nodejs version 10.4.

Can anyone help me compiling and running the example above?

Hiho.

I will look deeper into this, in the evening. Just from a small look of it: It looks like it does not know the Syntax of course. Have you tried it using babel-node (included in the babel-cli npm-package?

I usually run the code using babel (also using webpack), and that does all the magic. But will try this evening to figure out, what can be run on simple node.js.

Thanks for bringing this up.

Sooooo. I have to say, the packaging I did was kinda shitty.

I changed the packaging now. So it produces better bundles that can be used by node.js as well as a babel setup which I use. But I don't have to make other use it to, if a simple node.js setup works. So now I dont.

Please use the '2.0.2' version and try again. If you need an example. I also created a repository with that example at https://github.com/Calamari/BehaviorTree.js-Examples .

I hope everything works for you. Please try it out, and note down here, if it did, or not.

Thank you for the hints and the examples' repository. They helped me
a lot to understand how to make behaviortree compile and run.

The examples in the repository
https://github.com/Calamari/BehaviorTree.js-Examples (both the es6 and
nodejs version) work out-of-the-box (also with npm - not only with
yarn).

I close the issue and start playing with BTs.

P.S.: a note on babel (for those, like me, who are new to transpiling
with babel)

I realized that I wasn't able to transpile and run the example because
I didn't have

  1. a module like "babel-preset-es2015" (npm install babel-preset-es2015)
    and

  2. a .babelrc file:

$ cat .babelrc 
{
  "presets": ["es2015"]
}

With that module and file, the command

$ babel-node bt_test.js

does the job without errors (provided that class Dog is defined in
bt_test.js).