Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# assigning a default value to an exported custom resource property will cause error #80175

Closed
dasasdhba opened this issue Aug 2, 2023 · 9 comments · Fixed by #87838
Closed

Comments

@dasasdhba
Copy link

Godot version

v4.1.stable.mono.official

System information

Windows 10

Issue description

If you assign a default value to an exported custom resource property just like this:

[Export]
public MyResource Res { get; set; } = new();

Then you will get an editor error just as follows when building the solution:

modules/mono/glue/runtime_interop.cpp:1324 - System.ArgumentException: An item with the same key has already been added. Key: Game.MyResource
     at System.Collections.Generic.Dictionary`2.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

By the way, rebuilding again without restarting the editor at this time will also cause .NET: Failed to unload assemblies issue.

I think the editor should at least push a clear warning instead of throwing an error like this so that I have to restart the editor even if this behavior is not allowed.

Steps to reproduce

MyResource.cs:

using Godot;

namespace Game;

[GlobalClass]
public partial class MyResource : Resource
{
	public MyResource() { }
}

MyNode.cs:

using Godot;

namespace Game;

public partial class MyNode : Node
{
	[Export]
	public MyResource Res { get; set; } = new(); // can not assign a default value here
}

Building a project with these two cs scripts will cause editor error mentioned above.

Minimal reproduction project

gdcsharp-customresource-issue.zip

@RedworkDE
Copy link
Member

Cannot confirm on v4.2.dev.mono.custom_build [f9f5041]. Building, adding the script to a node and running it all work just fine.

@dasasdhba
Copy link
Author

dasasdhba commented Aug 4, 2023

I just tried official v4.2.dev2 and the issue still exists.
issue

I also tried v4.2.dev.mono.custom_build [eb4301b] later and the issue remains.

@dasasdhba
Copy link
Author

dasasdhba commented Aug 6, 2023

This issue will not happen for me if I change "MyResource" to "AResource", which seems to be relevant to the order of reloading the scripts . Additionally, If a scene containing MyNode has been opened in the editor, the issue also disappears.

@ipetrariu
Copy link

Bumping this since I've stumbled upon this same issue as well on 4.1 and also reproduced it when running 4.2 from source (roughly same steps as above).

I did a bit of debugging to see if I could see anything obviously wrong, but I'm not really familiar enough with the engine code to say for sure what's expected and not in this scenario. At a glance, the problem seems to be caused by the fact that the type for the member script (MyResource) will be added to the ScriptTypeBiMap during the default intialization of the owner script (MyNode), and then the same will be attempted when parsing the script for MyResource itself hence the double inserstion error.

Seemingly MyResource will also linger in the map and cause other issues down the line when attempting to unloading the project assembly as mentioned by dasasdhba.

I'm attaching my minimal project as well in case it helps with reproducing the issue.
godot_bug_example_proj.zip

@Rylius
Copy link

Rylius commented Sep 15, 2023

Just for tracking: Stumbled on the same problem (in 4.1.1) but commented on a different issue: #78513 (comment)

Also, for reproduction: Running this for the first time seems to work fine, the issue only occurs when making code changes and building. (At least that's how it was in my project, haven't tried the project from the comment above.)

Nice to see a PR addressing this!

@cjohnson57
Copy link

cjohnson57 commented Nov 27, 2023

I am also experiencing this on v4.1.1.stable.mono.official [bd6af8e0e], specifically after adding [Export] to my save Resource object. It happens after I edit any script and run, but if I run again after then no errors appear.

For me, it's happening even when I set the default values in the object's constructor rather than in the field declaration.

@cjohnson57
Copy link

Commenting again to add, I'm not sure if they're related, but the export values in my scenes keep getting randomly nulled out.

@LarsIsGaming
Copy link

LarsIsGaming commented Dec 5, 2023

EDIT: I'm also getting this #79519 frequently, and after reading through that suspect this is the underlying issue with what I've got happening below.

I'm seeing the same behaviour as @cjohnson57 in v4.2.stable.mono.official [46dc277] (the argument exception and also the export values getting nulled out randomly)

If it's useful info, I started seeing this after upgrading the my project to v4.2.stable.mono.official [46dc277] and adding in a script based on a generic base-class.

Structure I have is;

public abstract partial class BaseService<T> : Node
{ 
    _EnterTree() { ... }
    _Ready() { ... }
    _ExitTree() { ... }
}

public interface ICameraService
{ ... } 

public partial class CameraService : BaseService<ICameraService>, ICameraService
{
    [ExportCategory("Camera Service")]
    [Export] private CameraModuleConfigResource _config;
}

interestingy i've not seen the [Export] private CameraModuleConfigResource _config; above nulled out yet, but I am getting that seemingly randomly in other scenes.

Full output log;

Godot Engine v4.2.stable.mono.official (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.
  modules/gltf/register_types.cpp:63 - Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported.
--- Debug adapter server started ---
--- GDScript language server started on port 6005 ---
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=
Remove Node(s)
Remove Node(s)
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.ArgumentException: An item with the same key has already been added. Key: RiskyBusinessGames.Exodus.Shared.Services.BaseService`1[RiskyBusinessGames.Exodus.CameraModule.Services.ICameraService]
     at System.Collections.Generic.Dictionary`2.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.TryReloadRegisteredScriptWithClass(IntPtr scriptPtr) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 587
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.ArgumentException: An item with the same key has already been added. Key: RiskyBusinessGames.Exodus.Shared.Services.BaseService`1[RiskyBusinessGames.Exodus.CameraModule.Services.ICameraInputService]
     at System.Collections.Generic.Dictionary`2.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.TryReloadRegisteredScriptWithClass(IntPtr scriptPtr) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 587
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.ArgumentException: An item with the same key has already been added. Key: RiskyBusinessGames.Exodus.Shared.Services.BaseService`1[Exodus.Modules.SolarSystemMap.Interfaces.IMapLoaderService]
     at System.Collections.Generic.Dictionary`2.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.TryReloadRegisteredScriptWithClass(IntPtr scriptPtr) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 587
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.ArgumentException: An item with the same key has already been added. Key: Exodus.Modules.Shared.Factories.ComponentFactory`1[RiskyBusinessGames.Exodus.WorldMap.Components.MapItemComponent_LOD2]
     at System.Collections.Generic.Dictionary`2.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.TryReloadRegisteredScriptWithClass(IntPtr scriptPtr) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 587
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=
Updating materials, LineMaterial=, IconMaterial=

@cjohnson57
Copy link

Appreciate everyone's work to get this fixed! This is a huge boon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment