Go to the documentation of this file.00001 #include "behaviortree_cpp/bt_factory.h"
00002
00003 using namespace BT;
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 class Action_A: public SyncActionNode
00016 {
00017
00018 public:
00019
00020 Action_A(const std::string& name, const NodeConfiguration& config,
00021 int arg1, double arg2, std::string arg3 ):
00022 SyncActionNode(name, config),
00023 _arg1(arg1),
00024 _arg2(arg2),
00025 _arg3(arg3) {}
00026
00027 NodeStatus tick() override
00028 {
00029 std::cout << "Action_A: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
00030 return NodeStatus::SUCCESS;
00031 }
00032 static PortsList providedPorts() { return {}; }
00033
00034 private:
00035 int _arg1;
00036 double _arg2;
00037 std::string _arg3;
00038 };
00039
00040
00041 class Action_B: public SyncActionNode
00042 {
00043
00044 public:
00045 Action_B(const std::string& name, const NodeConfiguration& config):
00046 SyncActionNode(name, config) {}
00047
00048
00049 void init( int arg1, double arg2, std::string arg3 )
00050 {
00051 _arg1 = (arg1);
00052 _arg2 = (arg2);
00053 _arg3 = (arg3);
00054 }
00055
00056 NodeStatus tick() override
00057 {
00058 std::cout << "Action_B: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
00059 return NodeStatus::SUCCESS;
00060 }
00061 static PortsList providedPorts() { return {}; }
00062
00063 private:
00064 int _arg1;
00065 double _arg2;
00066 std::string _arg3;
00067 };
00068
00069
00070 static const char* xml_text = R"(
00071
00072 <root >
00073 <BehaviorTree>
00074 <Sequence>
00075 <Action_A/>
00076 <Action_B/>
00077 </Sequence>
00078 </BehaviorTree>
00079 </root>
00080 )";
00081
00082 int main()
00083 {
00084 BehaviorTreeFactory factory;
00085
00086
00087
00088
00089 NodeBuilder builder_A = [](const std::string& name, const NodeConfiguration& config)
00090 {
00091 return std::make_unique<Action_A>( name, config, 42, 3.14, "hello world" );
00092 };
00093
00094
00095
00096 factory.registerBuilder<Action_A>( "Action_A", builder_A);
00097
00098
00099 factory.registerNodeType<Action_B>( "Action_B" );
00100
00101 auto tree = factory.createTreeFromText(xml_text);
00102
00103
00104 for( auto& node: tree.nodes )
00105 {
00106 if( auto action_B_node = dynamic_cast<Action_B*>( node.get() ))
00107 {
00108 action_B_node->init( 69, 9.99, "interesting_value" );
00109 }
00110 }
00111
00112 tree.root_node->executeTick();
00113
00114
00115
00116
00117
00118
00119 return 0;
00120 }