So I was trying to make utility structures in my C# Godot project and I ended up with a weird error that occured when rebuilding with these two classes:

`
[Tool]
public partial class TimeDuration : RefCounted {
[Export] public float startTime = 0;
[Export] public float Duration {
get => Time.GetTicksMsec() - startTime;
set {;}
}

public TimeDuration() : base() {;}

public void Start(){
    startTime = Time.GetTicksMsec();
}
public static implicit operator float(TimeDuration timer) => timer.Duration;

}

[Tool]
public partial class BoolData : RefCounted {
[Export] public bool currentValue = false;
[Export] public bool lastValue = false;

[Export] public TimeDuration trueTimer = new();
[Export] public TimeDuration falseTimer = new();

[Export] public bool Started {
    get => currentValue && !lastValue;
    private set {;}
}
[Export] public bool Stopped {
    get => !currentValue && lastValue;
    private set {;}
}

public BoolData() : base() {;}

public void SetVal(bool value) {
    if (currentValue) falseTimer.Start();
    else trueTimer.Start();

    lastValue = currentValue;
    currentValue = value;
}

public static implicit operator bool(BoolData data) => data.currentValue;

}
`

The error goes as follows:

modules/mono/glue/runtime_interop.cpp:1324 - System.ArgumentException: An item with the same key has already been added. Key: TimeDuration
at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary
2.Add(TKey key, TValue value)
at Godot.Bridge.ScriptManagerBridge.ScriptTypeBiMap.Add(IntPtr scriptPtr, Type scriptType) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.types.cs:line 23
at Godot.Bridge.ScriptManagerBridge.AddScriptBridge(IntPtr scriptPtr, godot_string* scriptPath) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 419

Note: the issue happens without any existing instances of the said classes being used in the project, these classes just existing in the project triggers the error to appear.
This also happens if I make both classes inherit from Resource instead of RefCounted and if I remove the ToolAttribute.

I'm suspecting the issue comes from BoolData containing two instances of the same TimeDuration type, but I just might be unaware of some explicit limitation of GodotObject.

  • xyz replied to this.

    xyz
    Ok so after more experimentation, I've reached a very specific set of conditions for the issue to occur:
    1 - the "Parent" class needs to have both its script file and class named "BoolData".
    2 - it also needs to export at least one instance of any RefCount-inheriting class.
    3 - the project has to be rebuilt, needing all above conditions fulfilled after rebuilding.
    4 - the "Child" class cannot be one of the native Godot Resource or RefCounted classes, only a user-made class (I haven't tried whatever would be the equivalent in GDScript)
    5 - the project has to have been built before (the project I provided has not yet been built so you'd need to hard-build it twice for it to occur)
    6 - sometimes the default option for Creating a new Script has to be C# Script before the first rebuild of the project instance (???)
    So for example, creating a GDScript before rebuilding for the first time, can prevent the error from happening

    So the way to reach the error is the following:

    • load the project
    • build the project blankly
    • change something in the source files (to force a hard-rebuild when attempting a rebuild)
    • rebuild
    • profit??

    By the way, I'm working on Windows, Godot 4.1.1 .NET stable, dotnet v7.401; maybe that's part of the issue

    (I'm really sorry for exposing the world to this eldritch glitch that I've just uncovered)

    test.zip
    2kB

    I've just tried doing this with a different name to replace BoolData and it still happens, even though during my testing, it NEEDED to be named BoolData.
    I've really no idea, I guess condition n°1 is tied to something else I missed?

    • xyz replied to this.