+1 vote

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.

in Engine by (692 points)
edited by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.