Variables resetting after _ready

Godot Version



I’ve run a couple of prints on this and am pretty certain this is the issue, I have a script that saves the position of the node it’s attached to at ready but it seems like after ready it’s setting back to 0,0, is there anything here that might be causing this?

extends Sprite2D

@export var openFile: Node2D
var openFileGD = Script
@export var fileSlideAmount = 1
var mouseHovering = false
var startPos = Vector2()
var slidePos = Vector2()

func _ready():
	var startPos = self.position
	var slidePos = Vector2(self.position.x + 200, self.position.y)
	openFileGD = openFile.get_script()

func _on_area_2d_input_event(viewport, event, shape_idx):
	if (event is InputEventMouseButton && event.pressed):
		openFile.visible = true

func _process(delta):
	if mouseHovering == true:
		self.position = self.position.move_toward(slidePos, 6)
		#self.global_position = self.global_position.move_toward(startPos, 6)

func _on_area_2d_mouse_entered():
	mouseHovering = true

func _on_area_2d_mouse_exited():
	mouseHovering = false

You are redefining the startPos variable in _ready(). So you are never setting the global start position, you are only setting the local variable that shadows the global one.

To fix this, remove the var in front of the assignment. That way you are using the already defined variable instead of creating a new one.

1 Like

OOOOOOH ok. thanks so much, saints like you make learning code make sense

1 Like

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