Godot Version
Godot 4.6.2
Question
I have been using multi-threading in Godot for the procedural forest system in my game for over a year now. It loads chunks on a background thread, and then uses call_deferred to add nodes to the scene tree. I have also experimented with versions using RenderingServer and there seem to be similar problems either way.
Occasionally, I get crashes, and I have isolated it to the multi-threaded forest system (remove that system and the crashes go away). These crashes do not register any error or warning message in the editor, when running the game through the Godot editor.
My core question: Can 3D rendering nodes be created/instantiated on a background thread, and then added to the scene tree with call_deferred() ?
The docs clearly say this can be done, but perhaps this is not completely safe when creating Rendering nodes like MeshInstance3D?
I have read this document many times, and I am confident that my code is using mutexes/resources in the proper way:
Are either of these scenarios OK, running on a background thread or WorkerThreadPool?
Example, creating a mesh instance 3D:
func some_threaded_function() -> void:
var mesh_instance := MeshInstance3D.new()
# SOME_MESH_RESOURCE is preloaded(). is that a problem
mesh_instance.mesh = SOME_MESH_RESOURCE
some_node.add_child.call_deferred(mesh_instance)
Example, instantiating a packed scene that has 3D rendering nodes inside:
func some_threaded_function2() -> void:
# SOME PACKED SCENE contains rendering nodes, like MeshInstance3D or MultiMeshInstance3D
var scene := SOME_PACKED_SCENE.instantiate()
some_node.add_child.call_deferred(scene)
I’m kind of at my wits end trying to figure out where crashes are coming from, and I think it’s coming down to this line in the docs:
Instancing nodes that render anything in 2D or 3D (such as Sprite2D or MeshInstance3D) is not thread-safe by default.
And it sounds like the Separate threading option is not really recommended for production.
Thanks,
Mike
