Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9fec0a

Browse files
committed
halt the SwitchNode correctly
1 parent dce278e commit f9fec0a

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

include/behaviortree_cpp_v3/controls/switch_node.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class SwitchNode : public ControlNode
6161
template<size_t NUM_CASES> inline
6262
NodeStatus SwitchNode<NUM_CASES>::tick()
6363
{
64+
constexpr const char * case_port_names[9] = {
65+
"case_1", "case_2", "case_3", "case_4", "case_5", "case_6", "case_7", "case_8", "case_9"};
66+
6467
if( childrenCount() != NUM_CASES+1)
6568
{
6669
throw LogicError("Wrong number of children in SwitchNode; "
@@ -76,10 +79,18 @@ NodeStatus SwitchNode<NUM_CASES>::tick()
7679
// check each case until you find a match
7780
for (unsigned index = 0; index < NUM_CASES; ++index)
7881
{
79-
char case_str[20];
80-
sprintf(case_str, "case_%d", index+1);
82+
bool found = false;
83+
if( index < 9 )
84+
{
85+
found = (bool)getInput(case_port_names[index], value);
86+
}
87+
else{
88+
char case_str[20];
89+
sprintf(case_str, "case_%d", index+1);
90+
found = (bool)getInput(case_str, value);
91+
}
8192

82-
if (getInput(case_str, value) && variable == value)
93+
if (found && variable == value)
8394
{
8495
child_index = index;
8596
break;
@@ -90,18 +101,19 @@ NodeStatus SwitchNode<NUM_CASES>::tick()
90101
// if another one was running earlier, halt it
91102
if( running_child_ != -1 && running_child_ != child_index)
92103
{
93-
halt();
104+
children_nodes_[running_child_]->halt();
94105
}
95106

96-
NodeStatus ret = children_nodes_[child_index]->executeTick();
107+
auto& selected_child = children_nodes_[child_index];
108+
NodeStatus ret = selected_child->executeTick();
97109
if( ret == NodeStatus::RUNNING )
98110
{
99111
running_child_ = child_index;
100112
}
101113
else{
114+
selected_child->halt();
102115
running_child_ = -1;
103116
}
104-
105117
return ret;
106118
}
107119

0 commit comments

Comments
 (0)