+2 votes

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.

in Engine by (17 points)

1 Answer

+2 votes
Best answer

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: https://docs.godotengine.org/en/stable/tutorials/misc/running_code_in_the_editor.html

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.

by (251 points)
selected by
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.