coroutines allow you to run code alongside other code, it creates a
thread that runs along the main thread (The main code), Now you may be wondering: What is a thread?
I think @SubtotalAnt8185 Explained it really well,
They allow you to have multiple Paths in executing code within code.
Simply: They allow code run alongside the Main Code which is the Main Thread.
This is useful if you want to multi-task with things, like if you wanted a loop, you can create a a thread with a
coroutine and have two things running:
local thread = coroutine.create(function() -- creates thread
while true do
print"Hi, I'm running alongside the main thread!"
coroutine.resume(thread) -- starts thread
while true do
print"Hi, I'm running within the main thread!"
If you want a function thread, you would use
local cofunc = coroutine.wrap(function(str: string)
cofunc("Hello!") --> Hello!
task is used to fire code within the Engine Sheduler, It is basically similiar to
coroutines in many ways, but to clear up one thing that has been said in this Topic:
delay() are deperacated and should be replaced with
task.spawn() are faster and better than
spawn(), which fires code immediately with the Engine’s Scheduler without throttling, while
spawn() does this with throttling,
task.spawn() as stated by Documentation should replace
end) -- this will automatically fire
task.spawn() will automatically call and resume this new
thread, if you want this functionality with
coroutines, you would do this for the following:
coroutine.resume(coroutine.create(function)) -- automatically fires this thread
coroutine.wrap(function)() -- automatically fires this thread
What are the Differences between the two?
Now, There real question we are asking is the Differences between these Libraries, to be honest, there are only minor Differences between the two:
coroutines give you more control over the
thread, you are able to check its status, stop it, and resume it.
threads are usually just meant to fire code, the call and fire a new
thread and continue to execute the code, you can only stop it by using
Simply: Fire and Execute
coroutine are basically very similar if you already caught that, but here are similarities:
Other than that, there are no real differences between them, they run code alongside other code, it just depends on the use cases for them.
Feel free to correct me on this information, I can always be wrong about stuff!