Resource.Load() causes "MissingMemberException"

Godot Version

4.2.1 Stable Mono

Question

Hi, im trying to load saved resource (SettingsRES) via ResourceLoader.Load() but I always get this error

E 0:00:01:0396   ScriptManagerBridge.cs:146 @ Godot.NativeInterop.godot_bool Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(nint, nint, Godot.NativeInterop.godot_variant**, int): System.MissingMemberException: Cannot create script instance. The class 'DisplaySettingsRES' does not define a parameterless constructor.
  <C# Error>     System.MissingMemberException
  <C# Source>    /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs:146 @ Godot.NativeInterop.godot_bool Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(nint, nint, Godot.NativeInterop.godot_variant**, int)
  <Stack Trace>  ScriptManagerBridge.cs:146 @ Godot.NativeInterop.godot_bool Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(nint, nint, Godot.NativeInterop.godot_variant**, int)

You can see the loader code here:

private void LoadOrInitAll() {
    var currentPath = FullPath(CURRENT_NAME);
    var backupPath = FullPath(BACKUP_NAME);

    if (ResourceLoader.Exists(currentPath)) {
        _current = ResourceLoader.Load(currentPath) as SettingsRES;
    } else {
        _current = DefaultSettings();
        GDConsole.Log("Current settings load failed, creating new...");
    }
    
    if (ResourceLoader.Exists(backupPath)) {
        _current = ResourceLoader.Load(backupPath) as SettingsRES;
    } else {
        _current = DefaultSettings();
        GDConsole.Log("Current settings load failed, creating new...");
    }

}

And the resource that being loaded:

using Architecture.Services;
using Godot;


[GlobalClass]
public partial class SettingsRES : Resource {
    public SettingsRES(DisplaySettingsRES display, SoundSettingsRES sound) {
        Display = display;
        Sound = sound;
    }


    [Export] public DisplaySettingsRES Display { get; private set; }
    [Export] public SoundSettingsRES Sound { get; private set; }
}

Links:
Resources, I’m troubling to load
Loader script

What could I do wrong?
I’ll be looking forward your response. Thanks in advance!

Hi! The error message should be quite explicit.

You’re asking Godot to instantiate an object of type SettingsRES(through calling ResourceLoader.Load(...), but that type does not define a parameterless constructor. That is a requirement for all types instantiated by the engine.

Thanks a lot! For some reason I didn’t pay attention to that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.