Jemy191 / GDBridge

Package to simplify C# -> GDScript interoperability
https://www.nuget.org/packages/GDBridge/
The Unlicense
29 stars 3 forks source link

need Default constructor for generated bridge classes #9

Closed jasonswearingen closed 5 months ago

jasonswearingen commented 5 months ago

Hello, I am running into a weird edge case.

When rebuilding my csharp project in my IDE, the godot editor does a cold-reload, and throws this error:

  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.MissingMemberException: Cannot create script instance. The class 'PhantomCamera3DBridge' does not define a parameterless constructor.
     at Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(IntPtr scriptPtr, IntPtr godotObject, godot_variant** args, Int32 argCount) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 146
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.MissingMemberException: Cannot create script instance. The class 'PhantomCamera3DBridge' does not define a parameterless constructor.
     at Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(IntPtr scriptPtr, IntPtr godotObject, godot_variant** args, Int32 argCount) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:line 146

Not sure why the editor needs that, but it seems to be because cold-reload calls the default constructors on all .net objects.

Adding this code manually fixes the problem:

public partial class PhantomCamera3DBridge
{
   public PhantomCamera3DBridge() : base(null){}
}

so, maybe this should be added to the generated code by default?

This is on Godot 4.2

Jemy191 commented 5 months ago

Thanks I will check that. It seem to be a godot bug or some oversight.

jasonswearingen commented 5 months ago

oh one more thing, this only happens when the scene has the [Tool] attribute. without it there is no error.

also, I just tested on Godot 4.3dev6.mono and it's still occuring

Jemy191 commented 5 months ago

The scene? Do you mean the bridge class? Could you give me a snipet of your code?

jasonswearingen commented 5 months ago

The scene that I'm instancing the bridge from is a [Tool]. it's something about how the Editor cold-reloads tool code.

here's an example of what i'm doing

[SceneTree]
[Tool]
public partial class Main : Node
{
   public World World { get; set; }

   public PhantomCamera3DBridge pcamBridge { get; set; }
   public Node3D pcam { get; set; }

   // Called when the node enters the scene tree for the first time.
   public override void _Ready()
   {
      World = new();
      AddChild(World);

      var xbotCharacter = _GD.InstantiateScene<xbotCharacter>();
      this._AddChild(xbotCharacter);

      pcam = _.PhantomCamera3D;
      pcamBridge = new PhantomCamera3DBridge(pcam);

     }

//more code......
Jemy191 commented 5 months ago

Bridge used with [Tool] class are special case. Adding a parameter less constructor to the genereted class would lead to confusion on it use. It would be best to add it manually as a partial class.