Is there a way I can call a function from AnimatedSprite2D?

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

I’d like to call a specific function (or send a signal) when I reach a specific frame in my AnimatedSprite2D. The regular AnimationPlayer supports this functionality, is there any way I can also use it for the animated sprite, even through code?

I really don’t want to have to move all my animations to AnimationPlayer just to do this. Thanks

:bust_in_silhouette: Reply From: popcar2

Answered by Cpt Chuckles on Discord:

If you really have to, you can connect frame_changed to a function that checks what frame it is, and does thing() at frame n:

For Godot 3:

extends AnimatedSprite

func _ready() -> void:
    connect("frame_changed", self, "_on_frame_changed")

func _on_frame_changed() -> void:
    if frame == 69:
        print("Do something here")

For Godot 4:

extends AnimatedSprite2D

func _ready():
	frame_changed.connect(_on_frame_changed)

func _on_frame_changed():
	if frame == 69:
		print("Do something here")