How to access child nodes in setget method before _ready?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aaron
:warning: Old Version Published before Godot 3 was released.

I have a scene with this structure:

  • root
    • child

root has an export property with a setget method that affects child.

extends Node2D

export(Color) var colour = Color(1, 1, 1) setget _set_colour

func _set_colour(new_val):
    colour = new_val
    # Other relevant logic
    ...
    get_node("child").set_modulate(new_val)
    ...

This scene is instanced in another main scene. When I load the main scene during gameplay though, this scene’s _set_colour is run but gets an error because get_node("child") returns null.

Is it not possible to access the child node here?

:bust_in_silhouette: Reply From: volzhs

you can’t.
but you can do it by this.

extends Node2D

export(Color) var colour = Color(1, 1, 1) setget _set_colour

func _ready():
    _set_colour(colour)

func _set_colour(new_val):
    colour = new_val
    if has_node("child"):
        get_node("child").set_modulate(new_val)

OK.

My script is actually a tool mode script, and I was worried that changes to the child would not be visible in the editor if I only modified the child after _ready was run. Though trying it now I see that changes to the child are in fact visible in the editor.

Aaron | 2017-12-06 02:12