Tutorial 7: Load multiple XMLs

翻译自:https://www.behaviortree.dev

How to use multiple XML files to store your subtrees [如何使用多个 XML 文件来存储您的子树]

In the examples which we presented so far, we always create an entire Tree from a single XML file. [在我们目前提供的示例中,我们总是从单个 XML 文件创建一整棵树。]

If multiple Subtrees were used, they were all included into the same XML. [如果使用了多个子树,它们都将包含在同一个 XML 中。]

In recent version of BT.CPP (3.7+), the user can more easily load trees from multiple files, if needed. [在最新版本的 BT.CPP (3.7+) 中,如果需要,用户可以更轻松地从多个文件加载树。]

Load multiple files with “include”

We will consider a main tree that invoke 2 different subtrees. [我们将考虑调用 2 个不同子树的主树。]

File main_tree.xml:

<root main_tree_to_execute = "MainTree">
    <include path="./subtree_A.xml" />
    <include path="./subtree_B.xml" />
    <BehaviorTree ID="MainTree">
        <Sequence>
            <SaySomething message="starting MainTree" />
            <SubTree ID="SubTreeA" />
            <SubTree ID="SubTreeB" />
        </Sequence>
    </BehaviorTree>
<root>

File subtree_A.xml:

<root>
    <BehaviorTree ID="SubTreeA">
        <SaySomething message="Executing Sub_A" />
    </BehaviorTree>
</root>

File subtree_B.xml:

<root>
    <BehaviorTree ID="SubTreeB">
        <SaySomething message="Executing Sub_B" />
    </BehaviorTree>
</root>

As you may notice, we included two relative paths in main_tree.xml that tells to BehaviorTreeFactory where to find the required dependencies. [您可能会注意到,我们在 main_tree.xml 中包含了两个相对路径,它们告诉 BehaviorTreeFactory 在哪里可以找到所需的依赖项。]

We need to create the tree as usual: [我们需要像往常一样创建树:]

factory.createTreeFromFile("main_tree.xml")

Load multiple files manually

If we don’t want to add relative and hard-coded paths into our XML, or if we want to instantiate a subtree instead of the main tree, there is a new approach, since BT.CPP 3.7+. [如果我们不想将相对路径和硬编码路径添加到我们的 XML 中,或者如果我们想实例化一个子树而不是主树,那么自 BT.CPP 3.7+ 起就有一种新方法。]

The simplified version of main_tree.xml will be: [main_tree.xml 的简化版本将是:]

<root>
    <BehaviorTree ID="MainTree">
        <Sequence>
            <SaySomething message="starting MainTree" />
            <SubTree ID="SubTreeA" />
            <SubTree ID="SubTreeB" />
        </Sequence>
    </BehaviorTree>
<root>

To load manually the multiple files: [手动加载多个文件:]

int main()
{
    BT::BehaviorTreeFactory factory;
    factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");

    // Register the behavior tree definitions, but don't instantiate them, yet.
    // Order is not important.
    factory.registerBehaviorTreeFromText("main_tree.xml");
    factory.registerBehaviorTreeFromText("subtree_A.xml");
    factory.registerBehaviorTreeFromText("subtree_B.xml");

    //Check that the BTs have been registered correctly
    std::cout << "Registered BehaviorTrees:" << std::endl;
    for(const std::string& bt_name: factory.registeredBehaviorTrees())
    {
        std::cout << " - " << bt_name << std::endl;
    }

    // You can create the MainTree and the subtrees will be added automatically.
    std::cout << "----- MainTree tick ----" << std::endl;
    auto main_tree = factory.createTree("MainTree");
    main_tree.tickRoot();

    // ... or you can create only one of the subtree
    std::cout << "----- SubA tick ----" << std::endl;
    auto subA_tree = factory.createTree("SubTreeA");
    subA_tree.tickRoot();

    return 0;
}
/* Expected output:

Registered BehaviorTrees:
 - MainTree
 - SubTreeA
 - SubTreeB
----- MainTree tick ----
Robot says: starting MainTree
Robot says: Executing Sub_A
Robot says: Executing Sub_B
----- SubA tick ----
Robot says: Executing Sub_A
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值