-'coroutine.resume(coroutine.create(function() end)' and 'coroutine.wrap(function() end)()'? Rest of question here because of the maximum of characters limit.
I tested both coroutine.resume(coroutine.create(function() end)
and coroutine.wrap(function() end)()
with repeat until nil
loops, and they both reacted the same, but what is the difference between them both? I know they can run separate types of code, but, I don't know what they both do differently from eachother.
They are essentially the same.
There is an additional functionality possible using coroutine.wrap
though:
function sayHello() print("Hi!"); wait(5); print("Bye!"); end conversation = coroutine.wrap( sayHello ); -- coroutine.wrap returns a function -- so `conversation` is now a function we can call conversation(); conversation(); conversation(); conversation(); -- Hi! -- Hi! -- Hi! -- Hi! -- Bye! -- Bye! -- Bye! -- Bye!
If you were to use coroutine.resume
and coroutine.create
, you'd have to repeat that each time to generate a new thread.
The idea is to use coroutine.wrap
if you repeatedly have to make new threads (probably with different arguments)
One their own, when spawning a single thread, the behavior is as-far-as-I-know identical, and performance differences will be negligible.