0 votes

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)
    pool_vector.append(point1)
    pool_vector.append(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 :)

Godot version 3.4
in Engine by (26 points)

2 Answers

+1 vote

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

by (5,643 points)

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

+2 votes

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.

by (1,877 points)

Thanks for point it that out, now I think I get 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 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.