Using % symbol instead of get_node (GDSCRIPT)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By munja95

Hi guys,

I can’t find the answer to my question anywhere else, so I must ask here.
I am using godot for several weeks and I discover that I can use %{node_name} instead of get_node(node_name).
On a few tutorials, I saw that some tutors use %{node_name} everywhere, but some tutors don’t, they cached value in a variable.
So my question is which is better for performance? Does %{node_name} always translate to get_node(node_name) or is there some cache mechanism like a map where the first time %{node_name} is called it will be cached in the map so the next time it is called node will get from the map?

I don’t think there’s any caching. I vagely remember reading something somewhere that says the optimum thing to do is store $Node in a var, as calling $Node or get_node(“mynode”) involves a lookup each time.

SteveSmith | 2022-12-12 21:01

:bust_in_silhouette: Reply From: jgodfrey

I’m not aware that there’s any special caching with the new Scene unique node name feature. Really, the advantage to the new syntax is that it’s able to find the referenced node anywhere in the scene, regardless of where it lives. So, rather than using the (potentially) brittle get_node("<node>") or $<node> syntax, you can now use get_node("%<node>") or $"%<node>", which won’t fail if the node is later moved to a different location in the scene tree.

With that in mind, I’d say it’s still wise to cache the reference in a variable for reuse - no different than the prevailing advise when using the original syntax.