diff --git a/scene/main/node.cpp b/scene/main/node.cpp index ad7e445b5cb..094c3f0c941 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1167,6 +1167,20 @@ void Node::set_name(const String &p_name) { } } +// Returns a clear description of this node depending on what is available. Useful for error messages. +String Node::get_description() const { + String description; + if (is_inside_tree()) { + description = get_path(); + } else { + description = get_name(); + if (description.is_empty()) { + description = get_class(); + } + } + return description; +} + static SafeRefCount node_hrcr_count; void Node::init_node_hrcr() { @@ -1596,17 +1610,7 @@ Node *Node::get_node(const NodePath &p_path) const { Node *node = get_node_or_null(p_path); if (unlikely(!node)) { - // Try to get a clear description of this node in the error message. - String desc; - if (is_inside_tree()) { - desc = get_path(); - } else { - desc = get_name(); - if (desc.is_empty()) { - desc = get_class(); - } - } - + const String desc = get_description(); if (p_path.is_absolute()) { ERR_FAIL_V_MSG(nullptr, vformat(R"(Node not found: "%s" (absolute path attempted from "%s").)", p_path, desc)); diff --git a/scene/main/node.h b/scene/main/node.h index e848ab112af..bb29e473e06 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -383,6 +383,7 @@ public: /* NODE/TREE */ StringName get_name() const; + String get_description() const; void set_name(const String &p_name); void add_child(Node *p_child, bool p_force_readable_name = false, InternalMode p_internal = INTERNAL_MODE_DISABLED); @@ -732,12 +733,12 @@ Error Node::rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args) } #ifdef DEBUG_ENABLED -#define ERR_THREAD_GUARD ERR_FAIL_COND_MSG(!is_accessible_from_caller_thread(), "Caller thread can't call this function in this node. Use call_deferred() or call_thread_group() instead."); -#define ERR_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_accessible_from_caller_thread(), (m_ret), "Caller thread can't call this function in this node. Use call_deferred() or call_thread_group() instead.") -#define ERR_MAIN_THREAD_GUARD ERR_FAIL_COND_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), "This function in this node can only be accessed from the main thread. Use call_deferred() instead."); -#define ERR_MAIN_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), (m_ret), "This function in this node can only be accessed from the main thread. Use call_deferred() instead.") -#define ERR_READ_THREAD_GUARD ERR_FAIL_COND_MSG(!is_readable_from_caller_thread(), "This function in this node can only be accessed from either the main thread or a thread group. Use call_deferred() instead.") -#define ERR_READ_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_readable_from_caller_thread(), (m_ret), "This function in this node can only be accessed from either the main thread or a thread group. Use call_deferred() instead.") +#define ERR_THREAD_GUARD ERR_FAIL_COND_MSG(!is_accessible_from_caller_thread(), vformat("Caller thread can't call this function in this node (%s). Use call_deferred() or call_thread_group() instead.", get_description())); +#define ERR_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_accessible_from_caller_thread(), (m_ret), vformat("Caller thread can't call this function in this node (%s). Use call_deferred() or call_thread_group() instead.", get_description())); +#define ERR_MAIN_THREAD_GUARD ERR_FAIL_COND_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), vformat("This function in this node (%s) can only be accessed from the main thread. Use call_deferred() instead.", get_description())); +#define ERR_MAIN_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), (m_ret), vformat("This function in this node (%s) can only be accessed from the main thread. Use call_deferred() instead.", get_description())); +#define ERR_READ_THREAD_GUARD ERR_FAIL_COND_MSG(!is_readable_from_caller_thread(), vformat("This function in this node (%s) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.", get_description())); +#define ERR_READ_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_readable_from_caller_thread(), (m_ret), vformat("This function in this node (%s) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.", get_description())); #else #define ERR_THREAD_GUARD #define ERR_THREAD_GUARD_V(m_ret)