I'm trying to sleep a Godot thread for a few seconds. In pure C# i can do this by doing:
Thread.Sleep(1000);
But i don't have any idea how to do this with Godot threads.
CodePudding user response:
The solution was using Godot.Semaphore
. Instead of doing Thread.Sleep()
, i can do semaphore.Wait()
to sleep, and semaphore.Post()
to unsleep.
CodePudding user response:
The equivalent of Thread.Sleep(1000);
for Godot is OS.DelayMsec(1000)
. The documentation says:
Delays execution of the current thread by
msec
milliseconds.msec
must be greater than or equal to0
. Otherwise, delay_msec will do nothing and will print an error message.Note: delay_msec is a blocking way to delay code execution. To delay code execution in a non-blocking way, see SceneTree.create_timer. Yielding with SceneTree.create_timer will delay the execution of code placed below the
yield
without affecting the rest of the project (or editor, for EditorPlugins and EditorScripts).Note: When delay_msec is called on the main thread, it will freeze the project and will prevent it from redrawing and registering input until the delay has passed. When using delay_msec as part of an EditorPlugin or EditorScript, it will freeze the editor but won't freeze the project if it is currently running (since the project is an independent child process).