Detect animation_finished in a separate scene

Godot Version

Godot 4.2.2

Question

Hi, all,

Newbie here,

Following some videos on YouTube, which states everything should be its own scene, I’m making a game with a Main Scene, in the scene, I’ll have the player character and some enemy characters (all are separate scenes).

For example I have an enemy called Goblin (a scene), which has 3 AnimatedSprited2D animations: “idle”, “attack” and “die”.

At the end of its death animation, I would like to spawn a treasure chest, since I’d like to do some RNG operations to get random items from it. Different content will have different appearance, thus I made it into a scene.

Now, my problem is, I want to spawn the chest ONLY WHEN the Goblin finishes its death animation. While I can control the Goblin scene to play its death animation, I won’t know when the Goblin has finished its animation. I know there is an on???_animation_finished() signal in the AnimateSprite2D, but I couldn’t link that signal to my Main Scene to tell me when the Goblin is completely dead.

I tried to ignore the sequences, and spawn the chest as soon as the Goblin is dead in my game logic, but that just don’t look right…

So is it possible to detect when a scene finished its animation, or is my design fundamentally flawed? Like, I shouldn’t make everything into its own scene?

Please advice and thanks in advance.

Do you spawn the goblin in your MainScene? If yes, you can try this:
Goblin.gd:

signal died(position: Vector2)

func _on_animation_finished() -> void:
    if animated_sprite.animation == "death_animation" # your death_animation name here
        died.emit(global_position)
        queue_free()

MainScene.gd:

func spawnGoblin() -> void:
    var goblin = GOBLIN_PACKED_SCENE.instantiate()
    goblin.died.connect(goblin_died)
    add_child(goblin)

func goblin_died(death_position: Vector2) -> void:
    # insert code to spawn treasure_chest
    spawnTreasureChest(death_position)
1 Like

Thanks for the very detailed reply. Took me some time to fully grasp the idea, as I’m still new to Godot. :slight_smile:

Will try it out, thanks again!

1 Like

Yes, sorry for just throwing code at you. Its basically just the use of signals. When you emit a signal, all of the connected methods to it get executed

No worries, although I’m new to Godot, I’ve been programming in C++ for 20 years :smile:

I tried your code and it works like a charm, and you have even showed me how to clean up after the Goblin is dead :laughing:, saves me a lot of research time, really appreciate the clear and precise reply. :+1:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.