Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/behaviortree_cpp/decorators/script_precondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class PreconditionNode : public DecoratorNode
}

private:
void halt() override
{
_children_running = false;
DecoratorNode::halt();
}

virtual BT::NodeStatus tick() override
{
loadExecutor();
Expand All @@ -72,19 +78,18 @@ class PreconditionNode : public DecoratorNode

// Only check the 'if' script if we haven't started ticking the children yet.
Ast::Environment env = { config().blackboard, config().enums };
bool tick_children =
_children_running || (_children_running = _executor(env).cast<bool>());
bool tick_children = _children_running || _executor(env).cast<bool>();

if(!tick_children)
{
return else_return;
}

auto const child_status = child_node_->executeTick();
_children_running = (child_status == NodeStatus::RUNNING);
if(isStatusCompleted(child_status))
{
resetChild();
_children_running = false;
}
return child_status;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/gtest_preconditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,53 @@ TEST(PreconditionsDecorator, ChecksConditionOnce)
EXPECT_EQ(tree.tickOnce(), NodeStatus::SUCCESS);
}

TEST(PreconditionsDecorator, ChecksConditionAfterHalt)
{
BehaviorTreeFactory factory;
factory.registerNodeType<KeepRunning>("KeepRunning");

const std::string xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Precondition if="A==0" else="FAILURE">
<KeepRunning/>
</Precondition>
</BehaviorTree>
</root>)";

auto tree = factory.createTreeFromText(xml_text);
tree.rootBlackboard()->set("A", 0);
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);

tree.haltTree();
tree.rootBlackboard()->set("A", 1);
EXPECT_EQ(tree.tickOnce(), NodeStatus::FAILURE);
}

TEST(PreconditionsDecorator, ChecksConditionAfterSkippedChild)
{
BehaviorTreeFactory factory;
factory.registerNodeType<KeepRunning>("KeepRunning");

const std::string xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Precondition if="A==0" else="FAILURE">
<KeepRunning _skipIf="skip"/>
</Precondition>
</BehaviorTree>
</root>)";

auto tree = factory.createTreeFromText(xml_text);
tree.rootBlackboard()->set("A", 0);
tree.rootBlackboard()->set("skip", true);
EXPECT_EQ(tree.tickOnce(), NodeStatus::SKIPPED);

tree.rootBlackboard()->set("A", 1);
tree.rootBlackboard()->set("skip", false);
EXPECT_EQ(tree.tickOnce(), NodeStatus::FAILURE);
}

TEST(PreconditionsDecorator, CanRunChildrenMultipleTimes)
{
BehaviorTreeFactory factory;
Expand Down
Loading