BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
set_blackboard_node.h
1/* Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
2*
3* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7*
8* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11*/
12
13#ifndef ACTION_SETBLACKBOARD_NODE_H
14#define ACTION_SETBLACKBOARD_NODE_H
15
16#include "behaviortree_cpp/action_node.h"
17
18namespace BT
19{
20/**
21 * @brief The SetBlackboard is action used to store a string
22 * into an entry of the Blackboard specified in "output_key".
23 *
24 * Example usage:
25 *
26 * <SetBlackboard value="42" output_key="the_answer" />
27 *
28 * Will store the string "42" in the entry with key "the_answer".
29 *
30 * Alternatively, you can use it to copy one port inside another port:
31 *
32 * <SetBlackboard value="{src_port}" output_key="dst_port" />
33 *
34 * This will copy the type and content of {src_port} into {dst_port}
35 */
37{
38public:
39 SetBlackboardNode(const std::string& name, const NodeConfig& config)
40 : SyncActionNode(name, config)
41 {
42 setRegistrationID("SetBlackboard");
43 }
44
45 static PortsList providedPorts()
46 {
47 return { InputPort("value", "Value to be written into the output_key"),
48 BidirectionalPort("output_key", "Name of the blackboard entry where the "
49 "value should be written") };
50 }
51
52private:
53 virtual BT::NodeStatus tick() override
54 {
55 std::string output_key;
56 if(!getInput("output_key", output_key))
57 {
58 throw RuntimeError("missing port [output_key]");
59 }
60
61 const std::string value_str = config().input_ports.at("value");
62
63 StringView stripped_key;
64 BT::Any out_value;
65
66 std::shared_ptr<Blackboard::Entry> dst_entry =
67 config().blackboard->getEntry(output_key);
68
69 if(isBlackboardPointer(value_str, &stripped_key))
70 {
71 const auto input_key = std::string(stripped_key);
72 std::shared_ptr<Blackboard::Entry> src_entry =
73 config().blackboard->getEntry(input_key);
74
75 if(!src_entry)
76 {
77 throw RuntimeError("Can't find the port referred by [value]");
78 }
79 if(!dst_entry)
80 {
81 config().blackboard->createEntry(output_key, src_entry->info);
82 dst_entry = config().blackboard->getEntry(output_key);
83 }
84
85 out_value = src_entry->value;
86 }
87 else
88 {
89 out_value = BT::Any(value_str);
90 }
91
92 if(out_value.empty())
93 return NodeStatus::FAILURE;
94
95 // avoid type issues when port is remapped: current implementation of the set might be a little bit problematic for initialized on the fly values
96 // this still does not attack math issues
97 if(dst_entry && dst_entry->info.type() != typeid(std::string) && out_value.isString())
98 {
99 try
100 {
101 out_value = dst_entry->info.parseString(out_value.cast<std::string>());
102 }
103 catch(const std::exception& e)
104 {
105 throw LogicError("Can't convert string [", out_value.cast<std::string>(),
106 "] to type [", BT::demangle(dst_entry->info.type()),
107 "]: ", e.what());
108 }
109 }
110
111 config().blackboard->set(output_key, out_value);
112
113 return NodeStatus::SUCCESS;
114 }
115};
116} // namespace BT
117
118#endif
Definition: safe_any.hpp:50
The Blackboard is the mechanism used by BehaviorTrees to exchange typed data.
Definition: blackboard.h:35
The SetBlackboard is action used to store a string into an entry of the Blackboard specified in "outp...
Definition: set_blackboard_node.h:37
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:57
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: blackboard.h:51
Definition: tree_node.h:105