Make AnimatedSprite2D play longer

Godot Version

4.2.2

Question

I have character animations when it goes back and forth and want to add another one when it gets a damage, but the damage animation plays way too quickly since I bound right and left animations to key press it happens instantly. But when I don’t press any keys, it plays as it should be.

	if Input.is_action_pressed("right"):
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_h = true
	elif Input.is_action_pressed("left"):
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_h = false
	elif velocity == Vector2.ZERO:
		$AnimatedSprite2D.animation = "idle"

	#if lost all lives then game over
	if lives.get_child_count() == 0:
		Engine.time_scale = 0.5
		$CollisionShape2D.disabled = true
		$AnimationPlayer.play('death')
		rotation_degrees = 45		
		await get_tree().create_timer(0.3).timeout
		get_tree().reload_current_scene()
		Engine.time_scale = 1.0

I am adding it here as it is the only place when I can catch the life lost:

level.connect("enemy_collision", loosing_health)

# erasing life icons if collided with enemies
func loosing_health():
	$AnimatedSprite2D.animation = "damaged"
	lives.get_child(lives.get_child_count() - 1).queue_free()

When I add await, it throws an error because I am reloading the scene when there is no life left

How to make it play just a fraction of second?

in which line it tells the error, and what the error said?


	if Input.is_action_pressed("right"):
               # I add here if statement to check if animation is damage then
               # wait here for 0.3 seconds or less and then do the rest
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_h = true
	elif Input.is_action_pressed("left"):
                # same I did here
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_h = false
	elif velocity == Vector2.ZERO:
		$AnimatedSprite2D.animation = "idle"

And it throw and error: Cannot call method reload_current_scene on a null value

i see why, because you put these code in _process function, and there’s no blocking for entering this branch if of

so it got triggered twice

Should I put it somewhere else?

change to:

	if lives.get_child_count() == 0 and $AnimationPlayer and $AnimationPlayer.current_animation!="death":
		Engine.time_scale = 0.5
		$CollisionShape2D.disabled = true
		$AnimationPlayer.play('death')
		rotation_degrees = 45		
		await get_tree().create_timer(0.3).timeout
		get_tree().reload_current_scene()
		Engine.time_scale = 1.0

It worked! Thanks!