-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
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 Node.get_unique_node()
#64023
base: master
Are you sure you want to change the base?
Add Node.get_unique_node()
#64023
Conversation
scene/main/node.cpp
Outdated
if (!unique) { | ||
return nullptr; | ||
} | ||
return unique |
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.
This is lacking a semicolon and a dereference.
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 apologise for my ignorance, I'll update this PR soon, but what is a dereference in this case?
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.
return *unique;
You need to dereference unique because its currently becoming a pointer to a pointer of a Node and is not a pointer to a Node itself, you need to get the pointer to the Node, thus you dereference the pointer by doing *unique
, (converting it from Node**
to Node*
) which is what get_node_or_null
does.
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.
Thank you, very gentle and insightful!
scene/main/node.cpp
Outdated
if (!unique) { | ||
return nullptr; | ||
} | ||
return unique |
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.
This is lacking a semicolon and a dereference.
It's less convenient but FYI you can get compiler errors and download builds from the CI checks |
Thank you! I "fixed" it and actually attempted to compile Godot, but this results into |
scene/main/node.cpp
Outdated
|
||
if (data.owned_unique_nodes.size()) { | ||
// Has unique nodes in ownership | ||
Node **unique = data.owned_unique_nodes.getptr(p_name); |
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.
data.owned_unique_nodes.getptr(p_name)
here actually references the const function making it return a Node *const *
instead of Node **
, this needs to be fixed with Node *const *unique = data.owned_unique_nodes.getptr(p_name);
instead. (I forgot to check this specifically, sorry bout that)
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.
It's ok, how gentle! I figured just at the same time as this reply. It all compiles now.
This comment was marked as outdated.
This comment was marked as outdated.
Will implement autocompletion as well, soon. |
Explaining the concept in the docs is a bit untidy (scene /= owner). I may give a better shot at it another time. This is good for now. |
e0cbd26
to
7168d1a
Compare
I feel like the unique node symbol being stored in the HashMap should be noted in an issue, personally I would think of it as a bug (even if its otherwise fairly insignificant except in this specific case) since there is no reason to store that, should leave it to functions to handle that manually. |
The code looks reasonably self-contained. I may just open another PR at a later point in time. It can't quite be reported as an issue and tested, because to the external eye, and the editor, it doesn't matter ( It's possible this was done to speed up access using |
I guess, if it was immediately solved in a PR then bothering to put down the issue would be worthless, so long as an addressing PR isn't delayed, cause otherwise this problem would most certainly be forgotten.
Yeah, but since this PR overrides that specific case by being specific, maybe it should be addressed in this PR, I don't know. |
@Mickeon Your branch is currently 55 commits behind the godot/master, it probably has the state of the |
7168d1a
to
8d29dab
Compare
Thank you all for the support! This was my first (actual) code contribution. It seems all good now. |
a00b2f4
to
742dfd1
Compare
Updated to use |
63ce1ec
to
742dfd1
Compare
742dfd1
to
a562618
Compare
Also updates description of find_child() and find_parent() to mention get_unique_node()
a562618
to
8c466cb
Compare
ERR_FAIL_V_MSG(nullptr, "'name' parameter cannot be empty"); | ||
} | ||
|
||
const StringName key = StringName(UNIQUE_NODE_PREFIX + p_name); |
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.
The problem with this approach is that this bit is actually quite inefficient compared to just requesting "%Node", so if we added this function and some users prefer it, it would be slower.
I am not sure how it can be done better, as it is not intended to be obtained like this.
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.
This is true, yeah. In fact, please see the benchmark in #64274
The GDScript team talked about this proposal during a meeting (that is somewhat related to the following godotengine/godot-proposals#5064). We think that it would be a good idea to implement this kind of function for |
Closes godotengine/godot-proposals#5048
This PR adds a new method to Node called
get_unique_node()
. The code is almost copied and pasted from part ofget_node()
. However, unlike the latter that passes through a NodePath, this method requires a StringNamename
as argument, and accesses theowned_unique_nodes
directly.This method is complete with autocompletion:
Also updates description of
find_child()
to mentionget_unique_node()
.As a comparison, this method is much slower when the appropriate parameters are passed (NotePath VS. StringName), but it is faster when normal String parameters are passed to both, which is what most common users would write.
The reason for this massive discrepancy can be attributed to the computing time it takes to append "%":
const StringName key = StringName(UNIQUE_NODE_PREFIX + p_name);
This is a requirement because, FOR SOME REASON, the "%" is included in the key when the Node is set as an unique name. I find it very unnecessary, as there's no way anything without "%" can be inserted into the
owned_unique_nodes
HashMap anyhow, but okay. I am emphasising this, because such a simple thing was the burden of me for 8+ hours.This can probably be backported or cherrypicked to
3.x
with no issues.