diff --git a/behaviour_node.h b/behaviour_node.h index cf865e4b74..715db02584 100644 --- a/behaviour_node.h +++ b/behaviour_node.h @@ -14,15 +14,17 @@ protected: void _notification(int what); public: - virtual void enter() {} virtual void execute() {} + virtual void enter() { + set_status(Fail); + } virtual void exit() {} virtual BehaviourNode *get_next() { return this; } private: class BehaviourTree *behaviour_tree{ nullptr }; BehaviourNode *parent{ nullptr }; - Status status{}; + Status status{ Fail }; bool leaf{ false }; public: diff --git a/behaviour_tree.cpp b/behaviour_tree.cpp index 2aed80a598..7268f749cf 100644 --- a/behaviour_tree.cpp +++ b/behaviour_tree.cpp @@ -1,6 +1,7 @@ #include "behaviour_tree.h" #include "behaviour_nodes/behaviour_composite.h" #include "behaviour_nodes/behaviour_node.h" +#include "behaviour_nodes/control_nodes.h" #include "core/config/engine.h" #include "core/variant/typed_array.h" @@ -21,11 +22,12 @@ void BehaviourTree::_notification(int what) { return; case NOTIFICATION_READY: for (Variant var : get_children()) { - if ((this->current = cast_to(var))) { + if ((this->root = cast_to(var))) { + this->current = this->root; break; } } - ERR_FAIL_COND_EDMSG(this->current == nullptr, "No valid BehaviourNode in BehaviourTree"); + ERR_FAIL_COND_EDMSG(this->root == nullptr, "No valid BehaviourNode in BehaviourTree"); set_process(true); return; case NOTIFICATION_PROCESS: @@ -47,6 +49,6 @@ bool BehaviourTree::execute_next() { next->enter(); } this->current = next; - return cast_to(this->current) || this->current->get_status() == BehaviourNode::Fail; + return this->current != this->root && (cast_to(this->current) || this->current->get_status() != BehaviourNode::Running); } } diff --git a/behaviour_tree.h b/behaviour_tree.h index f9593c15c4..66d39ede2e 100644 --- a/behaviour_tree.h +++ b/behaviour_tree.h @@ -15,4 +15,5 @@ public: private: class BehaviourNode *current{ nullptr }; + class BehaviourNode *root{ nullptr }; };