BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
run_once_node.h
1/* Copyright (C) 2023-2025 Davide Faconti - All Rights Reserved
2* Copyright (C) 2022 Gaël Écorchard, Czech Institute of Informatics, Robotics, and Cybernetics (ciirc) <gael.ecorchard@cvut.cz>
3*
4* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6* 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:
7* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8*
9* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10* 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,
11* 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.
12*/
13
14#pragma once
15
16#include "behaviortree_cpp/decorator_node.h"
17
18namespace BT
19{
20
21/**
22 * @brief The RunOnceNode is used when you want to execute the child
23 * only once.
24 * If the child is asynchronous, we will tick until either SUCCESS or FAILURE is
25 * returned.
26 *
27 * After that first execution, you can set value of the port "then_skip" to:
28 *
29 * - if TRUE (default), the node will be skipped in the future.
30 * - if FALSE, return synchronously the same status returned by the child, forever.
31 */
32class RunOnceNode : public DecoratorNode
33{
34public:
35 RunOnceNode(const std::string& name, const NodeConfig& config)
36 : DecoratorNode(name, config)
37 {
38 setRegistrationID("RunOnce");
39 }
40
41 static PortsList providedPorts()
42 {
43 return { InputPort<bool>("then_skip", true,
44 "If true, skip after the first execution, "
45 "otherwise return the same NodeStatus returned once by the "
46 "child.") };
47 }
48
49private:
50 virtual BT::NodeStatus tick() override;
51
52 bool already_ticked_ = false;
53 NodeStatus returned_status_ = NodeStatus::IDLE;
54};
55
56//------------ implementation ----------------------------
57
58inline NodeStatus RunOnceNode::tick()
59{
60 bool skip = true;
61 if(auto const res = getInput<bool>("then_skip"))
62 {
63 skip = res.value();
64 }
65
66 if(already_ticked_)
67 {
68 return skip ? NodeStatus::SKIPPED : returned_status_;
69 }
70
71 setStatus(NodeStatus::RUNNING);
72 const NodeStatus status = child_node_->executeTick();
73
74 if(isStatusCompleted(status))
75 {
76 already_ticked_ = true;
77 returned_status_ = status;
78 resetChild();
79 }
80 return status;
81}
82
83} // namespace BT
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The RunOnceNode is used when you want to execute the child only once. If the child is asynchronous,...
Definition: run_once_node.h:33
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
void setStatus(NodeStatus new_status)
setStatus changes the status of the node. it will throw if you try to change the status to IDLE,...
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105