BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
subtree_node.h
1#ifndef DECORATOR_SUBTREE_NODE_H
2#define DECORATOR_SUBTREE_NODE_H
3
4#include "behaviortree_cpp/decorator_node.h"
5
6namespace BT
7{
8/**
9 * @brief The SubTreeNode is a way to wrap an entire Subtree,
10 * creating a separated BlackBoard.
11 * If you want to have data flow through ports, you need to explicitly
12 * remap the ports.
13 *
14 * NOTE: _autoremap will exclude all the ports which name start with underscore '_'
15 *
16 * Consider this example:
17
18<root main_tree_to_execute = "MainTree" >
19
20 <BehaviorTree ID="MainTree">
21 <Sequence>
22
23 <Script code="myParam='Hello'" />
24 <SubTree ID="Talk" param="{myParam}" />
25
26 <SubTree ID="Talk" param="World" />
27
28 <Script code="param='Auto remapped'" />
29 <SubTree ID="Talk" _autoremap="1" />
30
31 </Sequence>
32 </BehaviorTree>
33
34 <BehaviorTree ID="Talk">
35 <SaySomething message="{param}" />
36 </BehaviorTree>
37</root>
38
39 * You may notice three different approaches to remapping:
40 *
41 * 1) Subtree: "{param}" -> Parent: "{myParam}" -> Value: "Hello"
42 * Classical remapping from one port to another, but you need to use the syntax
43 * {myParam} to say that you are remapping the another port.
44 *
45 * 2) Subtree: "{param}" -> Value: "World"
46 * syntax without {}, in this case param directly point to the __string__ "World".
47 *
48 * 3) Subtree: "{param}" -> Parent: "{parent}"
49 * Setting to true (or 1) the attribute "_autoremap", we are automatically remapping
50 * each port. Useful to avoid boilerplate.
51 */
52class SubTreeNode : public DecoratorNode
53{
54public:
55 SubTreeNode(const std::string& name, const NodeConfig& config);
56
57 ~SubTreeNode() override = default;
58
59 SubTreeNode(const SubTreeNode&) = delete;
60 SubTreeNode& operator=(const SubTreeNode&) = delete;
61 SubTreeNode(SubTreeNode&&) = delete;
62 SubTreeNode& operator=(SubTreeNode&&) = delete;
63
64 static PortsList providedPorts();
65
66 void setSubtreeID(const std::string& ID)
67 {
68 subtree_id_ = ID;
69 }
70
71 const std::string& subtreeID() const
72 {
73 return subtree_id_;
74 }
75 virtual BT::NodeStatus tick() override;
76
77 virtual NodeType type() const override final
78 {
79 return NodeType::SUBTREE;
80 }
81
82private:
83 std::string subtree_id_;
84};
85
86} // namespace BT
87
88#endif // DECORATOR_SUBTREE_NODE_H
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The SubTreeNode is a way to wrap an entire Subtree, creating a separated BlackBoard....
Definition: subtree_node.h:53
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:21
Definition: tree_node.h:105