Godot multiple tweens issue

Godot Version

v4.6.stable.official [89cea1439]

I want to make my Control node move slightly up, whenver a mouse eneters button node, and whenever mouse leaves the button node, i want it go to the orginal place. The issue is that i dont know where button should move back after being lifted up. I cant just move it to its position+position_offset cause if it was lifted multiple times it will not come back to the original place. Is there any easy way to do it? Is there any simple solution?

extends Control
class_name Playmode

@export var position_offset: Vector2 = Vector2(0, 50)

@onready var button: Button = $Button

# var trigerred: int = 0
var previous_pos: Vector2


func _ready() -> void:
	button.pressed.connect(_on_button_pressed)
	button.mouse_entered.connect(_on_mouse_entered)
	button.mouse_exited.connect(_on_mouse_exited)


func _on_button_pressed():
	playmode_button_pressed.emit()


func _on_mouse_entered():
	tween = get_tree().create_tween()
	previous_pos = position
	tween.tween_property(self, "position", position-position_offset, 0.5)
	await tween.finished
	tween.kill()
	tween = null


func _on_mouse_exited():
	tween = get_tree().create_tween()
	tween.tween_property(self, "position", previous_pos, 0.5)
	await tween.finished
	tween.kill()
	tween = null
  • Store the button’s default position in _ready().
  • Kill the old tween when you want to create a new one.
  • Never use await. (Well, sometimes it is ok, but 99% time you are just creating new problems.)


var button_default_pos: Vector2
var tween: Tween

func _ready() -> void:
	button_default_pos = button.position


func _on_mouse_entered():
	if tween:
		tween.kill()
	tween = get_tree().create_tween()
	tween.tween_property(self, "position", button_default_pos - position_offset, 0.5)


func _on_mouse_exited():
	if tween:
		tween.kill()
	tween = get_tree().create_tween()
	tween.tween_property(self, "position", button_default_pos, 0.5)
2 Likes

Oh my god thank you!!! It works really well, i just added in _ready():

await get_tree().process_frame
default_pos = position

because the nodes were in VBoxContainer and i guess it didnt have time to change the position