Cast exception problem

Godot Version

Replace this line with your Godot version

Problem

FGAnimationLibrary wont cast to a regular AnimationPlayer when it derives from AnimationPlayer AND it has the [Tool] thing at the top of it I don’t know why it is doing this.

using Godot;
namespace ProjectKazen.Framework;
[GlobalClass]
[Tool]
public partial class FGAnimationPlayer : AnimationPlayer
{
    [Export]
    private Godot.Collections.Array<FGAnimationLibrary> FGAnimationLibraries;
    [Export]
    private Godot.Collections.Array<AnimationLibrary> AnimationLibraries;
    [Export]
    bool ReInit;
    public HitBoxContainer HitBoxContainer;
    public string LastAnimation = null;
    public int CurrentAnimationFrame; 
    public FGAnimationLibrary GetFGAnimationLibrary(string LibName)
    {
        foreach(FGAnimationLibrary AnimationLibrary in FGAnimationLibraries)
        {
            if(AnimationLibrary.Name == LibName)
            {
                return AnimationLibrary;
            }
        } 
        return null;
    }
    public void AddFGAnimationLibrary(FGAnimationLibrary AnimationLibrary)
    {
        FGAnimationLibraries.Add(AnimationLibrary);
    }
    public FGAnimationPlayer()
    {
        CurrentAnimationChanged += (_) =>
        {
            if(CurrentAnimation != _)
                HitBoxContainer.InitHitBoxes(GetCurrentFGAnimation().FightData);  
        };
    }
    public override void _Ready()
    {
        base._Ready();
        InitHitBoxContainerUnderCheck();
        Init();
 
    }
    public void InitHitBoxContainer()
    {   
        HitBoxContainer.Name = "HitBoxContainer";
        Node Owner = GetTree().CurrentScene is null ? EditorInterface.Singleton.GetEditedSceneRoot() : GetTree().Root;
        AddChild(HitBoxContainer);
        HitBoxContainer.Owner = Owner;
    }
    public FGAnimation GetCurrentFGAnimation()
    {
        FGAnimation Animation = null;
        foreach(FGAnimationLibrary AnimationLibrary in FGAnimationLibraries)
        {
            for(int i = 0; i < AnimationLibrary.FGAnimationList.Count;i++)
            {
                string AnimationName = AnimationLibrary.FGAnimationList[i].ResourceName;
                FGAnimation FGAnimation = AnimationLibrary.FGAnimationList[i];
                if (CurrentAnimation.Contains(AnimationName))
                {
                    Animation  = FGAnimation;
                }
            }
        }
        return Animation;
    }
    public void InitHitBoxContainerUnderCheck()
    {
        if(HitBoxContainer is not null)
        {
            if(HitBoxContainer.GetParent() is not null){
                if(!HitBoxContainer.GetParent().Equals(this))
                {
                    CallDeferred("InitHitBoxContainer");
                }
            }
            else
            {
                
                CallDeferred("InitHitBoxContainer");
            }
        }
        else
        {
            bool NodeTest = HasNode("HitBoxContainer");
            if(NodeTest)
            {
                HitBoxContainer = (HitBoxContainer)GetNode("HitBoxContainer");
            }
            else
            {
                FGAnimation CurrentAnimation = GetCurrentFGAnimation();
                if(CurrentAnimation is not null)
                {
                    HitBoxContainer = new(CurrentAnimation.FightData);
                }
                else
                {
                    HitBoxContainer = new();
                }
            }
        }
    }
    public void Init()
    {
        ClearAnimations();
        InitHitBoxContainerUnderCheck();
        for (int k = 0; k < AnimationLibraries.Count;k++)
        {
            AnimationLibrary AnimLib =  AnimationLibraries[k];
            AnimationLibrary AnimLibDuplicate = (AnimationLibrary)AnimLib.Duplicate();
            for (int i = 0; i < AnimLibDuplicate.GetAnimationList().Count;i++)
            {
                Animation CurrentAnimation = AnimLibDuplicate.GetAnimation(AnimLibDuplicate.GetAnimationList()[i]);
                for(int j = 0; j < CurrentAnimation.GetTrackCount(); j++)
                {
                    CurrentAnimation.TrackSetInterpolationType(j,Animation.InterpolationType.Nearest);
                }
            }
            AddAnimationLibrary("",AnimLibDuplicate);
        }
    }
    public void SpawnHitBoxes()
    {
        foreach(FGAnimationLibrary FGALib in FGAnimationLibraries)
        {
            foreach(string Animation in FGALib.GetAnimationList())
            {
                FGAnimation FGAnimation = (FGAnimation)FGALib.GetAnimation(Animation);
                //FGAnimation.FightData
            }
        }
    }
    public void ClearAnimations()
    {
        for (int i = 0; i<GetAnimationLibraryList().Count;i++)
        {
            RemoveAnimationLibrary(GetAnimationLibraryList()[i]);
        }
    }
    public override void _Process(double delta)
    {
        base._Process(delta);
        if (ReInit)
        {
            ClearAnimations();
            Init();
            ReInit = false;
        }
        if (IsPlaying())
        {
            CurrentAnimationFrame = (int)(CurrentAnimationPosition * 24 + 0.5f);  
        }
    }
    public void Play()
    {
        base.Play();
    }
}

Error mesege

/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86
/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.AnimationLibrary’ to type ‘ProjectKazen.Framework.FGAnimationLibrary’.
at Godot.Collections.Array1.get_Item(Int32 index) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1418 at Godot.Collections.Array1.GetEnumerator()+MoveNext() in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs:line 1834
at ProjectKazen.Framework.FGAnimationPlayer.GetCurrentFGAnimation() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 56
at ProjectKazen.Framework.FGAnimationPlayer.<.ctor>b__8_0(String _) in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 36
at Godot.AnimationPlayer.CurrentAnimationChangedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 705
at Godot.DelegateUtils.InvokeWithVariantArgs(IntPtr delegateGCHandle, Void* trampoline, godot_variant** args, Int32 argc, godot_variant* outRet) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs:line 86

I might be off base because I don’t know C# (only C++), but it seems like Godot.AnimationLibrary is a class name and you’re also using AnimationLibrary with the same capitalization as a variable name. Is legal in C# to have a variable name that’s identical to a type name in the same scope? I’d assume the compiler wouldn’t like that.
foreach(FGAnimationLibrary AnimationLibrary in FGAnimationLibraries)

Seems like the standard C# naming conventions use camelCase for variable names and PascalCase for class names. Never mind, I see the Godot C# API uses PascalCase for basically everything.

it is legal if it wernt then it would just give an error in the Ide instead of a runtime error