Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the difference between? -Read Question for more

Asked by 10 years ago

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

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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.

0
So, their both kindof of the same, but 'coroutine.wrap' has more advantages? TheeDeathCaster 2368 — 10y
0
Yeah. 'coroutine.wrap' allows you to use the function identifier to start the coroutine. Spongocardo 1991 — 10y
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. adark 5487 — 10y
Ad

Answer this question