BehaviorTree.CPP icon indicating copy to clipboard operation
BehaviorTree.CPP copied to clipboard

Parsing input ports in the constructor

Open George-Gi opened this issue 4 weeks ago • 0 comments

I have noticed in the tutorials that the input ports are parsed in the tick() method, which means they will be parsed repeatedly on every tick.

Is it safe to do the following, if we don't expect the input ports values to change?

class SaySomething : public SyncActionNode
{
public:
  // If your Node has ports, you must use this constructor signature 
  SaySomething(const std::string& name, const NodeConfig& config)
    : SyncActionNode(name, config)
  {
    Expected<std::string> msg = getInput<std::string>("message");
    // Check if expected is valid. If not, throw its error
    if (!msg)
    {
      throw BT::RuntimeError("missing required input [message]: ", 
                              msg.error() );
    }
    input = msg.value();
 }

  // It is mandatory to define this STATIC method.
  static PortsList providedPorts()
  {
    // This action has a single input port called "message"
    return { InputPort<std::string>("message") };
  }

  // Override the virtual function tick()
  NodeStatus tick() override
  {
    return NodeStatus::SUCCESS;
  }

  std::string input;
};

George-Gi avatar Nov 28 '25 12:11 George-Gi