So basically im trying to get an animation to play on a certain event for a set amount of time before swapping back to idle, i use an await timer but when I use this await timer it stays going so if an animation plays during the timer of the last animation it will swap back to the idle before the time that the animation was intended to swap back
The code is this

func _on_area_2d_body_entered(body):

if "Left" in body.name:
	body.queue_free()
	animated_sprite_2d.animation = "Left"
	await get_tree().create_timer(.15).timeout
	animated_sprite_2d.animation = "Idle"

elif "Down" in body.name:
	body.queue_free()
	animated_sprite_2d.animation = "Down"
	await get_tree().create_timer(.15).timeout
	animated_sprite_2d.animation = "Idle"

elif "Right" in body.name:
	body.queue_free()
	animated_sprite_2d.animation = "Right"
	await get_tree().create_timer(.15).timeout
	animated_sprite_2d.animation = "Idle"
	
elif "Up" in body.name:
	body.queue_free()
	animated_sprite_2d.animation = "Up"
	await get_tree().create_timer(.15).timeout
	animated_sprite_2d.animation = "Idle"
  • xyz replied to this.

    GDDev Refactor your logic so it doesn't use await.

      xyz like this? cus if so it doesnt work

        if "Left" in body.name:
      	body.queue_free()
      	animated_sprite_2d.animation = "Left"
      	get_tree().create_timer(.15).timeout
      	animated_sprite_2d.animation = "Idle"
      elif "Down" in body.name:
      	body.queue_free()
      	animated_sprite_2d.animation = "Down"
      	get_tree().create_timer(.15).timeout
      	animated_sprite_2d.animation = "Idle"
      
      elif "Right" in body.name:
      	body.queue_free()
      	animated_sprite_2d.animation = "Right"
      	get_tree().create_timer(.15).timeout
      	animated_sprite_2d.animation = "Idle"
      	
      elif "Up" in body.name:
      	body.queue_free()
      	animated_sprite_2d.animation = "Up"
      	get_tree().create_timer(.15).timeout
      	animated_sprite_2d.animation = "Idle"
      • xyz replied to this.

        GDDev No not like that. You just removed the word "await" from the code. Apart from not making much sense, that doesn't amount to refactoring the code/logic.

        Instead of await, use regular signal callbacks. That way you can simply disconnect the callback if you want to cancel its execution when the signal triggers.

        You can open the inspector to see which signals can help you. At the same time, you can merge the same code.