BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
try_catch_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#pragma once
14
15#include "behaviortree_cpp/control_node.h"
16
17namespace BT
18{
19/**
20 * @brief The TryCatch node executes children 1..N-1 as a Sequence ("try" block).
21 *
22 * If all children in the try-block succeed, this node returns SUCCESS.
23 *
24 * If any child in the try-block fails, the last child N is executed as a
25 * "catch" (cleanup) action, and this node returns FAILURE regardless of
26 * the catch child's result.
27 *
28 * - If a try-child returns RUNNING, this node returns RUNNING.
29 * - If a try-child returns SUCCESS, continue to the next try-child.
30 * - If a try-child returns FAILURE, enter catch mode and tick the last child.
31 * - If the catch child returns RUNNING, this node returns RUNNING.
32 * - When the catch child finishes (SUCCESS or FAILURE), this node returns FAILURE.
33 * - SKIPPED try-children are skipped over (not treated as failure).
34 *
35 * Port "catch_on_halt" (default false): if true, the catch child is also
36 * executed when the TryCatch node is halted while the try-block is RUNNING.
37 *
38 * Requires at least 2 children.
39 */
40class TryCatchNode : public ControlNode
41{
42public:
43 TryCatchNode(const std::string& name, const NodeConfig& config);
44
45 ~TryCatchNode() override = default;
46
47 TryCatchNode(const TryCatchNode&) = delete;
48 TryCatchNode& operator=(const TryCatchNode&) = delete;
49 TryCatchNode(TryCatchNode&&) = delete;
50 TryCatchNode& operator=(TryCatchNode&&) = delete;
51
52 static PortsList providedPorts()
53 {
54 return { InputPort<bool>("catch_on_halt", false,
55 "If true, execute the catch child when "
56 "the node is halted during the try-block") };
57 }
58
59 void halt() override;
60
61private:
62 size_t current_child_idx_ = 0;
63 size_t skipped_count_ = 0;
64 bool in_catch_ = false;
65
66 BT::NodeStatus tick() override;
67};
68
69} // namespace BT
The ControlNode is the base class for nodes that can have multiple children.
Definition: control_node.h:32
The TryCatch node executes children 1..N-1 as a Sequence ("try" block).
Definition: try_catch_node.h:41
void halt() override
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105