BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
script_node.h
1/* Copyright (C) 2022-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/action_node.h"
17#include "behaviortree_cpp/scripting/script_parser.hpp"
18
19namespace BT
20{
21/**
22 * @brief The ScriptNode executes a piece of script code to set or modify
23 * entries in the Blackboard.
24 *
25 * The script is passed via the input port "code" and can use the BT++ scripting
26 * language syntax. For instance:
27 *
28 * <Script code=" msg:='hello world' " />
29 * <Script code=" A:=42; B:=3.14 " />
30 *
31 * The node always returns SUCCESS after executing the script.
32 */
33class ScriptNode : public SyncActionNode
34{
35public:
36 ScriptNode(const std::string& name, const NodeConfig& config)
37 : SyncActionNode(name, config)
38 {
39 setRegistrationID("ScriptNode");
40
41 loadExecutor();
42 }
43
44 static PortsList providedPorts()
45 {
46 return { InputPort<std::string>("code", "Piece of code that can be parsed") };
47 }
48
49private:
50 virtual BT::NodeStatus tick() override
51 {
52 loadExecutor();
53 if(_executor)
54 {
55 Ast::Environment env = { config().blackboard, config().enums };
56 _executor(env);
57 }
58 return NodeStatus::SUCCESS;
59 }
60
61 void loadExecutor()
62 {
63 std::string script;
64 if(!getInput("code", script))
65 {
66 throw RuntimeError("Missing port [code] in Script");
67 }
68 if(script == _script)
69 {
70 return;
71 }
72 auto executor = ParseScript(script);
73 if(!executor)
74 {
75 throw RuntimeError(executor.error());
76 }
77 else
78 {
79 _executor = executor.value();
80 _script = script;
81 }
82 }
83
84 std::string _script;
85 ScriptFunction _executor;
86};
87
88} // namespace BT
The ScriptNode executes a piece of script code to set or modify entries in the Blackboard.
Definition: script_node.h:34
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
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