Braintweaser: child can't tell parent to to change child's variable?

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

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 ‘par_func()’ the chi.chi_var=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.

Can you share the error code you get?

p7f | 2020-07-30 16:03

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:

rakkarage | 2020-07-30 17:36

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…

Black Monkey | 2020-07-31 00:31

:bust_in_silhouette: Reply From: njamster

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.

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

Black Monkey | 2020-08-01 19:06