Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the difference between? -Read Question for more

-'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.

1 answer

✓ Accepted answer

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.

3 comments

0

So, their both kindof of the same, but 'coroutine.wrap' has more advantages?

0

Yeah. 'coroutine.wrap' allows you to use the function identifier to start the coroutine.

2

Wrapping the coroutine is better if you're using it multiple times. The other way is better if you're using it only once.

This discussion is preserved as it was. New answers, comments, and votes are closed.