BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
script_condition.h
1/* Copyright (C) 2023-2025 Davide Faconti - All Rights Reserved
2 *
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/condition_node.h"
17#include "behaviortree_cpp/scripting/script_parser.hpp"
18
19namespace BT
20{
21/**
22 * @brief Execute a script, and if the result is true, return
23 * SUCCESS, FAILURE otherwise.
24 */
25class ScriptCondition : public ConditionNode
26{
27public:
28 ScriptCondition(const std::string& name, const NodeConfig& config)
29 : ConditionNode(name, config)
30 {
31 setRegistrationID("ScriptCondition");
32 loadExecutor();
33 }
34
35 static PortsList providedPorts()
36 {
37 return { InputPort("code", "Piece of code that can be parsed. Must return false or "
38 "true") };
39 }
40
41private:
42 virtual BT::NodeStatus tick() override
43 {
44 loadExecutor();
45
46 Ast::Environment env = { config().blackboard, config().enums };
47 auto result = _executor(env);
48 return (result.cast<bool>()) ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
49 }
50
51 void loadExecutor()
52 {
53 std::string script;
54 if(!getInput("code", script))
55 {
56 throw RuntimeError("Missing port [code] in ScriptCondition");
57 }
58 if(script == _script)
59 {
60 return;
61 }
62 auto executor = ParseScript(script);
63 if(!executor)
64 {
65 throw RuntimeError(executor.error());
66 }
67 else
68 {
69 _executor = executor.value();
70 _script = script;
71 }
72 }
73
74 std::string _script;
75 ScriptFunction _executor;
76};
77
78} // namespace BT
The ConditionNode is a leaf node used to check a condition.
Definition: condition_node.h:32
Execute a script, and if the result is true, return SUCCESS, FAILURE otherwise.
Definition: script_condition.h:26
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
The Environment class is used to encapsulate the information and states needed by the scripting langu...
Definition: script_parser.hpp:32
Definition: tree_node.h:105