行為樹(四)BT的選擇節點
阿新 • • 發佈:2020-12-19
Fallback
該節點家族在其他框架中被稱為“選擇器Selector”或“優先順序Priority”。
他們的目的是嘗試不同的策略,直到找到可行的策略。
它們具有以下規則:
tick
第一個孩子之前,節點狀態為RUNNING
。- 如果子節點返回
FAILURE
,則後備會tick
下一個節點。 - 如果最後一個子節點也返回
FAILURE
,則所有子節點都將暫停,並且序列將返回FAILURE
。 - 如果子節點返回
SUCCESS
,它將停止並返回SUCCESS
。 所有的子節點都停止了。
當孩子返回RUNNING
時,Fallback
的兩個版本的反應方式不同:
FallbackStar
將返回RUNNING
,並且下次對其進行tick
tick
。- 普通的舊
Fallback
會返回RUNNING
,並且每次執行後都會重置下一個要執行的子級的索引。
Fallback
在此示例中,我們嘗試不同的策略來開啟大門。 首先(和一次)檢查門是否開啟。
// index is initialized to 0 in the constructor status = RUNNING; while( _index < number_of_children ) { child_status = child[index]->tick(); if( child_status == RUNNING ) { // Suspend execution and return RUNNING. // At the next tick, _index will be the same. return RUNNING; } else if( child_status == FAILURE ) { // continue the while loop _index++; } else if( child_status == SUCCESS ) { // Suspend execution and return SUCCESS. HaltAllChildren(); _index = 0; return SUCCESS; } } // all the children returned FAILURE. Return FAILURE too. index = 0; HaltAllChildren(); return FAILURE;
ReactiveFallback
如果先前條件之一將其狀態從FAILURE
更改為SUCCESS
,則當您想中斷非同步子項時,將使用此ControlNode:ReactiveFallback
。
在以下示例中,如果角色充分休息,則該角色最多可睡8個小時或更短的時間。
虛擬碼
// index is initialized to 0 in the constructor 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 ) { // continue the while loop index++; } else if( child_status == SUCCESS ) { // Suspend execution and return SUCCESS. // At the next tick, index will be the same. HaltAllChildren(); return SUCCESS; } } // all the children returned FAILURE. Return FAILURE too. index = 0; HaltAllChildren(); return FAILURE;