From c4c9b5fe091478ac104ba6c4ec7bb75cdf38631a Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sat, 3 Aug 2024 11:39:23 +0200 Subject: [PATCH] Utilize BTInstance in BTState --- bt/bt_instance.cpp | 13 ++++++++++++- bt/bt_instance.h | 1 + bt/bt_state.cpp | 27 +++++++++++++-------------- bt/bt_state.h | 4 ++-- editor/debugger/limbo_debugger.cpp | 11 ++++++++--- editor/debugger/limbo_debugger.h | 3 ++- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/bt/bt_instance.cpp b/bt/bt_instance.cpp index 2cfb9b0..74997fe 100644 --- a/bt/bt_instance.cpp +++ b/bt/bt_instance.cpp @@ -73,7 +73,17 @@ bool BTInstance::get_monitor_performance() const { void BTInstance::register_with_debugger() { #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 } @@ -123,6 +133,7 @@ void BTInstance::_bind_methods() { 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("unregister_with_debugger"), &BTInstance::unregister_with_debugger); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitor_performance"), "set_monitor_performance", "get_monitor_performance"); diff --git a/bt/bt_instance.h b/bt/bt_instance.h index b037fdb..dbace83 100644 --- a/bt/bt_instance.h +++ b/bt/bt_instance.h @@ -50,6 +50,7 @@ public: bool get_monitor_performance() const; void register_with_debugger(); + void unregister_with_debugger(); static Ref create(Ref p_root_task, String p_source_bt_path, Node *p_owner_node); diff --git a/bt/bt_state.cpp b/bt/bt_state.cpp index dee8e34..cd44d21 100644 --- a/bt/bt_state.cpp +++ b/bt/bt_state.cpp @@ -11,7 +11,6 @@ #include "bt_state.h" -#include "../editor/debugger/limbo_debugger.h" #include "../util/limbo_compat.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."); 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)."); - 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 - if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { - LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path()); - } + bt_instance->register_with_debugger(); #endif } void BTState::_exit() { - if (tree_instance.is_valid()) { - tree_instance->abort(); + if (bt_instance.is_valid()) { + bt_instance->get_root_task()->abort(); } else { 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. return; } - ERR_FAIL_NULL(tree_instance); - int status = tree_instance->execute(p_delta); + ERR_FAIL_NULL(bt_instance); + bt_instance->update(p_delta); + BTTask::Status status = bt_instance->get_last_status(); if (status == BTTask::SUCCESS) { get_root()->dispatch(success_event, Variant()); } else if (status == BTTask::FAILURE) { @@ -92,15 +91,15 @@ void BTState::_notification(int p_notification) { switch (p_notification) { #ifdef DEBUG_ENABLED case NOTIFICATION_ENTER_TREE: { - if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { - LimboDebugger::get_singleton()->register_bt_instance(tree_instance, get_path()); + if (bt_instance.is_valid()) { + bt_instance->register_with_debugger(); } } break; #endif // DEBUG_ENABLED case NOTIFICATION_EXIT_TREE: { #ifdef DEBUG_ENABLED - if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { - LimboDebugger::get_singleton()->unregister_bt_instance(tree_instance, get_path()); + if (bt_instance.is_valid()) { + bt_instance->unregister_with_debugger(); } #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("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("get_success_event"), &BTState::get_success_event); diff --git a/bt/bt_state.h b/bt/bt_state.h index 717b975..94b1c4f 100644 --- a/bt/bt_state.h +++ b/bt/bt_state.h @@ -22,7 +22,7 @@ class BTState : public LimboState { private: Ref behavior_tree; - Ref tree_instance; + Ref bt_instance; StringName success_event; StringName failure_event; @@ -42,7 +42,7 @@ public: void set_behavior_tree(const Ref &p_value); Ref get_behavior_tree() const { return behavior_tree; } - Ref get_tree_instance() const { return tree_instance; } + Ref get_bt_instance() const { return bt_instance; } void set_success_event(const StringName &p_success_event) { success_event = p_success_event; } StringName get_success_event() const { return success_event; } diff --git a/editor/debugger/limbo_debugger.cpp b/editor/debugger/limbo_debugger.cpp index 89294e2..4f3fc11 100644 --- a/editor/debugger/limbo_debugger.cpp +++ b/editor/debugger/limbo_debugger.cpp @@ -33,9 +33,6 @@ //**** LimboDebugger LimboDebugger *LimboDebugger::singleton = nullptr; -LimboDebugger *LimboDebugger::get_singleton() { - return singleton; -} LimboDebugger::LimboDebugger() { 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) { ERR_FAIL_COND(p_instance_id == 0); + if (!IS_DEBUGGER_ACTIVE()) { + return; + } + BTInstance *inst = Object::cast_to(OBJECT_DB_GET_INSTANCE(p_instance_id)); ERR_FAIL_NULL(inst); 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) { ERR_FAIL_COND(!active_bt_instances.has(p_instance_id)); diff --git a/editor/debugger/limbo_debugger.h b/editor/debugger/limbo_debugger.h index 05becbe..9b8e446 100644 --- a/editor/debugger/limbo_debugger.h +++ b/editor/debugger/limbo_debugger.h @@ -39,7 +39,7 @@ private: public: static void initialize(); static void deinitialize(); - static LimboDebugger *get_singleton(); + _FORCE_INLINE_ static LimboDebugger *get_singleton() { return singleton; } ~LimboDebugger(); @@ -66,6 +66,7 @@ public: void register_bt_instance(uint64_t p_instance_id); void unregister_bt_instance(uint64_t p_instance_id); + bool is_active() const; #endif // ! DEBUG_ENABLED };