Exported value effects only updating in editor, not in game or in inspector?

Godot Version

v4.2.1

Question

I have created a scene whose root is a Node3D. I have attached a script to that node. It is a tool script with exported values.

My desire is to be able to insert this custom scene into another scene, and then tweak some attributes via the inspector.

One field is “length”. It has some default. When I type a value and press enter, the scene in the editor is affected, but the number shown in the inspector reverts back to the default value.

Although the change shows in the inspector, it does not show in game.

Any help with either issue (getting number to stay changed in inspector, or getting change to affect in game) would be appreciated. Thanks.

Here is the relevant portion of my script:

@export var length: float:
	set(length):
		spring_top.position = Vector3(0, length/2, 0)

So now I have the opposite problem. I updated my code to this:

@export var length: float = 2:
	set(new_length):
		length = new_length

Now the value in the inspector updates, but the object in the editor doesnt.

Of course, you’d think I could just put in this as well:

spring_top.position = Vector3(0, length/2, 0)

But now that throws an error because spring_top is null??

Why wasnt it null before? spring_top is a child node that is referenced in my script via

@onready var spring_top = $SpringTop

Ok, finally got it:

@export var length: float = 2:
	set(value):
		length = value
		if Engine.is_editor_hint():
			spring_top.position = Vector3(0, length/2, 0)

func _ready():
	spring_top.position = Vector3(0, length/2, 0)

I needed to update the spring_top value inside the setter, but only when in the editor. And setting it in the _ready() function handles it in game.

If there is a better or more idiomatic way of doing this, please let me know,

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.