Handling Tweens interrupted by non-tween action

Godot Version

4.3

Question

I want to use Tween to handle a menu animation. I have a texture that should be animated to move in a direction (up or down, for example) from its original position, then moved to a position in the opposite direction, and finally animated back to the original position.

Scenario:

  1. The player clicks on the top button, the grid in the scene will move up 1000 pixels.
  2. It will disappear from the script since I’m using a Subviewport.
  3. Then, I’ll place it in the opposite direction (1000 pixels below the original position in the center).
  4. Finally, it will move 1000 pixels from below until it reaches the center.

Sometimes it is hard to understand Tweens, but here is the solution:

func tween_game_mode(direction:TweenDirection) -> void:
	var tween:Tween = create_tween()
	tween.set_ease(Tween.EASE_OUT)
	tween.set_trans(Tween.TRANS_BACK)
	tween.set_parallel(false)
	tween.tween_property(
		theme_icon,
		"global_position:y",
		1000 * direction,
		1.0
	)
	tween.tween_property(
		theme_icon,
		"global_position:y",
		0,
		1.0
	).from(1000 * direction * -1)
	tween.play()
	return

Where the direction will be a value of -1 or 1. So, you should declare the tweens, set the parallel as false (because you don’t want them to run together) and then play().