Skip to content

Commit 3e23456

Browse files
committed
Convert vector to vector<Any> before placing on the blackboard
1 parent e3b0cd7 commit 3e23456

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

include/behaviortree_cpp/blackboard.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,13 @@ inline void Blackboard::set(const std::string& key, const T& value)
257257

258258
std::type_index previous_type = entry.info.type();
259259

260+
// Allow mismatch if going from vector -> vector<Any>.
261+
bool previous_is_vector = std::string(previous_type.name()).find("vector") != std::string::npos;
262+
bool new_is_vector_any = new_value.type() == typeid(std::vector<Any>);
263+
260264
// check type mismatch
261-
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type())
265+
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type() &&
266+
!(previous_is_vector && new_is_vector_any))
262267
{
263268
bool mismatching = true;
264269
if(std::is_constructible<StringView, T>::value)

include/behaviortree_cpp/tree_node.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,19 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value)
619619
}
620620

621621
remapped_key = stripBlackboardPointer(remapped_key);
622-
config().blackboard->set(static_cast<std::string>(remapped_key), value);
622+
623+
if constexpr(is_vector<T>::value && !std::is_same_v<T, std::vector<Any>>)
624+
{
625+
// If the object is a vector but not a vector<Any>, convert it to vector<Any> before placing it on the blackboard.
626+
auto any_vec = std::vector<Any>();
627+
std::transform(value.begin(), value.end(), std::back_inserter(any_vec),
628+
[](auto &element) { return BT::Any(element); });
629+
config().blackboard->set(static_cast<std::string>(remapped_key), any_vec);
630+
}
631+
else
632+
{
633+
config().blackboard->set(static_cast<std::string>(remapped_key), value);
634+
}
623635

624636
return {};
625637
}

0 commit comments

Comments
 (0)