I have a method that is going to take a while to finish to run from my _process(delta)
method. Since I don't want this to lag the main thread, I thought I could move the logic into a separate thread, but it's not working.
Basically, when the user changes a parameter on a field, a flag is set indicating the the component needs and update. The _process
method checks to see if the flag is set and if it is, if a thread is already running to update the component. If no thread is running, one is created and given my long method to run.
I then want to wait for the thread to finish so I can take the result and use it in my component. I thought I could do this by calling await thread.wait_to_finish()
, but that's just causing the main thread to block.
What's the right way to wait for a thread to complete?
var thread_build_mesh:Thread
var count:int = 0
func _process(delta):
if flag_update_mesh:
print("flag_update_mesh %d" % count)
count += 1
if !thread_build_mesh:
print("start thread")
thread_build_mesh = Thread.new()
thread_build_mesh.start(build_mesh)
await thread_build_mesh.wait_to_finish()
thread_build_mesh = null
print("end thread")
flag_update_mesh = false