Class CSharpScript.New() works in editor but not during runtime

Godot Version

Godot_v4.2-stable_mono_win64

Help

I’m trying to load in a .cs file during runtime from the executable’s directory. It works in the editor but not after exporting. No error messages.

  • The load path is correct and it doesn’t return null.
  • No exceptions were thrown and proceeds to print "Loaded".
  • If TestScript.cs was missing when running exported, it would error missing file as expected.
  • I’ve tried replacing TestScript.cs with a empty file and still no exceptions.

Please guide me to properly load a CSharpScript during runtime. Attached project files below too.
Thanks!

GD.Print($"Loading Path: {path}");

CSharpScript csTestScript = ResourceLoader.Load<CSharpScript>(path);
csTestScript.New();  //Prints Hello World!

GD.Print("Loaded");
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce GTX 1060 6GB

Loading Path: E:\Documents\Godot Projects\Debug\CSharpLoadTest\bin/TestScript.cs
ERROR: Cannot open file 'E:\Documents/Godot Projects/Debug/CSharpLoadTest/bin/TestScript.cs'.
   at: (modules/mono/utils/string_utils.cpp:152)
ERROR: Failed to read file: 'E:\Documents/Godot Projects/Debug/CSharpLoadTest/bin/TestScript.cs'.
   at: (modules/mono/csharp_script.cpp:2797)
ERROR: Cannot load C# script file 'E:\Documents/Godot Projects/Debug/CSharpLoadTest/bin/TestScript.cs'.
   at: (modules/mono/csharp_script.cpp:2867)
ERROR: Failed loading resource: E:\Documents/Godot Projects/Debug/CSharpLoadTest/bin/TestScript.cs. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:274)
ERROR: Error loading resource: 'E:\Documents\Godot Projects\Debug\CSharpLoadTest\bin/TestScript.cs'.
   at: (core/core_bind.cpp:73)
ERROR: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at AutoLoad..ctor() in E:\Documents\Godot Projects\Debug\CSharpLoadTest\AutoLoad.cs:line 15
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Godot.Bridge.ScriptManagerBridge.CreateManagedForGodotObjectScriptInstance(IntPtr scriptPtr, IntPtr godotObject, godot_variant** args, Int32 argCount)
   at: void Godot.NativeInterop.ExceptionUtils.LogException(System.Exception) (:0)

when added TestScript.cs to exported version

Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce GTX 1060 6GB

Loading Path: E:\Documents\Godot Projects\Debug\CSharpLoadTest\bin/TestScript.cs
Loaded

My conclusion Runtime cannot compile file “TestScript.cs” and run it?

if (OS.HasFeature("editor")) is your problem, delete and you have

Thanks for more insight, but I need if (OS.HasFeature("editor")) because I’m excluding TestScript.cs from export. This is just an example project with the same problem which I’m trying solve for another project.

The general case this may be for modding or custom user interface with a external cs script.

is not gonna work then. External .cs file is just text file.
you needed look for “Handling External Processes in C#” or make dll or something

1 Like

I see, thanks again, I’ll look into it.