How do i make a node smoothly move with just one button click?

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

i am currently working on a 2d game with grid based movement and i want the player to move smoothly from one position to another. Right nowI use code like this:
func _process(_delta): if Input.is_action_just_pressed("ui_up"): if can_go_up == true: self.position += Vector2 (0,-100) It gets the job done but is very choppy since the player just teleports somewhere instead of smoothly moving.

:bust_in_silhouette: Reply From: Enfyna
tween = create_tween()
var target_pos = self.position + Vector2 (0,100)
tween.tween_property(self,"position",target_pos,0.1)
tween.tween_callback(
func end_movement():
	self.position = target_pos
)

You can use tween for this. tween_property will make the movement and after it ends save the position with tween_callback. Tween_callback will call the callable after tween_property ends. You can put the end_movement function somewhere else and call it from tween_callback but I wanted to keep it short. And end_movement is a normal function so you can add more things it doesnt have to be 1 line.