2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_subtree.cpp
|
|
|
|
* =============================================================================
|
2024-03-04 20:36:16 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-09-20 17:15:48 +00:00
|
|
|
|
|
|
|
#include "bt_subtree.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-03-26 17:19:31 +00:00
|
|
|
void BTSubtree::set_subtree(const Ref<BehaviorTree> &p_subtree) {
|
|
|
|
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();
|
2023-08-15 15:05:30 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2024-03-26 17:19:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTSubtree::_generate_name() {
|
2022-09-20 17:15:48 +00:00
|
|
|
String s;
|
|
|
|
if (subtree.is_null()) {
|
|
|
|
s = "(unassigned)";
|
2022-12-15 07:26:52 +00:00
|
|
|
} else if (subtree->get_path().is_empty()) {
|
2022-09-20 17:15:48 +00:00
|
|
|
s = "(unsaved)";
|
|
|
|
} else {
|
|
|
|
s = vformat("\"%s\"", subtree->get_path());
|
|
|
|
}
|
|
|
|
return vformat("Subtree %s", s);
|
|
|
|
}
|
|
|
|
|
2024-05-01 21:20:17 +00:00
|
|
|
void BTSubtree::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard, Node *p_scene_root) {
|
2022-09-21 14:13:17 +00:00
|
|
|
ERR_FAIL_COND_MSG(!subtree.is_valid(), "Subtree is not assigned.");
|
|
|
|
ERR_FAIL_COND_MSG(!subtree->get_root_task().is_valid(), "Subtree root task is not valid.");
|
|
|
|
ERR_FAIL_COND_MSG(get_child_count() != 0, "Subtree task shouldn't have children during initialization.");
|
2022-09-20 17:15:48 +00:00
|
|
|
|
2022-09-21 14:13:17 +00:00
|
|
|
add_child(subtree->get_root_task()->clone());
|
|
|
|
|
2024-05-01 21:20:17 +00:00
|
|
|
BTNewScope::initialize(p_agent, p_blackboard, p_scene_root);
|
2022-09-20 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTSubtree::_tick(double p_delta) {
|
2022-09-20 17:15:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator doesn't have a child.");
|
|
|
|
return get_child(0)->execute(p_delta);
|
|
|
|
}
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
PackedStringArray BTSubtree::get_configuration_warnings() {
|
2023-08-15 15:05:30 +00:00
|
|
|
PackedStringArray warnings = BTTask::get_configuration_warnings(); // ! BTDecorator skipped intentionally
|
2022-09-20 17:15:48 +00:00
|
|
|
if (subtree.is_null()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Subtree needs to be assigned.");
|
2022-09-20 17:15:48 +00:00
|
|
|
}
|
2023-08-15 15:05:30 +00:00
|
|
|
return warnings;
|
2022-09-20 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTSubtree::_bind_methods() {
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_subtree", "behavior_tree"), &BTSubtree::set_subtree);
|
2022-09-20 17:15:48 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_subtree"), &BTSubtree::get_subtree);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree");
|
2023-08-15 15:05:30 +00:00
|
|
|
}
|
2024-03-26 17:19:31 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|