Previewing node's child changes in editor

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

Let’s say I have a light plant (serves as background item to provide light in a 2D platformer). This plant can have couple of different looks, but the light is always at the same spot.

So I create a new scene with root node Node2D, then put a Sprite and Light2D nodes as children. Because the plant can have couple of different looks, I create a spritesheet of how the plant can look like, and set the Sprite nodes vertical and horisontal frame counts accordingly.

Now, I can set the ‘frame’ property of Sprite node to be random, but I want the environment to have some consistency, so I won’t do that.

I can attach a script to the root (Node2D) and export a variable that I would connect to Sprite nodes frame property, so it would look a bit like this:

export(int) var spriteFrame setget set_sprite_frame

func set_sprite_frame(f: int):
    $Sprite.frame = f

The result of this is that I can set the exact look of my plant in level editor, but I cannot see that immediate change in the editor, as it just changes the middleman (spriteFrame property) of the root Node2D, and not the Sprite node’s frame property

How do I make this change visible in the editor?

One of the solutions is that I put the Sprite node as the root node, and simply put the Light2D as it’s only child, but that is not the point of this question. The question is how to export child node variables that can then be seen in editor.

If you’d like me to make it a bit more logical, imagine that I need both the frame of the Sprite node AND the energy from the Light2D node to be exported to the root node and the changes to these properties to be visible in the editor. Now the solution of making one of these two nodes a root node falls off.

:bust_in_silhouette: Reply From: path9263

Ran across this today too, using the tool keyword at the top of your script should allow this to work. It is important to use setget (like you have) to set variables you want to update in the editor.

More details here: Running code in the editor — Godot Engine (stable) documentation in English

Here’s what my code looked like:

extends someNodeType

export(Texture) var btnTexture = preload("res://assets/yourIcon.png") setget setIcon

func setIcon(tex):  # this is a setter function
	btnTexture = tex  # <-- important to actually set the var in the setter function

Note that $icon is the child node that this node is updating.