Export variable with a setter will reset to default value when the value has been changed in the inspector

Godot Version

4.2.1.stable

Question

I’m working on a custom Node called DirectionalMovementComponent that’s meant to move a target Node2D in a direction. I’m currently using it on a scene that I’ve changed the value of direction in the inspector to (0, -1). However, whenever the scene is instantiated the value of direction is reset back to it’s default value. Is there something I’m doing wrong in my scripting or is this a bug?

Here’s the DirectionalMovementComponent script:

class_name DirectionalMovementComponent
extends BaseMovementComponent
## Moves a target Node2D in a single direction.


## Direction target will move in. Will be normalized on set.
@export var direction := Vector2(0, 1):
	set = set_direction


func _ready() -> void:
	print("ready: " + str(direction))


func _physics_process(_delta: float) -> void:
	target.position += direction * speed
	print("physics_process: " + str(direction))


func set_direction(value: Vector2) -> void:
	value = value.normalized()
	print("setter: " + str(value.normalized()))

and the console output:

setter: (0, -1)
ready: (0, 1)
physics_process: (0, 1)
physics_process: (0, 1)
physics_process: (0, 1)

should be:

func set_direction(value: Vector2) -> void:
   direction = value.normalized()

Quite a silly mistake - thank you :slight_smile:

None of us want to admit it, but that’s like 90% of every programmer’s debugging time :smiling_face_with_tear:

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