+1 vote

Hello there! I'm making new threads to improve performance and load some terrain in the background. I was testing out threads when I found the following problem:

I have this function in World:

func test_threads():
    var newchunk=MeshInstance.new()
    var mythread=Thread.new()
    var err
    err=mythread.start(newchunk,"test_threads","butt")#Need to pass user data
    var a=mythread.wait_to_finish()

Then this function in chunk.gd:

func test_threads(userdata):
    print("chunk.test_threads got run")
    var mesh=Mesh.new()
    return userdata

And this crashes! Some other cases:
Remove var mesh=Mesh.new() -> it runs fine
Change to var mesh=MeshInstance.new() -> It crashes
Change to var mesh=Resource.new() -> It runs fine

So do we know of this problem? Can anyone else make a mesh from a new thread?

in Engine by (56 points)
edited by

1 Answer

0 votes


Modifying a unique resource from multiple threads is not supported, but loading them on threads or handling a reference is perfectly supported. Scenes, textures, meshes, etc. Can be loaded and manipulated on threads, then added to the active scene in the main thread.

What I do to work around this is generate the vertex data I need on a thread, then on the main thread when complete, generate the mesh with the vertex data.
Not ideal, but that is as close as you can get for now.

by (52 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.