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.