What's the difference between putting a variable outside or inside the ready function?

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

Hello :), noob question here:
I have this code:

extends Node2D

onready var line = get_node ("Line2D")
onready var point1 = get_node ("punto1").global_position
onready var point2= get_node ("punto2").global_position
var pool_vector = PoolVector2Array ()
var direction_p1p2 = point1.direction_to(point2)

func _ready():
#	var direction_p1p2= point1.direction_to(point2)

	print (direction_p1p2)
	line.points = pool_vector
	print (point1)
	print (point2)
	print (pool_vector)

I was putting the var direccion_p1p2 = poin1.direction_to(point2) outside of func _ready():

and I was getting this error when I was trying to print it print (direction_p1p2)
"invalid call. nonexistent function 'direction_to' in base 'nil' Why it has to be inside of func _ready(): ?

I think it is because outside of the func_ready(): we store the variables, but we don’t do anything with them until we put it in a function. Am I right?

Thanks a lot :slight_smile:

:bust_in_silhouette: Reply From: Inces

It is not really an issue about being inside ready() or not.

Look, You introduce ONREADY var point2, but below You introduce normal var direction p1p2 and make it dependent on point2. So when the project starts, your p1p2 variable is created before point2, and so p1p2 cannot calculate itself from non-exisiting component.
When You have put p1p2 into ready() function - only then point2 is created first and p1p2 is created next using existing point2. The same would happen if You just introduced directionp1p2 as ONREADY. This is what ONREADY does - it waits until ready() function of the node to execute following line of code

Now I see clearly, thanks for the great answer! :slight_smile:

Maranpis | 2021-12-02 18:22

:bust_in_silhouette: Reply From: DaddyMonster

Just to flesh out the previous (quite correct) answer. So, you click to start the game and your computer starts executing Godot’s code that creates the tree, instancing all the node objects. It does this in a particular order but let’s not worry about that right now.

So func _ready(): and onready exist because if you try to reference a node that doesn’t exist [yet] it’ll fail. So the readies put you in a nice safe state to start.

Just note, that before onready var point1 = get_node ("punto1").global_position is actually ready point1 = nil.

So as Inces quite rightly says, by using onready you delayed the those variables but you didn’t delay distance_to so you were asking the computer to do nil.distance_to(nil) and a nil object doesn’t have a distance_to method. Which is exactly what Godot told you in the exception.

The other thing to bear in mind is there’s a big difference in scope. So variables declared inside a method (eg _ready) are only accessible within that method (function) whereas member variables have much wider scope and can be accessed by all methods and even external scripts.

Thanks for point it that out, now I think I get it :slight_smile:

Maranpis | 2021-12-02 18:22