This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

1: Should i put variables in onready var = ... Or inside _ready function?
2: Should i reference a node in a vraiable or use $nodename instead?

in Engine by (36 points)

1 Answer

+1 vote
Best answer
  1. Honestly this is entirely up to you
    Like if you're a one-liner junkie onready var is the way to go as it gives your code a sense of cleanliness and this becomes most apparent the more nodes you need to reference.

  2. Yes, yes and absolutely yes

Consider the following

onready var node = $nodename

func _ready():
    node.foo()

func _input(event):
    node.bar()

func _process(delta):
    node.foo_bar()

If you want another node to have the same functionality all you would have to do is change the reference

node = $another_nodename

Or if you decided to change name of the node there would only be one line to modify

onready var node = $different_node_name

There is also a matter of efficiency as there is a micro cost to calling the get_node func and even more so for the find_node one

by (6,942 points)
selected by

Thanks man. appreciate it.

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 Frequently asked questions and 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 [email protected] with your username.