Here is a very simple example that crashes.
using Godot;
using System;
using System.Threading;
public class Node : Godot.Node
{
public override void _Ready(){
System.Threading.Thread t = new System.Threading.Thread(() => this.Test());
t.Start();
}
public void Test(){
var imageTexture = new ImageTexture(); // this does not work
GD.Print("This is not printing because the thread crashed at the previous line. ");
}
}
So, creating a new ImageTexture object cannot be done from another thread. This is just one example, but I stumbled upon several things that I was not able to perform from another thread (for example using SurfaceTool.commit()). This behavior makes no sense to me. Either Godot does not support C# threads completely or they have to be used a specific way and not the way shown in my example.
I would like to emphasize that multithreading indeed works when no Godot methods are called. Also many Godot methods can be called from other threads. For example I was able to generate a mesh in the background with SurfaceTool, but had to call the 'commit' method from the main thread. The example I present crashes because of the line I indicated. Without this line, the message "This is..." gets printed to the console as expected.
Here is the error when running from the console:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at (wrapper managed-to-native) Godot.NativeCalls.godot_icall_ImageTexture_Ctor(Godot.ImageTexture)
at Godot.ImageTexture..ctor () [0x00019] in
/home/bluepangolin55/.local/share/godot/mono/solutions/5516125489248431140_2_3/GodotSharp/ObjectType/ImageTexture.cs:54
at Node.Test () [0x00001] in /home/bluepangolin55/workspace/bugtest/Node.cs:13
at Node.<_Ready>b__0_0 () [0x00000] in /home/bluepangolin55/workspace/bugtest/Node.cs:8
at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <71d8ad678db34313b7f718a414dfcb25>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <71d8ad678db34313b7f718a414dfcb25>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <71d8ad678db34313b7f718a414dfcb25>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <71d8ad678db34313b7f718a414dfcb25>:0
at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <71d8ad678db34313b7f718a414dfcb25>:0
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================