Skip to content

Godot 4 .Net - Loading / Assigning a script exported from another project fails with error ("Cannot instance script because the associated class could not be found") #75352

@Heremeus

Description

@Heremeus

Godot version

v4.0.1.stable.mono.official [cacf499]

System information

Windows 10

Issue description

Exporting a script from one godot project and loading it in another does not work (following https://docs.godotengine.org/en/stable/tutorials/export/exporting_pcks.html). The ResourceLoader.Load<Script>() call finds the script and succeeds, but when assigning the script to a node, the following error occurs:

E 0:00:00:0477   Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* ): Cannot instance script because the associated class could not be found. Script: 'res://HelloWorld.cs'. Make sure the script exists and contains a class definition with a name that matches the filename of the script exactly (it's case-sensitive).
  <C++ Error>    Method/function failed. Returning: false
  <C++ Source>   modules/mono/csharp_script.cpp:2301 @ can_instantiate()
  <Stack Trace>  Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* )
                 NativeCalls.cs:5208 @ void Godot.NativeCalls.godot_icall_1_583(IntPtr , IntPtr , Godot.Variant )
                 GodotObject.cs:434 @ void Godot.GodotObject.SetScript(Godot.Variant )
                 ImportScript.cs:24 @ void ImportScript._Ready()
                 Node.cs:1783 @ Boolean Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
                 Node3D.cs:988 @ Boolean Godot.Node3D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
                 ImportScript_ScriptMethods.generated.cs:24 @ Boolean ImportScript.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
                 CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godot_string_name* , Godot.NativeInterop.godot_variant** , Int32 , Godot.NativeInterop.godot_variant_call_error* , Godot.NativeInterop.godot_variant* )

Loading other resources (e.g. scenes) works. The assembly was also loaded successfully. Might be related to this reported issue: .NET 6 - Running exported PCK fails to load scripts or fails to load hostfxr #72159

Steps to reproduce

Note: With the minimal reproduction project, only steps 3-5 and step 8 are required.

  1. Create two new Godot Projects (an exporting and an importing project)
  2. In the exporting, create a new script "HelloWorld" in res:// that prints "Hello World" on _Ready
  3. Export the exporting project (with "Runnable" unchecked) into a .pck file for your platform
  4. Copy the exported .pck file and the project's .dll from .godot\mono\temp\bin\ExportRelease\win-x64
  5. Paste the two files to the res:// folder of the importing project
  6. In the importing project, create a 3D scene and add a new script to the root node
  7. In the new script, paste the _Ready function below
  8. Run the importing project and check the Debugger->Errors list
public override void _Ready()
{
	// Prepare empty node
	Node3D node = new Node3D();
	AddChild(node);

	// Load assembly and pck file
	System.Reflection.Assembly.LoadFile(ProjectSettings.GlobalizePath("res://ExportingProject.dll"));
	ProjectSettings.LoadResourcePack("res://ExportingProject.pck");

	// Load script and assign it
	Script script = ResourceLoader.Load<Script>("res://HelloWorld.cs");
	node.SetScript(script);
}

Minimal reproduction project

MinimalReproductionProjects.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions