-
Notifications
You must be signed in to change notification settings - Fork 714
Cannot register node that takes interface as extra parameter #945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Perhaps you need to make your class PathFollow default_constructable. |
But then I would have to pass the argument in an init function using a visitor as explained in the tutorial, but I would rather prefer to have everything on a single step. Also avoid pointers if possible, |
If instead of using references, std::shared_ptr are used, then the code compiles correctly #include <behaviortree_cpp/bt_factory.h>
#include <iostream>
#include <memory>
// interface
struct IMotor {
virtual void doMove() = 0;
};
// implementation
struct LinearMotor : public IMotor {
void doMove() override { std::cout << ">> doMove\n"; }
};
// node using interface
class PathFollow : public BT::StatefulActionNode {
public:
PathFollow(const std::string &name, const BT::NodeConfig &config,
std::shared_ptr<IMotor> motor)
: BT::StatefulActionNode(name, config), imotor_(motor) {}
static BT::PortsList providedPorts() { return {}; }
BT::NodeStatus onStart() override {
std::cout << "onStart\n";
imotor_->doMove();
return BT::NodeStatus::RUNNING;
}
BT::NodeStatus onRunning() override {
std::cout << "onRunning\n";
imotor_->doMove();
return BT::NodeStatus::SUCCESS;
}
void onHalted() override {}
private:
std::shared_ptr<IMotor> imotor_;
};
int main() {
auto motor = std::make_shared<LinearMotor>();
BT::BehaviorTreeFactory factory;
factory.registerNodeType<PathFollow>("PathFollow", motor);
auto tree =
factory.createTreeFromText((" <root BTCPP_format=\"4\">"
" <BehaviorTree ID=\"MainTree\">"
" <Sequence name=\"root_sequence\">"
" <PathFollow name=\"path_follow\"/>"
" </Sequence>"
" </BehaviorTree>"
"</root>"));
tree.tickWhileRunning();
} |
This compiles: LinearMotor motor;
BT::BehaviorTreeFactory factory;
factory.registerNodeType<PathFollow>("PathFollow", std::ref(motor)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
Using release 4.6.2.
I was trying to register a node that depends on an interface in its constructor:
Trying different ways to register the node, none of them compile. Is there a way to use an interface as an extra argument? Already checked the tutorial 8 and normal examples with values compile without problems.
Here is the compilation output:
This node should be "param_constructable" with the LinearMotor struct passed to it. Any guess on how to do it? Am I missing something basic?
The text was updated successfully, but these errors were encountered: