t09_scripting.cpp
Go to the documentation of this file.
2 #include "dummy_nodes.h"
3 
4 using namespace BT;
5 
6 // clang-format off
7 static const char* xml_text = R"(
8  <root BTCPP_format="4">
9  <BehaviorTree>
10  <Sequence>
11  <Script code=" msg:='hello world' " />
12  <Script code=" A:=THE_ANSWER; B:=3.14; color:=RED " />
13  <Precondition if="A>-B && color != BLUE" else="FAILURE">
14  <Sequence>
15  <SaySomething message="{A}"/>
16  <SaySomething message="{B}"/>
17  <SaySomething message="{msg}"/>
18  <SaySomething message="{color}"/>
19  </Sequence>
20  </Precondition>
21  </Sequence>
22  </BehaviorTree>
23  </root>
24  )";
25 
26 // clang-format on
27 
28 int main()
29 {
30  BehaviorTreeFactory factory;
31  factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
32 
33  enum Color
34  {
35  RED = 1,
36  BLUE = 2,
37  GREEN = 3
38  };
39  // We can add these enums to the scripting language
40  factory.registerScriptingEnums<Color>();
41 
42  // Or we can do it manually
43  factory.registerScriptingEnum("THE_ANSWER", 42);
44 
45  auto tree = factory.createTreeFromText(xml_text);
46  tree.tickWhileRunning();
47 
48  return 0;
49 }
50 
51 /* Expected output:
52 
53 Robot says: 42.000000
54 Robot says: 3.140000
55 Robot says: hello world
56 Robot says: 1.000000
57 
58 */
BT
Definition: ex01_wrap_legacy.cpp:29
main
int main()
Definition: t09_scripting.cpp:28
bt_factory.h
BT::BehaviorTreeFactory::registerScriptingEnum
void registerScriptingEnum(StringView name, int value)
Add an Enum to the scripting language. For instance if you do:
Definition: bt_factory.cpp:451
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:610
dummy_nodes.h
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
Color
Color
Definition: gtest_enums.cpp:6
xml_text
static const char * xml_text
Definition: t09_scripting.cpp:7
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::BehaviorTreeFactory::registerScriptingEnums
void registerScriptingEnums()
registerScriptingEnums is syntactic sugar to automatically register multiple enums....
Definition: bt_factory.h:462


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17