Godot: Shorthand for get_node()

Created on 13 Apr 2016  Â·  19Comments  Â·  Source: godotengine/godot

get_node() is a very frequently used function. An alias like $() or @() could help increase productivity, as something that's built into Godot (just like $() is built into jQuery).

feature proposal gdscript

Most helpful comment

A shorthand for get_node() was added to the master branch, it looks like this:

$MyLabel.text = "Hello world!"
# or…
$MyLabel.set_text("Hello world!")

All 19 comments

Its not so much an issue. I suppose you can do something like a decorator function like so if you dont want to type get_node and then just import it at the global scope

func gn(node_path):
    get_node(node_path)

@randyyaj it's possible to attach custom functions to global scope? Any ideas on how to do this??

Any way of removing reactions? I was just experimenting :-|

Any way of removing reactions? I was just experimenting :-|

Just click the reaction icon/text.

Nope I lied. There's no global scoping :( http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html?highlight=singleton
Thought that was it for sure.

You can however declare it in a gdscript and have all your scripts extend it but that wouldn't be ideal.

I thought about doing something like $Node or $Path/to/Node I wonder if
that could be useful
On Apr 14, 2016 11:01 AM, "Randy Yang" [email protected] wrote:

Nope I lied. There's no global scoping :(
http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html?highlight=singleton
Thought that was it for sure.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/godotengine/godot/issues/4309#issuecomment-210078518

I proposd this a while ago though and no one liked it, so will try again for 3.0

Not sure if 6 thumbs up means no one liked it...

Personally I'm not really sure this would be a useful addition, get_node() is easy to understand and arguably not that long. But I wouldn't mind a shortcut to get added, as long as it looks GDScript-like.

With a shortcut like $Node I wonder how it would handle node paths with spaces, like $Path/To/Super Node. It should then probably become $"Path/To/Super Node", and already the interest of the feature seems diminished IMO..

On the other hand the feature wouldn't hurt much either, and I don't have to use it if I don't like it, so feel free to implement something :D

I was going to open up a feature request for this very same issue, fortunately I ran into this accidentally via google...

I am definitely in favor of adding a shorter alias for get_node(). It constantly pops up in my mind when writing & reading GDScript that this could & should be simpler.

What I was going to suggest, was to simply use array/dictionary access, e.g:

var anim = foo["/path/to/my/anim"]
var content = self["/content"]
# A singleton
Game["/gui"].hide()

I used a very similar syntax in another language (Kotlin) in which you can override operators yourself - in an Entity-Component System to access the Components of an Entity, and the Systems of an Engine via a class name - and it was very intuitive.

A prefix like /, @, $ seems to be mandatory with GDScript, as the variables of objects can also be accessed via the []-operator. I'm not super-familiar with GDScript, so I might be missing something obvious here though!

Any thoughts?

Why not "with", VB-style, as proposed in #6300 and #6295?

@Sslaxx

I must admit I'm a bit confused.. I mean I'm quite familiar with the with-statement (well, not from VB, but Kotlin), but how is it related to this issue? when operates on a variable, and is used to eliminate the "superfluous" references.

e.g. something like:

var sprite = get_node("sprite")
with(sprite)
    set_pos(Vector2(50, 50))
    set_rot(20)
    for i in range(0, 4):
        set_something(i)

instead of

var sprite = get_node("sprite")
sprite.set_pos(Vector2(50, 50)
sprite.set_rot(20)
...
..

It's not that I always want to use with / multiple statements when I grab a node from a node.

Not that I'm particularly against the with-statement, or anything.

Using "with" is simpler and works without adding a new coding style. This will be less confusing for gdscript beginners IMO.

A shorthand for get_node() was added to the master branch, it looks like this:

$MyLabel.text = "Hello world!"
# or…
$MyLabel.set_text("Hello world!")

Well, now this feature needs documentation

From my understanding you can access the root by using get_tree().root or get_node("/root"). I expected $/root to work that gives an error. Is this expected, and if not, will the shortcut be usable for the root in the future? I'm really enjoying the shortcut, and I think the tutorials should be updated to use the function so that new users are shown how handy the shorthand can be.

Use $"/root". Basically if $x doesn't work, try $"x" so that it parses symbols and whitespaces correctly.

Thank you very much for implementing this functionality. It is very useful.

@Calinou is there a way to make it work for nodepaths?

If you have something like:
export (NodePath) var monkey

It would be good to be able to do $(monkey).dance() instead of get_node(monkey).dance() or even just monkey.dance()?

Was this page helpful?
0 / 5 - 0 ratings