Geting a casting error for when the node is the same type as the explicit cast type

Godot Version

Replace this line with your Godot version

Problem

casting error appereres at line 92 and I don’t know why. HitboxContainer class derives form Node3D I don’t see why it would have this issue

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(){}
    public override void _Ready()
    {
        base._Ready();
        InitHitBoxContainerUnderCheck();
        Init();
        CurrentAnimationChanged += (Pressent) =>{
            if(CurrentAnimation != Pressent)
            {

            }
        };
    }
    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
            {
                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();
    }
}

full error

/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type ‘Godot.Node3D’ to type ‘ProjectKazen.Framework.HitBoxContainer’.
at ProjectKazen.Framework.FGAnimationPlayer.InitHitBoxContainerUnderCheck() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 92
at ProjectKazen.Framework.FGAnimationPlayer._Ready() in C:\Users\User\Documents\project-kazen\Framework\Animation.cs\FGAnimationPlayer.cs:line 35
at Godot.Node.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/Node.cs:line 2401
at Godot.AnimationMixer.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationMixer.cs:line 789
at Godot.AnimationPlayer.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/AnimationPlayer.cs:line 759
at ProjectKazen.Framework.FGAnimationPlayer.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in C:\Users\User\Documents\project-kazen.godot\mono\temp\obj\Debug\Godot.SourceGenerators\Godot.SourceGenerators.ScriptMethodsGenerator\ProjectKazen.Framework.FGAnimationPlayer_ScriptMethods.generated.cs:line 140
at Godot.Bridge.CSharpInstanceBridge.Call(IntPtr godotObjectGCHandle, godot_string_name* method, godot_variant** args, Int32 argCount, godot_variant_call_error* refCallError, godot_variant* ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs:line 24

If this is code that must run in the editor, then HitboxContainer must also have [Tool] at the top, otherwise you will get the invalid cast errors.

[Tool] is at the top so you’re saying it is because of that it’s doing this?

If this script must run in the editor, then you also need to put [Tool] at the top of the HitboxContainer class.

Otherwise, if you don’t want this code to run in the editor, then yes, you should remove [Tool] as it’s likely the reason why you are getting invalid cast errors.

is there any way to let it run in the editor and not get these casting errors?

As I said, you need to put [Tool] at the top of your HitboxContainer class.

oh I miss read sorry

Thank you so much!