Skip to content

Commit cacd698

Browse files
author
Davide Faconti
committed
docs update
1 parent f8c16a3 commit cacd698

File tree

6 files changed

+199
-2
lines changed

6 files changed

+199
-2
lines changed
File renamed without changes.
File renamed without changes.

docs/FallbackNode.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ControlNodes
2+
3+
ControlNodes can have multiple children. Children are always __ordered__
4+
and it is up to the ControlNode itself to decide if and when a child should
5+
be ticked.
6+
7+
## SequenceNode
8+
9+
The SequenceNode is used to execute the children in a sequence.
10+
11+
It ticks its children __as long as__ they returns SUCCESS.
12+
13+
- Before ticking the first child, Sequence becomes __RUNNING__.
14+
- If a child return __SUCCESS__, it ticks the next child.
15+
- If the __last__ child returns __SUCCESS__ too, all the children are halted and
16+
the Sequence returns __SUCCESS__.
17+
- If a child returns __RUNNING__, Sequence suspends and returns __RUNNING__.
18+
- If a child returns __FAILURE__, Sequence stops and returns __FAILURE__.
19+
20+
21+
22+
23+
24+
25+
26+

docs/SequenceNode.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Sequences
2+
3+
A __Sequence__ ticks all it's children, from left to right, as long as
4+
they return SUCCESS. If any child returns FAILURE, the sequence is suspended.
5+
6+
Here we introduce two kinds of TreeNodes:
7+
8+
- SequenceNode
9+
- SequenceStarNode
10+
- SequenceAllNode
11+
12+
The best way to determine which one should be used is to ask yourself:
13+
14+
Q: "What should I do if one of the childs returns FAILURE?"
15+
16+
Use __SequenceNode__ if you answer is:
17+
18+
A: "Restart the entire sequence"
19+
20+
Use __SequenceStarNode__ if, instead, the answer is:
21+
22+
23+
A: "Try again to execute the failed child.
24+
Do not re-tick children which succeeded already."
25+
26+
__SequenceAllNode__ is used when you want all the children to be ticked at least
27+
once. If any of them failed, the SequenceAllNode returns FAILURE.
28+
29+
The shared logic is:
30+
31+
- Before ticking the first child, SequenceNode becomes __RUNNING__.
32+
- If a child returns __SUCCESS__, it ticks the next child.
33+
- If the __last__ child returns __SUCCESS__ too, all the children are halted and
34+
the SequenceNode returns __SUCCESS__.
35+
- If a child returns __RUNNING__, Sequence suspends and returns __RUNNING__.
36+
The next time it is ticked, it will use the same index.
37+
38+
The three Sequences differ in what they do if a child returns FAILURE.
39+
40+
## SequenceNode
41+
If a child returns FAILURE, the sequence returns FAILURE.
42+
Reset the index and halt all the children.
43+
The entire sequence will be executed again at the next tick.
44+
45+
??? example "See the pseudocode"
46+
``` c++
47+
// At the beginning, start from first child
48+
if( state != RUNNING) {
49+
index = 0;
50+
}
51+
state = RUNNING;
52+
53+
while( index < number_of_children )
54+
{
55+
child_state = child[index]->tick();
56+
57+
if( child_state == RUNNING ) {
58+
// Suspend execution and return RUNNING.
59+
// At the next tick, index will be the same.
60+
state = RUNNING;
61+
return state;
62+
}
63+
else if( child_state == SUCCESS ) {
64+
// continue the while loop
65+
index++;
66+
}
67+
else if( child_state == FAILURE ) {
68+
// Suspend execution and return FAILURE.
69+
// index is reset and children are halted.
70+
state = FAILURE;
71+
index = 0;
72+
HaltAllChildren();
73+
return state;
74+
}
75+
}
76+
// all the children returned success. Return SUCCESS too.
77+
state = SUCCESS;
78+
HaltAllChildren();
79+
return state;
80+
```
81+
82+
83+
## SequenceStarNode
84+
85+
If a child returns FAILURE, the sequence returns FAILURE. At the next tick,
86+
the failed child is executed again.
87+
88+
??? example "See the pseudocode"
89+
``` c++
90+
91+
// index is initialized to 0 in the constructor
92+
state = RUNNING;
93+
94+
while( index < number_of_children )
95+
{
96+
child_state = child[index]->tick();
97+
98+
if( child_state == RUNNING ) {
99+
// Suspend execution and return RUNNING.
100+
// At the next tick, index will be the same.
101+
state = RUNNING;
102+
return state;
103+
}
104+
else if( child_state == SUCCESS ) {
105+
// continue the while loop
106+
index++;
107+
}
108+
else if( child_state == FAILURE ) {
109+
// Suspend execution and return FAILURE.
110+
// At the next tick, index will be the same.
111+
state = FAILURE;
112+
return state;
113+
}
114+
}
115+
// all the children returned success. Return SUCCESS too.
116+
state = SUCCESS;
117+
HaltAllChildren();
118+
return state;
119+
```
120+
121+
122+
## SequenceAllNode
123+
124+
All the children are executed at least once.
125+
If __any__ child returned FAILURE,
126+
the sequence is __not__ interrupted but the sequence itself will return FAILURE.
127+
128+
??? example "See the pseudocode"
129+
``` c++
130+
if( state != RUNNING) {
131+
index = 0;
132+
at_least_one_failure = false;
133+
}
134+
state = RUNNING;
135+
136+
while( index < number_of_children )
137+
{
138+
child_state = child[index]->tick();
139+
140+
if( child_state == RUNNING ) {
141+
// Suspend execution and return RUNNING.
142+
// At the next tick, index will be the same.
143+
state = RUNNING;
144+
return state;
145+
}
146+
else if( child_state == SUCCESS ) {
147+
index++;
148+
}
149+
else if( child_state == FAILURE ) {
150+
index++;
151+
at_least_one_failure = true;
152+
}
153+
}
154+
// If any child failed, the entire sequence fails.
155+
state = at_least_one_failure ? FAILURE : SUCCESS;
156+
HaltAllChildren();
157+
return state;
158+
```
159+
160+
161+
162+
163+
164+
165+
166+

docs/images/BT.png

6.19 KB
Loading

mkdocs.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ copyright: 'Copyright &copy; 2018 Davide Faconti, Eurecat'
77
theme:
88
name: 'material'
99
language: en
10+
logo: 'images/BT.png'
1011
feature:
1112
tabs: true
1213
palette:
@@ -22,14 +23,18 @@ repo_url: 'https://github.com/Eurecat/BehaviorTree.CPP'
2223
markdown_extensions:
2324
- admonition
2425
- pymdownx.details
26+
- pymdownx.superfences
27+
- codehilite:
28+
guess_lang: true
2529

2630

2731
pages:
2832
- Home: index.md
2933
- Learn the basics:
3034
- The Basics: BT_basics.md
31-
- Control Nodes: ControlNodes.md
32-
- Decorators Nodes: DecoratorNodes.md
35+
- Sequence Nodes: SequenceNode.md
36+
- Fallback Nodes: FallbackNode.md
37+
- Decorators Nodes: DecoratorNode.md
3338
- The BlackBoard: BlackBoard.md
3439
- Getting Started:
3540
- How to use the library: getting_started.md

0 commit comments

Comments
 (0)