+2 votes

An often recommended performance optimization is to remove get_node() calls from hot code paths and instead move them to an onready var. Does the same hold true for the $NodePath literal syntax, or is it somehow optimized away?


Basically, my question is, does

_process(delta):
    $Neck/Camera.do_stuff()

work exactly like

_process(delta):
    get_node("Neck/Camera").do_stuff()

And, if so, is it recommended to use something like

onready var Cam = $Neck/Camera

_process(delta):
    Cam.do_stuff()
in Engine by (88 points)
edited by

This issue on github seems relevant:
https://github.com/godotengine/godot-proposals/issues/996

1 Answer

+5 votes
Best answer

Yes, those two statements are equivalent.

As for your performance question, this is something that can only be answered by profiling. In practice, for most scripts, the difference is negligible.

by (20,679 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.