Skip to content

Add close function into BehaviorTree class. #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/core/BehaviorTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,35 @@ export default class BehaviorTree {

return state;
}

/**
* Close all open nodes to ensure close function in nodes be called.
* If stop running tree tick via external, call this function.
* @param target
* @param blackboard
* @returns {*}
*/
close(target, blackboard) {
if (!blackboard) {
throw 'The blackboard parameter is obligatory and must be an ' +
'instance of b3.Blackboard';
}

/* CREATE A TICK OBJECT */
var tick = new Tick();
tick.debug = this.debug;
tick.target = target;
tick.blackboard = blackboard;
tick.tree = this;

/* CLOSE ALL OPEN NODES */
var lastOpenNodes = blackboard.get('openNodes', this.id);
for (var i = 0; i < lastOpenNodes.length; i++) {
lastOpenNodes[i]._close(tick);
}

/* POPULATE BLACKBOARD */
blackboard.set('openNodes', tick._openNodes.slice(0), this.id);
blackboard.set('nodeCount', tick._nodeCount, this.id);
}
};