0 votes

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?

in Engine by (291 points)

1 Answer

0 votes
Best answer

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)
by (9,796 points)
selected by

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.

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 Frequently asked questions and 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 [email protected] with your username.