Skip to content

Commit 9bce572

Browse files
committed
add t19
1 parent e153bdb commit 9bce572

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ endif()
4242

4343
CompileExample("t17_blackboard_backup")
4444
CompileExample("t18_waypoints")
45+
CompileExample("t19_global_blackboard")
4546

4647
CompileExample("ex01_wrap_legacy")
4748
CompileExample("ex02_runtime_ports")

examples/t19_global_blackboard.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "behaviortree_cpp/bt_factory.h"
2+
3+
using namespace BT;
4+
5+
/**
6+
* This example introduces the concept of a "global blackboard",
7+
* and the syntax to use it.
8+
*
9+
* As you know know from previous tutorials, blackboard are "scoped",
10+
* i.e. each SubTree (including the one in the root) has its own
11+
* Blackboard, isolated by default, unless we do remapping.
12+
*
13+
* It is possible (since version 4.6) to create a global BB,
14+
* accessible from everywhere without remapping.
15+
*
16+
* In the example below we can access the entry "value" and
17+
* "value_sqr" from everywhere, as long as we use the pregix "@".
18+
*
19+
* Note as <SubTree ID="MySub"/> doesn't have any remapping
20+
*
21+
* In other words, the prefix "@" means: "search the entry in the top-level
22+
* blackboard of the hierarchy".
23+
*
24+
* In this case, the top-level blackboard will be [global_blackboard].
25+
*/
26+
27+
// clang-format off
28+
static const char* xml_main = R"(
29+
<root BTCPP_format="4">
30+
31+
<BehaviorTree ID="MainTree">
32+
<Sequence>
33+
<PrintNumber name="main_print" val="{@value}" />
34+
<SubTree ID="MySub"/>
35+
</Sequence>
36+
</BehaviorTree>
37+
38+
<BehaviorTree ID="MySub">
39+
<Sequence>
40+
<PrintNumber name="sub_print" val="{@value}" />
41+
<Script code="@value_sqr := @value * @value" />
42+
</Sequence>
43+
</BehaviorTree>
44+
</root>
45+
)";
46+
47+
// clang-format on
48+
49+
class PrintNumber : public BT::SyncActionNode
50+
{
51+
public:
52+
PrintNumber(const std::string& name, const BT::NodeConfig& config)
53+
: BT::SyncActionNode(name, config)
54+
{}
55+
56+
// You must override the virtual function tick()
57+
NodeStatus tick() override
58+
{
59+
const int val = getInput<int>("val").value();
60+
std::cout << "[" << name() << "] val: " << val << std::endl;
61+
return NodeStatus::SUCCESS;
62+
}
63+
64+
// It is mandatory to define this static method.
65+
static BT::PortsList providedPorts()
66+
{
67+
return { BT::InputPort<int>("val") };
68+
}
69+
};
70+
71+
//---------------------------------------------------
72+
int main()
73+
{
74+
BehaviorTreeFactory factory;
75+
76+
factory.registerNodeType<PrintNumber>("PrintNumber");
77+
factory.registerBehaviorTreeFromText(xml_main);
78+
79+
// No one "own" this blackboard
80+
auto global_blackboard = BT::Blackboard::create();
81+
// This blackboard will be owned by "MainTree". It parent is global_blackboard
82+
auto root_blackboard = BT::Blackboard::create(global_blackboard);
83+
84+
auto tree = factory.createTree("MainTree", root_blackboard);
85+
86+
for(int i = 1; i <= 3; i++)
87+
{
88+
global_blackboard->set("value", i);
89+
tree.tickOnce();
90+
int value_sqr = global_blackboard->get<int>("value_sqr");
91+
std::cout << "[While loop] value: " << i << " value_sqr: " << value_sqr << "\n\n";
92+
}
93+
94+
return 0;
95+
}
96+
97+
/* Expecte output:
98+
99+
[main_print] val: 1
100+
[sub_print] val: 1
101+
[While loop] value: 1 value_sqr: 1
102+
103+
[main_print] val: 2
104+
[sub_print] val: 2
105+
[While loop] value: 2 value_sqr: 4
106+
107+
[main_print] val: 3
108+
[sub_print] val: 3
109+
[While loop] value: 3 value_sqr: 9
110+
111+
*/

0 commit comments

Comments
 (0)