onready var ... OR reference using $?

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

Which one is better, storing a node in a variable before using it or just reference it with $ when using it?

:bust_in_silhouette: Reply From: Gluon

There is no right or wrong here, rather it is down to what you are trying to do and which one makes more sense for your code and coding practices.

In many cases storing it in a variable is useful if you will be referecing the node many times in your code, its usually easier to reference a variable name and you can make that name make sense in your script when the name of the node is actually different (e.g. change “monster.tscn” to “enemy”).

That said there is nothing wrong with $nodename as a convention and I do this all the time especially when it is a one off in the script as it makes it very clear what you are referencing there without having to find out where a variable is instantiated when you come to read the code 2 years later.

So really it depends on what works best for you. Sorry, not the most helpful conclusion!

Got you. Thanks man. Appreciate your help.

Maz90 | 2023-02-09 21:23