Exported variable of tool script not reflected in Editor

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

I have a tool script with an exported variable:

tool
extends Spatial

class_name Satellite

export var orbit_radius = 30.0

func _ready():
	var mesh = get_node("MeshInstance")
	mesh.transform.origin = Vector3(orbit_radius, 0, 0)

In the Editor, however, the mesh is always at (0, 0, 0). When I run the game, the mesh will appear at its proper destination, e.g. (30, 0, 0).

How can I have the value of orbit_radius affect the position of the mesh in the 3D editor?

================
EDIT:

Based on Lopy’s answer, I changed the code to this:

tool
extends Spatial

class_name Satellite

export var orbit_radius = 30.0 setget _set_radius
var mesh

func _ready():
	if mesh == null:
		mesh = get_node("MeshInstance")

func _set_radius(value):
	orbit_radius = value
	if mesh == null:
		mesh = get_node("MeshInstance")
	mesh.transform.origin = Vector3(orbit_radius, 0, 0)

I placed a breakpoint in the line mesh = get_node("MeshInstance") of the _ready() function. The line does not even execute before I get an error:

E 0:00:00.654 get_node: Node not found: MeshInstance. <C++ Error>
Condition “!node” is true. Returned: __null <C++ Source>
scene/main/node.cpp:1381 @ get_node() Satellite.gd:28
@ _set_radius()

So it seems the editor tries to run the setter before even calling _ready(), however the tree is not set up at this moment. It does work in the editor though!

Just to be sure, was the scene reloaded to check the tool script?

Ertain | 2021-01-27 17:47

Not by me on purpose. How would I do that?

hansiC | 2021-01-27 18:37

:bust_in_silhouette: Reply From: Lopy

You should be able to use a setget for that. The Editor does not call your _ready() regularly. It calls _init when the scene is saved, but your get_node() would probably fail inside an _init.

export var orbit_radius = 30.0 setget _set_radius

func _set_radius(value):
    orbit_radius = value
    var mesh = get_node("MeshInstance")
    mesh.transform.origin = Vector3(orbit_radius, 0, 0)
func _ready():
    _set_radius(orbit_radius) # Force the setter to be called

Sorry, accepted too soon :confused: Now it ONLY works in the editor. When I run the game, get_node(“MeshInstance”) does not find the node

hansiC | 2021-01-27 20:46