Skip to content

Commit f42cd38

Browse files
authored
Fix LoopNode does not reload dynamic vector input on second execution (#1137)
* Fix LoopNode does not reload dynamic vector input on second execution I've implemented the suggestion from #1124 This seems to have fixed the issue I had with LoopNode not reloading the input vector on second execution. * Add unittest for restarting LoopNode with a vector from the blackboard
1 parent df3228a commit f42cd38

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

include/behaviortree_cpp/decorators/loop_node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class LoopNode : public DecoratorNode
5959
if(status() == NodeStatus::IDLE)
6060
{
6161
child_running_ = false;
62+
current_queue_.reset();
63+
6264
// special case: the port contains a string that was converted to SharedQueue<T>
6365
if(static_queue_)
6466
{

tests/gtest_loop.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,42 @@ TEST(LoopNode, RestartAfterCompletion)
410410
ASSERT_EQ(tick_count, 3); // Should iterate over queue again
411411
}
412412

413+
TEST(LoopNode, RestartAfterCompletion_VectorFromBlackboard)
414+
{
415+
BehaviorTreeFactory factory;
416+
417+
int tick_count = 0;
418+
factory.registerSimpleAction("CountTicks", [&tick_count](TreeNode&) {
419+
tick_count++;
420+
return NodeStatus::SUCCESS;
421+
});
422+
423+
const std::string xml_text = R"(
424+
<root BTCPP_format="4">
425+
<BehaviorTree>
426+
<LoopInt queue="{my_vector}" value="{val}">
427+
<CountTicks/>
428+
</LoopInt>
429+
</BehaviorTree>
430+
</root>)";
431+
432+
auto tree = factory.createTreeFromText(xml_text);
433+
tree.rootBlackboard()->set("my_vector", std::vector{ 1, 2, 3 });
434+
435+
// First execution
436+
auto status = tree.tickWhileRunning();
437+
ASSERT_EQ(status, NodeStatus::SUCCESS);
438+
ASSERT_EQ(tick_count, 3);
439+
440+
// Reset and execute again
441+
tree.haltTree();
442+
tick_count = 0;
443+
tree.rootBlackboard()->set("my_vector", std::vector{ 1, 2, 3 });
444+
status = tree.tickWhileRunning();
445+
ASSERT_EQ(status, NodeStatus::SUCCESS);
446+
ASSERT_EQ(tick_count, 3); // Should iterate over vector again
447+
}
448+
413449
// ============ convertFromString tests for SharedQueue ============
414450

415451
TEST(LoopNode, ConvertFromString_Int)

0 commit comments

Comments
 (0)