BTSubtree: Fix subtree blackboard plan is not property utilized

This commit is contained in:
Serhii Snitsaruk 2024-03-26 18:19:31 +01:00
parent 31db4b4543
commit e50b00c70e
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
4 changed files with 43 additions and 3 deletions

View File

@ -11,6 +11,15 @@
#include "bt_new_scope.h" #include "bt_new_scope.h"
void BTNewScope::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
blackboard_plan = p_plan;
if (blackboard_plan.is_null()) {
blackboard_plan.instantiate();
}
_update_blackboard_plan();
emit_changed();
}
void BTNewScope::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) { void BTNewScope::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) {
ERR_FAIL_COND(p_agent == nullptr); ERR_FAIL_COND(p_agent == nullptr);
ERR_FAIL_COND(p_blackboard == nullptr); ERR_FAIL_COND(p_blackboard == nullptr);

View File

@ -26,7 +26,9 @@ private:
protected: protected:
static void _bind_methods(); static void _bind_methods();
void set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) { blackboard_plan = p_plan; } virtual void _update_blackboard_plan() {}
void set_blackboard_plan(const Ref<BlackboardPlan> &p_plan);
Ref<BlackboardPlan> get_blackboard_plan() const { return blackboard_plan; } Ref<BlackboardPlan> get_blackboard_plan() const { return blackboard_plan; }
virtual Status _tick(double p_delta) override; virtual Status _tick(double p_delta) override;

View File

@ -11,11 +11,27 @@
#include "bt_subtree.h" #include "bt_subtree.h"
void BTSubtree::set_subtree(const Ref<BehaviorTree> &p_value) { void BTSubtree::set_subtree(const Ref<BehaviorTree> &p_subtree) {
subtree = p_value; if (Engine::get_singleton()->is_editor_hint()) {
if (subtree.is_valid() && subtree->is_connected(LW_NAME(changed), callable_mp(this, &BTSubtree::_update_blackboard_plan))) {
subtree->disconnect(LW_NAME(changed), callable_mp(this, &BTSubtree::_update_blackboard_plan));
}
if (p_subtree.is_valid()) {
p_subtree->connect(LW_NAME(changed), callable_mp(this, &BTSubtree::_update_blackboard_plan));
}
}
subtree = p_subtree;
_update_blackboard_plan();
emit_changed(); emit_changed();
} }
void BTSubtree::_update_blackboard_plan() {
if (get_blackboard_plan().is_null()) {
set_blackboard_plan(Ref<BlackboardPlan>(memnew(BlackboardPlan)));
}
get_blackboard_plan()->set_base_plan(subtree.is_valid() ? subtree->get_blackboard_plan() : nullptr);
}
String BTSubtree::_generate_name() { String BTSubtree::_generate_name() {
String s; String s;
if (subtree.is_null()) { if (subtree.is_null()) {
@ -57,3 +73,11 @@ void BTSubtree::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree");
} }
BTSubtree::~BTSubtree() {
if (Engine::get_singleton()->is_editor_hint()) {
if (subtree.is_valid() && subtree->is_connected(LW_NAME(changed), callable_mp(this, &BTSubtree::_update_blackboard_plan))) {
subtree->disconnect(LW_NAME(changed), callable_mp(this, &BTSubtree::_update_blackboard_plan));
}
}
}

View File

@ -26,6 +26,8 @@ private:
protected: protected:
static void _bind_methods(); static void _bind_methods();
virtual void _update_blackboard_plan() override;
virtual String _generate_name() override; virtual String _generate_name() override;
virtual Status _tick(double p_delta) override; virtual Status _tick(double p_delta) override;
@ -35,6 +37,9 @@ public:
virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) override; virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) override;
virtual PackedStringArray get_configuration_warnings() override; virtual PackedStringArray get_configuration_warnings() override;
BTSubtree() = default;
~BTSubtree();
}; };
#endif // BT_SUBTREE_H #endif // BT_SUBTREE_H