t08_additional_node_args.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
5 /*
6  * Sometimes, it is convenient to pass additional (static) arguments to a Node.
7  * If these parameters are known at compilation time or at deployment-time
8  * and they don't change at run-time, input ports are probably overkill and cumbersome.
9  *
10  * This tutorial demonstrates two possible ways to initialize a custom node with
11  * additional arguments.
12  */
13 
14 // Action_A has a different constructor than the default one.
15 class Action_A : public SyncActionNode
16 {
17 public:
18  // additional arguments passed to the constructor
19  Action_A(const std::string& name, const NodeConfiguration& config, int arg1,
20  double arg2, std::string arg3) :
21  SyncActionNode(name, config), _arg1(arg1), _arg2(arg2), _arg3(arg3)
22  {}
23 
24  NodeStatus tick() override
25  {
26  std::cout << "Action_A: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
27  return NodeStatus::SUCCESS;
28  }
30  {
31  return {};
32  }
33 
34 private:
35  int _arg1;
36  double _arg2;
37  std::string _arg3;
38 };
39 
40 // Action_B implements an init(...) method that must be called once at the beginning.
41 class Action_B : public SyncActionNode
42 {
43 public:
44  Action_B(const std::string& name, const NodeConfiguration& config) :
45  SyncActionNode(name, config)
46  {}
47 
48  // we want this method to be called ONCE and BEFORE the first tick()
49  void init(int arg1, double arg2, std::string arg3)
50  {
51  _arg1 = (arg1);
52  _arg2 = (arg2);
53  _arg3 = (arg3);
54  }
55 
56  NodeStatus tick() override
57  {
58  std::cout << "Action_B: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
59  return NodeStatus::SUCCESS;
60  }
62  {
63  return {};
64  }
65 
66 private:
67  int _arg1;
68  double _arg2;
69  std::string _arg3;
70 };
71 
72 // Simple tree, used to execute once each action.
73 static const char* xml_text = R"(
74 
75  <root >
76  <BehaviorTree>
77  <Sequence>
78  <Action_A/>
79  <Action_B/>
80  </Sequence>
81  </BehaviorTree>
82  </root>
83  )";
84 
85 int main()
86 {
87  BehaviorTreeFactory factory;
88 
89  // A node builder is nothing more than a function pointer to create a
90  // std::unique_ptr<TreeNode>.
91  // Using lambdas or std::bind, we can easily "inject" additional arguments.
92  NodeBuilder builder_A = [](const std::string& name, const NodeConfiguration& config) {
93  return std::make_unique<Action_A>(name, config, 42, 3.14, "hello world");
94  };
95 
96  // BehaviorTreeFactory::registerBuilder is the more general way to register a custom node.
97  // Not the most user friendly, but definitely the most flexible one.
98  factory.registerBuilder<Action_A>("Action_A", builder_A);
99 
100  // The regitration of Action_B is done as usual, but we still need to call Action_B::init()
101  factory.registerNodeType<Action_B>("Action_B");
102 
103  auto tree = factory.createTreeFromText(xml_text);
104 
105  // Iterate through all the nodes and call init if it is an Action_B
106  for (auto& node : tree.nodes)
107  {
108  if (auto action_B_node = dynamic_cast<Action_B*>(node.get()))
109  {
110  action_B_node->init(69, 9.99, "interesting_value");
111  }
112  }
113 
114  tree.tickRootWhileRunning();
115 
116  /* Expected output:
117 
118  Action_A: 42 / 3.14 / hello world
119  Action_B: 69 / 9.99 / interesting_value
120  */
121  return 0;
122 }
NodeStatus tick() override
Method to be implemented by the user.
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeConfiguration &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
Definition: bt_factory.h:32
Action_A(const std::string &name, const NodeConfiguration &config, int arg1, double arg2, std::string arg3)
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
static PortsList providedPorts()
static const char * xml_text
static PortsList providedPorts()
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
void init(int arg1, double arg2, std::string arg3)
NodeStatus tick() override
Method to be implemented by the user.
NodeStatus
Definition: basic_types.h:35
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
Definition: bt_factory.cpp:91
Action_B(const std::string &name, const NodeConfiguration &config)


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14