+9 votes

Is it feasible to use get_node() in the _process() or _fixed_process() callbacks in lots of different scripts? Like if I have 30 scripts which are all calling get_node() in _process(), am I likely to see any performance issues? Should I be storing a reference to the node that I get during the _ready() callback, and using that reference in _process()?

in Engine by (44 points)

I don't know, so I can't answer to your question. But I took the habit to store a reference of nodes I use frequently, typically like:

onready var somenode=get_node("somenode")

Then I only use my reference. So far so good.
And this way, I can change the node name without changing everywhere my code.

And this way, I can change the node name without changing everywhere my code.

That's a good enough reason on it's own to do it. Also, I forgot about the onready keyword.

I usually do it as Gokudomatic2. If you don't know the performance impact, you can always test it. The 2.1 has a new shiny profiler.

That's true, I could do it in the profiler. TBH I haven't actually had any problems with performance. I posted the question for posterity as much as anything, so these kinds of best practices are great to put here ;)

FYI, get_node() looks for all children and tests their name one by one, it doesn't uses a dictionary or a lookup thing. So the more nodes you have, the slower it will be.
Have a look here: https://github.com/godotengine/godot/blob/master/scene/main/node.cpp#L1589

@Gokudomatic2, you should really add this as an answer.

I think it is faster by storing the node in a variable. I tested it with two function that simply access to a node and change a parameter (the text in a label node). One function use get_node, the other just used a variable. The average times are:

  • Function with get_node: 0.000044 seconds = 44 microseconds
  • Function without get_node: 0.000005 seconds = 5 microseconds

So by not using get_node we would get 5/44 = 8.8x speedup.

Probably this effect is negligible except in very specific cases, and maybe what @Gokudomatic2 said is more relevant, as other users noted:

And this way, I can change the node name without changing everywhere my code.

Please log in or register to answer this question.

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.