Utilize BTInstance in BTState

This commit is contained in:
Serhii Snitsaruk 2024-08-03 11:39:23 +02:00
parent fc26f51ff2
commit c4c9b5fe09
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
6 changed files with 38 additions and 21 deletions

View File

@ -73,7 +73,17 @@ bool BTInstance::get_monitor_performance() const {
void BTInstance::register_with_debugger() { void BTInstance::register_with_debugger() {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
LimboDebugger::get_singleton()->register_bt_instance(get_instance_id()); if (LimboDebugger::get_singleton()->is_active()) {
LimboDebugger::get_singleton()->register_bt_instance(get_instance_id());
}
#endif
}
void BTInstance::unregister_with_debugger() {
#ifdef DEBUG_ENABLED
if (LimboDebugger::get_singleton()->is_active()) {
LimboDebugger::get_singleton()->unregister_bt_instance(get_instance_id());
}
#endif #endif
} }
@ -123,6 +133,7 @@ void BTInstance::_bind_methods() {
ClassDB::bind_method(D_METHOD("update", "delta"), &BTInstance::update); ClassDB::bind_method(D_METHOD("update", "delta"), &BTInstance::update);
ClassDB::bind_method(D_METHOD("register_with_debugger"), &BTInstance::register_with_debugger); ClassDB::bind_method(D_METHOD("register_with_debugger"), &BTInstance::register_with_debugger);
ClassDB::bind_method(D_METHOD("unregister_with_debugger"), &BTInstance::unregister_with_debugger);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitor_performance"), "set_monitor_performance", "get_monitor_performance"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitor_performance"), "set_monitor_performance", "get_monitor_performance");

View File

@ -50,6 +50,7 @@ public:
bool get_monitor_performance() const; bool get_monitor_performance() const;
void register_with_debugger(); void register_with_debugger();
void unregister_with_debugger();
static Ref<BTInstance> create(Ref<BTTask> p_root_task, String p_source_bt_path, Node *p_owner_node); static Ref<BTInstance> create(Ref<BTTask> p_root_task, String p_source_bt_path, Node *p_owner_node);

View File

@ -11,7 +11,6 @@
#include "bt_state.h" #include "bt_state.h"
#include "../editor/debugger/limbo_debugger.h"
#include "../util/limbo_compat.h" #include "../util/limbo_compat.h"
#include "../util/limbo_string_names.h" #include "../util/limbo_string_names.h"
@ -54,18 +53,17 @@ void BTState::_setup() {
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned."); ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
Node *scene_root = get_owner(); Node *scene_root = get_owner();
ERR_FAIL_NULL_MSG(scene_root, "BTState: Initialization failed - can't get scene root (make sure the BTState's owner property is set)."); ERR_FAIL_NULL_MSG(scene_root, "BTState: Initialization failed - can't get scene root (make sure the BTState's owner property is set).");
tree_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), scene_root); bt_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), scene_root);
ERR_FAIL_COND_MSG(bt_instance.is_null(), "BTState: Initialization failed - can't instantiate behavior tree.");
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { bt_instance->register_with_debugger();
LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path());
}
#endif #endif
} }
void BTState::_exit() { void BTState::_exit() {
if (tree_instance.is_valid()) { if (bt_instance.is_valid()) {
tree_instance->abort(); bt_instance->get_root_task()->abort();
} else { } else {
ERR_PRINT_ONCE("BTState: BehaviorTree is not assigned."); ERR_PRINT_ONCE("BTState: BehaviorTree is not assigned.");
} }
@ -78,8 +76,9 @@ void BTState::_update(double p_delta) {
// Bail out if a transition happened in the meantime. // Bail out if a transition happened in the meantime.
return; return;
} }
ERR_FAIL_NULL(tree_instance); ERR_FAIL_NULL(bt_instance);
int status = tree_instance->execute(p_delta); bt_instance->update(p_delta);
BTTask::Status status = bt_instance->get_last_status();
if (status == BTTask::SUCCESS) { if (status == BTTask::SUCCESS) {
get_root()->dispatch(success_event, Variant()); get_root()->dispatch(success_event, Variant());
} else if (status == BTTask::FAILURE) { } else if (status == BTTask::FAILURE) {
@ -92,15 +91,15 @@ void BTState::_notification(int p_notification) {
switch (p_notification) { switch (p_notification) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { if (bt_instance.is_valid()) {
LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path()); bt_instance->register_with_debugger();
} }
} break; } break;
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { if (bt_instance.is_valid()) {
LimboDebugger::get_singleton()->unregister_bt_instance(tree_instance, get_path()); bt_instance->unregister_with_debugger();
} }
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
@ -117,7 +116,7 @@ void BTState::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_behavior_tree", "behavior_tree"), &BTState::set_behavior_tree); ClassDB::bind_method(D_METHOD("set_behavior_tree", "behavior_tree"), &BTState::set_behavior_tree);
ClassDB::bind_method(D_METHOD("get_behavior_tree"), &BTState::get_behavior_tree); ClassDB::bind_method(D_METHOD("get_behavior_tree"), &BTState::get_behavior_tree);
ClassDB::bind_method(D_METHOD("get_tree_instance"), &BTState::get_tree_instance); ClassDB::bind_method(D_METHOD("get_bt_instance"), &BTState::get_bt_instance);
ClassDB::bind_method(D_METHOD("set_success_event", "event"), &BTState::set_success_event); ClassDB::bind_method(D_METHOD("set_success_event", "event"), &BTState::set_success_event);
ClassDB::bind_method(D_METHOD("get_success_event"), &BTState::get_success_event); ClassDB::bind_method(D_METHOD("get_success_event"), &BTState::get_success_event);

View File

@ -22,7 +22,7 @@ class BTState : public LimboState {
private: private:
Ref<BehaviorTree> behavior_tree; Ref<BehaviorTree> behavior_tree;
Ref<BTTask> tree_instance; Ref<BTInstance> bt_instance;
StringName success_event; StringName success_event;
StringName failure_event; StringName failure_event;
@ -42,7 +42,7 @@ public:
void set_behavior_tree(const Ref<BehaviorTree> &p_value); void set_behavior_tree(const Ref<BehaviorTree> &p_value);
Ref<BehaviorTree> get_behavior_tree() const { return behavior_tree; } Ref<BehaviorTree> get_behavior_tree() const { return behavior_tree; }
Ref<BTTask> get_tree_instance() const { return tree_instance; } Ref<BTInstance> get_bt_instance() const { return bt_instance; }
void set_success_event(const StringName &p_success_event) { success_event = p_success_event; } void set_success_event(const StringName &p_success_event) { success_event = p_success_event; }
StringName get_success_event() const { return success_event; } StringName get_success_event() const { return success_event; }

View File

@ -33,9 +33,6 @@
//**** LimboDebugger //**** LimboDebugger
LimboDebugger *LimboDebugger::singleton = nullptr; LimboDebugger *LimboDebugger::singleton = nullptr;
LimboDebugger *LimboDebugger::get_singleton() {
return singleton;
}
LimboDebugger::LimboDebugger() { LimboDebugger::LimboDebugger() {
singleton = this; singleton = this;
@ -93,6 +90,10 @@ bool LimboDebugger::parse_message_gdext(const String &p_msg, const Array &p_args
void LimboDebugger::register_bt_instance(uint64_t p_instance_id) { void LimboDebugger::register_bt_instance(uint64_t p_instance_id) {
ERR_FAIL_COND(p_instance_id == 0); ERR_FAIL_COND(p_instance_id == 0);
if (!IS_DEBUGGER_ACTIVE()) {
return;
}
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(p_instance_id)); BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(p_instance_id));
ERR_FAIL_NULL(inst); ERR_FAIL_NULL(inst);
ERR_FAIL_COND(!inst->is_instance_valid()); ERR_FAIL_COND(!inst->is_instance_valid());
@ -126,6 +127,10 @@ void LimboDebugger::unregister_bt_instance(uint64_t p_instance_id) {
} }
} }
bool LimboDebugger::is_active() const {
return IS_DEBUGGER_ACTIVE();
}
void LimboDebugger::_track_tree(uint64_t p_instance_id) { void LimboDebugger::_track_tree(uint64_t p_instance_id) {
ERR_FAIL_COND(!active_bt_instances.has(p_instance_id)); ERR_FAIL_COND(!active_bt_instances.has(p_instance_id));

View File

@ -39,7 +39,7 @@ private:
public: public:
static void initialize(); static void initialize();
static void deinitialize(); static void deinitialize();
static LimboDebugger *get_singleton(); _FORCE_INLINE_ static LimboDebugger *get_singleton() { return singleton; }
~LimboDebugger(); ~LimboDebugger();
@ -66,6 +66,7 @@ public:
void register_bt_instance(uint64_t p_instance_id); void register_bt_instance(uint64_t p_instance_id);
void unregister_bt_instance(uint64_t p_instance_id); void unregister_bt_instance(uint64_t p_instance_id);
bool is_active() const;
#endif // ! DEBUG_ENABLED #endif // ! DEBUG_ENABLED
}; };