From bd575c5fe56c7c03b727d3f0e322312bb80ed0ea Mon Sep 17 00:00:00 2001 From: vPumpking Date: Sun, 21 May 2023 18:04:03 +0200 Subject: [PATCH] Add 'get_tree_string()' and 'get_tree_string_pretty()' to Node class to complement printing methods --- doc/classes/Node.xml | 30 ++++++++++++++++++++++++++++++ scene/main/node.cpp | 42 +++++++++++++++++++++++++++--------------- scene/main/node.h | 6 ++++-- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 7510d6bb640..dda550bcc4a 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -448,6 +448,36 @@ Returns the [SceneTree] that contains this node. + + + + Returns the tree as a [String]. Used mainly for debugging purposes. This version displays the path relative to the current node, and is good for copy/pasting into the [method get_node] function. It also can be used in game UI/UX. + [b]Example output:[/b] + [codeblock] + TheGame + TheGame/Menu + TheGame/Menu/Label + TheGame/Menu/Camera2D + TheGame/SplashScreen + TheGame/SplashScreen/Camera2D + [/codeblock] + + + + + + Similar to [method get_tree_string], this returns the tree as a [String]. This version displays a more graphical representation similar to what is displayed in the Scene Dock. It is useful for inspecting larger trees. + [b]Example output:[/b] + [codeblock] + ┖╴TheGame + ┠╴Menu + ┃ ┠╴Label + ┃ ┖╴Camera2D + ┖╴SplashScreen + ┖╴Camera2D + [/codeblock] + + diff --git a/scene/main/node.cpp b/scene/main/node.cpp index d8a50c4313a..4ba1c1be6f7 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -2157,30 +2157,40 @@ int Node::get_persistent_group_count() const { return count; } -void Node::_print_tree_pretty(const String &prefix, const bool last) { - String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴"); - print_line(prefix + new_prefix + String(get_name())); - _update_children_cache(); - for (uint32_t i = 0; i < data.children_cache.size(); i++) { - new_prefix = last ? String::utf8(" ") : String::utf8(" ┃ "); - data.children_cache[i]->_print_tree_pretty(prefix + new_prefix, i == data.children_cache.size() - 1); - } -} - void Node::print_tree_pretty() { - _print_tree_pretty("", true); + print_line(_get_tree_string_pretty("", true)); } void Node::print_tree() { - _print_tree(this); + print_line(_get_tree_string(this)); } -void Node::_print_tree(const Node *p_node) { - print_line(String(p_node->get_path_to(this))); +String Node::_get_tree_string_pretty(const String &p_prefix, bool p_last) { + String new_prefix = p_last ? String::utf8(" ┖╴") : String::utf8(" ┠╴"); _update_children_cache(); + String return_tree = p_prefix + new_prefix + String(get_name()) + "\n"; for (uint32_t i = 0; i < data.children_cache.size(); i++) { - data.children_cache[i]->_print_tree(p_node); + new_prefix = p_last ? String::utf8(" ") : String::utf8(" ┃ "); + return_tree += data.children_cache[i]->_get_tree_string_pretty(p_prefix + new_prefix, i == data.children_cache.size() - 1); } + return return_tree; +} + +String Node::get_tree_string_pretty() { + return _get_tree_string_pretty("", true); +} + +String Node::_get_tree_string(const Node *p_node) { + _update_children_cache(); + String return_tree = String(p_node->get_path_to(this)) + "\n"; + for (uint32_t i = 0; i < data.children_cache.size(); i++) { + return_tree += data.children_cache[i]->_get_tree_string(p_node); + } + return return_tree; +} + +String Node::get_tree_string() { + return _get_tree_string(this); } void Node::_propagate_reverse_notification(int p_notification) { @@ -3279,6 +3289,8 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false)); ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree); ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty); + ClassDB::bind_method(D_METHOD("get_tree_string"), &Node::get_tree_string); + ClassDB::bind_method(D_METHOD("get_tree_string_pretty"), &Node::get_tree_string_pretty); ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path); ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path); ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification); diff --git a/scene/main/node.h b/scene/main/node.h index feda200b24b..2a45b701390 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -217,8 +217,8 @@ private: Ref multiplayer; - void _print_tree_pretty(const String &prefix, const bool last); - void _print_tree(const Node *p_node); + String _get_tree_string_pretty(const String &p_prefix, bool p_last); + String _get_tree_string(const Node *p_node); Node *_get_child_by_name(const StringName &p_name) const; @@ -476,6 +476,8 @@ public: void print_tree(); void print_tree_pretty(); + String get_tree_string(); + String get_tree_string_pretty(); void set_scene_file_path(const String &p_scene_file_path); String get_scene_file_path() const;