0 votes

Code in Parent:

extends Node2D
onready var chi = get_node("child")
var par_var

func _ready():

func par_func():

Code in child:

extends Node2D 

onready var par = get_node("..")

var chi_var

func _ready():

func chi_func():

(don't ask me why!)
Only if the child calls the 'parfunc()' the chi.chivar=0 gives an error.
So: Parent and child can access each others variables; or their 'functions to do so'.
The parent can call a function in child, to change the parameter of the parent,
but the child can not call a function of the parent to change a parameter of the child.
Or am I missing something?
It seems as if they are both ready.
edit: oh... and in my game it was the other way around: Parent can't call child function to
get variables of parent.

in Engine by (50 points)
edited by

Can you share the error code you get?

i think this just does not work from start because of the order of creation...
either the parent or the child is not _started yet when you get there
maybe try after a delay?

func _ready() -> void:
    get_tree().create_timer(0.333).connect("timeout", self, "_after")

func _after() -> void:

invalid set index 'chi_var'(on base: 'nill) with value of type 'int'

Off course this is wrong in all the wright places. But doesn't the child have to exist,
to make that call in the first place?
All I can think of, is that a node can execute a function before it's connected to the tree.

p.s. Delaying didn't change a thing...

1 Answer

0 votes

Children are calling _ready before their parents. So your child-node gets its parent node, sets the value of par_var and then calls par_func. However, the parent node hasn't yet become ready, thus the onready var chi is still undefined (i.e. null). But in par_func you use it anyways, resulting in the error you've witnessed.

by (10,469 points)

Al wright, this makes sense. Thanks.
I didn't get get, that nodes can access code from other nodes before they are ready.

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.