-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add get_descendants, get_descendant_count, get_ancestors and get_ancestor_count to Node #76674
Open
Wolfyxon
wants to merge
16
commits into
godotengine:master
Choose a base branch
from
Wolfyxon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−0
Open
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9c0cbf9
Added get_descendant_count method
Wolfyxon b55021f
Added get_descendants method that returns a single array of all node'…
Wolfyxon 5d3560c
Add documentation for get_descendants and get_descendant_count
Wolfyxon 38b3378
Used proper formatting
Wolfyxon 8553ffa
Fixed spaces being used instead of tabs for indentation
Wolfyxon 8f2d725
Fixed spaces being used instead of tabs for indentation
Wolfyxon 8d9b926
Fixed spaces being used instead of tabs for indentation
Wolfyxon 9c85fa6
Re-added missing return statement in get_descendants
Wolfyxon f90d3bb
Added line between get_descendant_count and get_descendants to improv…
Wolfyxon 4ae9091
Fixed indentation
Wolfyxon 86deb8f
Fixed invalid return type
Wolfyxon 6d1985b
Added get_ancestor_count()
Wolfyxon 8f9d9b4
Added get_ancestors()
Wolfyxon b15b6ca
Reordered previously added funcitons
Wolfyxon bc13906
Added documentation for get_ancestors() and get_ancestor_count()
Wolfyxon 6521b67
Reordered get_ancestor_count and get_ancestors in docs with --doctool
Wolfyxon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1289,6 +1289,29 @@ Node *Node::_get_child_by_name(const StringName &p_name) const { | |
} | ||
} | ||
|
||
int Node::get_descendant_count(bool p_include_internal) const { | ||
int count = 0; | ||
TypedArray<Node> children = get_children(p_include_internal); | ||
count += children.size(); | ||
for (int i = 0; i < children.size(); i++) { | ||
Node *node = Object::cast_to<Node>(children[i]); | ||
if (node->get_child_count(p_include_internal) > 0) { | ||
count += node->get_descendant_count(p_include_internal); | ||
} | ||
} | ||
return count; | ||
} | ||
TypedArray<Node> Node::get_descendants(bool p_include_internal) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For style there should be a line between these two functions, to make it easier to navigate and read |
||
TypedArray<Node> res = get_children(p_include_internal); | ||
TypedArray<Node> children = get_children(p_include_internal); | ||
for (int i = 0; i < children.size(); i++) { | ||
Node *node = Object::cast_to<Node>(children[i]); | ||
if (node->get_child_count(p_include_internal) > 0) { | ||
res.append_array(node->get_descendants(p_include_internal)); | ||
} | ||
} | ||
} | ||
|
||
Node *Node::get_node_or_null(const NodePath &p_path) const { | ||
if (p_path.is_empty()) { | ||
return nullptr; | ||
|
@@ -2857,6 +2880,8 @@ void Node::_bind_methods() { | |
ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript. | ||
ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::get_children, DEFVAL(false)); | ||
ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false)); | ||
ClassDB::bind_method(D_METHOD("get_descendants", "include_internal"), &Node::get_descendants, DEFVAL(false)); | ||
ClassDB::bind_method(D_METHOD("get_descendant_count", "include_internal"), &Node::get_descendant_count, DEFVAL(false)); | ||
ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node); | ||
ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node); | ||
ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure using the "get_children" function is the best way to achieve this, none of the other functions use this and instead access the children directly, see the function itself for that, not sure what the performance difference would be but that would be my suggestion