Skip to content

Commit 689fb9f

Browse files
committed
Fix issue #978 : skiped was not working properly
1 parent 4b7177a commit 689fb9f

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/controls/fallback_node.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ NodeStatus FallbackNode::tick()
2828
{
2929
const size_t children_count = children_nodes_.size();
3030

31-
if(status() == NodeStatus::IDLE)
31+
if(!isStatusActive(status()))
3232
{
3333
skipped_count_ = 0;
3434
}
@@ -77,19 +77,22 @@ NodeStatus FallbackNode::tick()
7777
} // end while loop
7878

7979
// The entire while loop completed. This means that all the children returned FAILURE.
80+
const bool all_children_skipped = (skipped_count_ == children_count);
8081
if(current_child_idx_ == children_count)
8182
{
8283
resetChildren();
8384
current_child_idx_ = 0;
85+
skipped_count_ = 0;
8486
}
8587

8688
// Skip if ALL the nodes have been skipped
87-
return (skipped_count_ == children_count) ? NodeStatus::SKIPPED : NodeStatus::FAILURE;
89+
return (all_children_skipped) ? NodeStatus::SKIPPED : NodeStatus::FAILURE;
8890
}
8991

9092
void FallbackNode::halt()
9193
{
9294
current_child_idx_ = 0;
95+
skipped_count_ = 0;
9396
ControlNode::halt();
9497
}
9598

src/controls/sequence_node.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ SequenceNode::SequenceNode(const std::string& name, bool make_async)
2727
void SequenceNode::halt()
2828
{
2929
current_child_idx_ = 0;
30+
skipped_count_ = 0;
3031
ControlNode::halt();
3132
}
3233

3334
NodeStatus SequenceNode::tick()
3435
{
3536
const size_t children_count = children_nodes_.size();
3637

37-
if(status() == NodeStatus::IDLE)
38+
if(!isStatusActive(status()))
3839
{
3940
skipped_count_ = 0;
4041
}
@@ -86,13 +87,15 @@ NodeStatus SequenceNode::tick()
8687
} // end while loop
8788

8889
// The entire while loop completed. This means that all the children returned SUCCESS.
90+
const bool all_children_skipped = (skipped_count_ == children_count);
8991
if(current_child_idx_ == children_count)
9092
{
9193
resetChildren();
9294
current_child_idx_ = 0;
95+
skipped_count_ = 0;
9396
}
9497
// Skip if ALL the nodes have been skipped
95-
return (skipped_count_ == children_count) ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
98+
return (all_children_skipped) ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
9699
}
97100

98101
} // namespace BT

src/controls/sequence_with_memory_node.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ NodeStatus SequenceWithMemory::tick()
2525
{
2626
const size_t children_count = children_nodes_.size();
2727

28-
if(status() == NodeStatus::IDLE)
28+
if(!isStatusActive(status()))
2929
{
3030
skipped_count_ = 0;
3131
}
@@ -79,13 +79,15 @@ NodeStatus SequenceWithMemory::tick()
7979
} // end while loop
8080

8181
// The entire while loop completed. This means that all the children returned SUCCESS.
82+
const bool all_children_skipped = (skipped_count_ == children_count);
8283
if(current_child_idx_ == children_count)
8384
{
8485
resetChildren();
8586
current_child_idx_ = 0;
87+
skipped_count_ = 0;
8688
}
8789
// Skip if ALL the nodes have been skipped
88-
return (skipped_count_ == children_count) ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
90+
return (all_children_skipped) ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
8991
}
9092

9193
void SequenceWithMemory::halt()

tests/gtest_preconditions.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,34 @@ TEST(Preconditions, WhileCallsOnHalt)
474474
ASSERT_EQ(status, BT::NodeStatus::SKIPPED);
475475
ASSERT_EQ(tree.rootBlackboard()->get<int>("B"), 69);
476476
}
477+
478+
TEST(Preconditions, SkippedSequence)
479+
{
480+
const std::string xml_text = R"(
481+
<root BTCPP_format="4" >
482+
<BehaviorTree ID="MainTree">
483+
<Sequence>
484+
<AlwaysSuccess _skipIf="skip"/>
485+
</Sequence>
486+
</BehaviorTree>
487+
</root>)";
488+
489+
auto factory = BT::BehaviorTreeFactory();
490+
auto tree = factory.createTreeFromText(xml_text);
491+
492+
tree.rootBlackboard()->set("skip", true);
493+
auto status = tree.tickWhileRunning();
494+
ASSERT_EQ(status, BT::NodeStatus::SKIPPED);
495+
496+
tree.rootBlackboard()->set("skip", false);
497+
status = tree.tickWhileRunning();
498+
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
499+
500+
tree.rootBlackboard()->set("skip", true);
501+
status = tree.tickWhileRunning();
502+
ASSERT_EQ(status, BT::NodeStatus::SKIPPED);
503+
504+
tree.rootBlackboard()->set("skip", false);
505+
status = tree.tickWhileRunning();
506+
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
507+
}

0 commit comments

Comments
 (0)