BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
condition_node.h
1/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
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#ifndef CONDITIONNODE_H
15#define CONDITIONNODE_H
16
17#include "leaf_node.h"
18
19namespace BT
20{
21/**
22 * @brief The ConditionNode is a leaf node used to check a condition.
23 *
24 * Unlike ActionNodes, a ConditionNode should NOT alter the system or have side-effects.
25 * and should NOT return RUNNING. It should return SUCCESS or FAILURE
26 * synchronously to indicate whether a condition is met.
27 *
28 * Conditions are typically used to check the state of the world or
29 * the system before performing an action (e.g., "IsDoorOpen", "IsBatteryLow").
30 */
31class ConditionNode : public LeafNode
32{
33public:
34 ConditionNode(const std::string& name, const NodeConfig& config);
35
36 ~ConditionNode() override = default;
37
38 ConditionNode(const ConditionNode&) = delete;
39 ConditionNode& operator=(const ConditionNode&) = delete;
40 ConditionNode(ConditionNode&&) = delete;
41 ConditionNode& operator=(ConditionNode&&) = delete;
42
43 //Do nothing
44 virtual void halt() override final
45 {
47 }
48
49 virtual NodeType type() const override final
50 {
51 return NodeType::CONDITION;
52 }
53};
54
55/**
56 * @brief The SimpleConditionNode provides an easy to use ConditionNode.
57 * The user should simply provide a callback with this signature
58 *
59 * BT::NodeStatus functionName(void)
60 *
61 * This avoids the hassle of inheriting from a ActionNode.
62 *
63 * Using lambdas or std::bind it is easy to pass a pointer to a method.
64 */
66{
67public:
68 using TickFunctor = std::function<NodeStatus(TreeNode&)>;
69
70 // You must provide the function to call when tick() is invoked
71 SimpleConditionNode(const std::string& name, TickFunctor tick_functor,
72 const NodeConfig& config);
73
74 ~SimpleConditionNode() override = default;
75
76 SimpleConditionNode(const SimpleConditionNode&) = delete;
77 SimpleConditionNode& operator=(const SimpleConditionNode&) = delete;
78 SimpleConditionNode(SimpleConditionNode&&) = delete;
79 SimpleConditionNode& operator=(SimpleConditionNode&&) = delete;
80
81protected:
82 virtual NodeStatus tick() override;
83
84 TickFunctor tick_functor_;
85};
86} // namespace BT
87
88#endif
The ConditionNode is a leaf node used to check a condition.
Definition: condition_node.h:32
virtual void halt() override final
Definition: condition_node.h:44
Definition: leaf_node.h:22
The SimpleConditionNode provides an easy to use ConditionNode. The user should simply provide a callb...
Definition: condition_node.h:66
virtual NodeStatus tick() override
Method to be implemented by the user.
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
void resetStatus()
Set the status to IDLE.
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:21
Definition: tree_node.h:105