Sequences node
序列节点,是所有子节点都返回成功,序列节点为成功,如果有其中一个子节点返回失败,则序列为失败
【原文链接】Sequence Nodes - BehaviorTree.CPP
(A Sequence ticks all its children as long as they return SUCCESS. If any child returns FAILURE, the sequence is aborted)
行为树提供以下类型的节点
- Sequence
- SequenceStar
- ReactiveSequence
它们有以下共同特点:
- 在tick第一个子节点,序列节点状态为Running。(Before ticking the first child, the node status becomes RUNNING.)
- 如果一个子节点返回成功,则继续ticks下一个子节点
- 如果最后一子个节点返回成功,并且所有子节点都执行完了,序列节点会返回成功(If the
last child returns SUCCESS too, all the children are 停止 and the sequence returns SUCCESS.)
这三种类型的区别如下
"Restart":重启从序列节点的第一个子节点开始(means that the entire sequence is restarted from the first child of the list.)
"Tick again" :下次tick时,之前已经返回成功的子节点不会被tick。(means that the next time the sequence is ticked, the same child is ticked again. Previous sibling, which returned SUCCESS already, are not ticked again.)
Sequence
按顺序执行,任何一个子节点失败都会重新开始
伪代码
status = RUNNING;
// _index is a private member
while(_index < number_of_children)
{
child_status = child[_index]->tick();
if( child_status == SUCCESS ) {
_index++;
}
else if( child_status == RUNNING ) {
// keep same index
return RUNNING;
}
else if( child_status == FAILURE ) {
HaltAllChildren();
_index = 0;
return FAILURE;
}
}
// all the children returned success. Return SUCCESS too.
HaltAllChildren();
_index = 0;
return SUCCESS;
ReactiveSequence
此节点特别适用于连续检查条件;但是用户在使用异步子节点时也应该小心,以确保它们不会比预期的更频繁地被勾选。(This node is particularly useful to continuously check Conditions; but the user should also be careful when using asynchronous children, to be sure that they are not ticked more often that expected.)
例子,看见敌人 接近敌人
接近敌人(ApproachEnemy)是一个异步操作,它会返回running,知道它最终完成
(ApproachEnemy is an asynchronous action that returns RUNNING until it is, eventually, completed.)
条件 敌人是否可见,可能会被调用很多次,如果它变成失败,则接近敌人
(The condition isEnemyVisible will be called many times and, if it becomes false (i,e, "FAILURE"), is halted.ApproachEnemy)
伪代码
status = RUNNING;
for (int index=0; index < number_of_children; index++)
{
child_status = child[index]->tick();
if( child_status == RUNNING ) {
return RUNNING;
}
else if( child_status == FAILURE ) {
HaltAllChildren();
return FAILURE;
}
}
// all the children returned success. Return SUCCESS too.
HaltAllChildren();
return SUCCESS;
SequenceStar
当返回成功的子节点不希望再次执行时,可以使用这个node。例如在多点任务巡航过程
去A 成功,去B失败,下次tick时不会再去A
(On the other hand, isBatteryOK must be checked at every tick, for this reason its parent must be a .ReactiveSequence)
伪代码
status = RUNNING;
// _index is a private member
while( index < number_of_children)
{
child_status = child[index]->tick();
if( child_status == SUCCESS ) {
_index++;
}
else if( child_status == RUNNING ||
child_status == FAILURE )
{
// keep same index
return child_status;
}
}
// all the children returned success. Return SUCCESS too.
HaltAllChildren();
_index = 0;
return SUCCESS;