-
-
Notifications
You must be signed in to change notification settings - Fork 97
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 shorthand expr.$NodePath
for expr.get_node("NodePath")
#1776
Comments
Not sure if that's the best possible syntax, but I agree with this being a problem. |
If we were to allow this, I think |
This is a matter of using Replace All, so it's not really a big problem. |
Yes, you're right, this is just syntactic sugar. But this is a logical continuation of the |
You can do
|
@Tyrannosaurus1234 You may not have known this, but it is not necessary, it even works without the quotes. var x = "path/to/node"
# In 3.2 these options are equivalent:
$path/to/node
$"path/to/node"
get_node(x)
# In an alternate reality:
$"path/to/node"
$x
$(x)
$("path/to/" + "node") Basically, I like the option with parentheses because it doesn't break anything. var grid_path = "Scroll/Grid/"
onready var item1 = $(grid_path + "Item1") But my option is still more concise: onready var grid = $Scroll/Grid
onready var item1 = grid$Item1 |
For reference, quotes with $"../Player"
$"/root/Game/Level" |
Or when the node name contains spaces or special characters (i.e. when it's not a valid identifier). That said, I would prefer Calinou's suggestion of using |
I'm satisfied with both options. If you think the second is better, I'll agree with you. |
Is this a frequent use case? Personally I've made the experience that most of the time, I'm requesting a node relative to the current one. |
Quite often in UI scenes, because many intermediate nodes are used, especially containers. In other types of scenes, too, there are sometimes many levels of nesting. I don't use 3D, but I saw from the screenshots how many nodes people have. Plus:
|
is this really purely syntactic sugar? I grabbed
feel free to test yourself: project4.zip as you can see, the so my conclusion is unless node paths are variable,
|
Yes, I read somewhere that the difference between Try also comparing with the Yes, it's not exactly syntactic sugar, but it's more shorthand and syntactic sugar than an optimization tool. |
didn't knew about the ^ thing! still very much in favor of this proposal though. |
two things here:
|
The more I’m using Godot, the more I’m relying on nodes, get_node and on the $ shortcut. I’ve tried to use I think this proposal is a logical generalization of the current $ shortcut. And I think the $ shortcut is useful enough for this generalization to be important. |
meanwhile scene unique nodes where introduced with their own shorthand, so one would now expect |
expr$NodePath
for expr.get_node("NodePath")
expr.$NodePath
for expr.get_node("NodePath")
This is useful syntax clarity for using heavy composition with nodes. I have a damage system that all creatures in my game have, this is represented by a node with the same script inside each creature scene. Using |
I would also like to add a use case that I'm currently working with. I have a bunch of code in various places that is doing more or less the same bunch of function calls on the children of different child nodes. For example:
(there are a bunch more lines similar in structure to the above that I removed for brevity) I'm looking to refactor my code to implement the common behavior in a helper function, in the spirit of DRY. Something like the following:
I can then call it with |
Describe the project you are working on:
A game
Describe the problem or limitation you are having in your project:
When I want to create
onready
variables for nested nodes, I have to repeat the path prefix:Obviously, this is not good. For example, the path to the
Grid
node may change and must be changed in all variables.I can replace this with
But I would like to use a more compact syntax.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
Add shorthand
expr$NodePath
forexpr.get_node("NodePath")
. Then we can do this:or:
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
I haven't looked at how this is implemented, so I don't know.
If this enhancement will not be used often, can it be worked around with a few lines of script?:
It is possible to attach the script directly to the
Grid
node, but this option is not always acceptable.Is there a reason why this should be core and not an add-on in the asset library?:
This cannot be done with an add-on, because it affects the GDScript syntax.
The text was updated successfully, but these errors were encountered: