get_node() every frame?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yepbear
:warning: Old Version Published before Godot 3 was released.

I’m wondering about the efficiency of using get_node() in process() every frame. I see many code examples do this, but is it okay? I usually use get_node() once and store the node reference in a variable, but is it fine to skip that and just use get_node() all the time?

:bust_in_silhouette: Reply From: eons

I don’t know if is “faster” and most people use instance variables just for readability, but for OOP design, if it is not a class property just get it when you need it in the scope where it is used (a specific method).

Is the ideal on any form of structured programming too.

:bust_in_silhouette: Reply From: Zylann

When I know the node is always going to be here, I get_node once and use the variable afterwards, using onready if possible. Because it has a cost, very small, but existing. I also like to separate node name and variable name, so that it’s cleaner to write and changing the name of the node doesn’t become a copy/paste hell.