0 votes

I have a simple script/node, run an animation and then remove it

using Godot;
using System;

public class OneShotAnimation : AnimatedSprite
{
    public override void _Ready()
    {
        Connect("animation_finished",this,nameof(AnimFinished));
    }
    private void AnimFinished(){
        GetParent().RemoveChild(this);
    }

}

Unfortunately this works about half the time, and the other half the completed animation is left onscreen and in the remote scene tree. Why isn't it running to completion 100% of the time?

HOWEVER, as a workaround, using the frame_changed signal does work 100% of the time.

using Godot;
using System;

public class OneShotAnimation : AnimatedSprite
{
    public override void _Ready()
    {
        Connect("frame_changed",this,nameof(FrameChanged));
    }
    private void FrameChanged(){
        int lastFrame = Frames.GetFrameCount(Animation) - 1;
        bool isLastFrame = Frame == lastFrame;
        if (isLastFrame) AnimFinished();
    }
    private void AnimFinished(){
        GetParent().RemoveChild(this);
    }

}

Though this removes the animatedSprite when the last frame begins, not when it ends.

asked Mar 14, 2019 in Engine by jarlowrey (605 points)
edited Mar 14, 2019 by jarlowrey

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.