limboai/bt/decorators/bt_subtree.cpp

59 lines
2.0 KiB
C++
Raw Normal View History

2022-09-20 17:15:48 +00:00
/* bt_subtree.cpp */
#include "bt_subtree.h"
#include "core/config/engine.h"
#include "core/error/error_macros.h"
#include "core/object/object.h"
2022-09-20 17:15:48 +00:00
#include "core/typedefs.h"
#include "core/variant/variant.h"
2022-09-20 17:15:48 +00:00
#include "modules/limboai/blackboard.h"
#include "modules/limboai/bt/actions/bt_action.h"
#include "modules/limboai/bt/actions/bt_fail.h"
#include "modules/limboai/bt/bt_task.h"
#include "modules/limboai/bt/decorators/bt_decorator.h"
2022-09-21 14:13:17 +00:00
#include "modules/limboai/bt/decorators/bt_new_scope.h"
2022-09-20 17:15:48 +00:00
String BTSubtree::_generate_name() const {
String s;
if (subtree.is_null()) {
s = "(unassigned)";
} 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);
}
2022-12-17 07:33:18 +00:00
void BTSubtree::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) {
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());
BTNewScope::initialize(p_agent, p_blackboard);
2022-09-20 17:15:48 +00:00
}
int 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);
}
String BTSubtree::get_configuration_warning() const {
String warning = BTTask::get_configuration_warning(); // BTDecorator skipped intentionally
if (!warning.is_empty()) {
2022-09-20 17:15:48 +00:00
warning += "\n";
}
if (subtree.is_null()) {
warning += "Subtree needs to be assigned.\n";
}
return warning;
}
void BTSubtree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_subtree", "p_value"), &BTSubtree::set_subtree);
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");
}