• RuntimesGodot
  • Check if an animation exists before playing it

Hello, is it somehow possible to check if an animation exists before playing it? Something like this:

public void SetAnimation(string animation, bool loop) {
        if (!GetAnimationState().HasAnimation(animation))
            return;
        GetAnimationState().SetAnimation(animation, loop);
}

I'm using Godot 4.4.1 C#/mono. I have already tried wrapping it in a try-catch but it throws an error anyways, and it breaks the game. How can I fix this?

Related Discussions
...

To check if an animation exists before playing it in Godot using the spine-godot runtime, you can use the SkeletonData to verify the presence of the animation. Here's how you can implement this in C#:

public void SetAnimation(string animation, bool loop) {
    var skeletonData = GetSkeleton().Data;
    if (skeletonData.FindAnimation(animation) == null)
        return;
    GetAnimationState().SetAnimation(animation, loop, 0);
}

This code snippet uses FindAnimation on the SkeletonData to check if the animation exists. If the animation is not found, it returns early, preventing any errors from occurring when trying to set a non-existent animation.

    Spinebot That works, thank you!

    • Misaki 님이 이 게시물을 좋아합니다..