Custom behavior(extends Sequece) is not working fine


#1

Hi

I’m writing custom behavior 'SequenceIf’
This behavior is same to Sequence of core but skip with succeess when condition is false.
So I extended jibo.bt.behaviors.Sequence and overrode start function
but when I call super.start() on overried function, Sequence is not working.
And I tried to copy start source of Sequence of jibo.js to my start function instead of call super.start()
This copy is working fine.
What is the difference?
How can I make super.start call works fine?

Thanks

# SequenceIf.js

"use strict";

let jibo = require('jibo');
let Sequence = jibo.bt.behaviors.Sequence;

class SequenceIf extends Sequence {
  start() {
    if( this.options.condition() == false ) {
        return !0;
    }
    
    var call_parent = true;
    if( call_parent == true ) {    <= Parent start call. as a result NG
      super.start();    
    } else {      <= Just copy start function source from jibo.js OK
      if (0 === this.children.length) return !0;   
      this.current = 0;
      var t = this.children[this.current]._start();
      var res = this.current = t ? this.current : -1, t
      return this.current = t ? this.current : -1, t
    }
  }
}

jibo.bt.register('SequenceIf', 'project', SequenceIf);
module.exports = SequenceIf;

#2

When you say you are looking to create a behavior that is the same as the “Sequence of core” do you are looking to create a custom parent behavior that works just like our default Sequence behavior but with a built in decorator succeed on false decorator?

If that is what you are looking to do I would like to confirm a couple of things.

First, I would like to confirm that the code you provided above (SequenceIf.js) is the code you NOT seeing work. Is that correct?

If that is the case, because each custom behavior requires both a .js and a .json file (as mentioned in our docs here) I would like to look at the .json file you created for this behavior as well. If you could send the code for that as well it may help us provide some further guidance.

Also, if possible could you also provide the .js and .json file for the custom behavior you created that was working. That would provide a point of comparison that will also help us provide some direction for you.

Thank you! :slight_smile:


#3

Hi

When you say you are looking to create a behavior that is the same as the “Sequence of core” do you are looking to create a custom parent behavior that works just like our default Sequence behavior but with a built in decorator succeed on false decorator?

I had searched the combination of core behaviors for my scenario but could not find.
Particulary switch/case combination is recommended on this article
So I started to make my custom one

First, I would like to confirm that the code you provided above (SequenceIf.js) is the code you NOT seeing work. Is that correct?

in my code above,
if call_parent = true, then call super.start => NG
otherwise, copy jibo.js’s code => OK

If that is the case, because each custom behavior requires both a .js and a .json file (as mentioned in our docs here) I would like to look at the .json file you created for this behavior as well. If you could send the code for that as well it may help us provide some further guidance.

SequenceIf.json

{
    "SequenceIf": {
        "description": "This behavior is extention of Sequence behavior. If condition returns true, then same with Sequence. Otherwise skips child behaviors",
        "type": "composite",
        "options": [
            {
              "type": "function",
              "field": "condition",
              "description": "If return false, then skips child behaviors",
              "defaultValue": "() => {\n}"
            }
        ]
    },
    "meta": {
        "version": 1
    }
}

Also, if possible could you also provide the .js and .json file for the custom behavior you created that was working. That would provide a point of comparison that will also help us provide some direction for you.

See above.


#4

Hi thank you very much for the extra detail!

I may need a little extra detail on why you are looking to use super.start();

It sounds like your current goal is to create a sequence that is skipped if its condition returns false. As you mentioned, that can be accomplished by removing the super.start(); part of your custom code.

So, as you said, the following code can be used creates a sequence that, if its conditional is false, skips all of its children without the sequence itself failing:

"use strict";

let jibo = require('jibo');
let Sequence = jibo.bt.behaviors.Sequence;

class SequenceIf extends Sequence {
  start() {
    if( this.options.condition() == false ) {
        return !0;
    }

      if (0 === this.children.length) return !0;
      this.current = 0;
      var t = this.children[this.current]._start();
      var res = this.current = t ? this.current : -1, t
      return this.current = t ? this.current : -1, t
  }
}

jibo.bt.register('SequenceIf', 'project', SequenceIf);
module.exports = SequenceIf;

If you could provide me with a little more detail on what you are looking to accomplish with using super.start(); along with the above setup, that may help me point you in the right direction. :slight_smile: