what are coroutines and how to use them?
xxxxxxxxxx
coroutine the problem solver of all the languages only not python since its slow
anyways lets see what i mean.
function foo() while wait(1) do end
foo()
so now the problem is the code is yielding at foo and never
reaches end you want to keep going without losing your while loop
how can we achive it? this is where coroutines come to play
where you can make a new thread and control it how ever you want
spawn(function() foo() end)
tadaah its that easy lets now go deeper
lets say you want something like this
if (thread.status == "dead") then print("thread is dead") end
we cant do that with spawn() thats where you have coroutines
but we have not only .create but wrap which one?
coroutine.create is the best used to have most control over your thread
coroutine.wrap() is the same as spawn()
local cour = coroutine.create(function() foo() end)
local wrap = coroutine.wrap(function() foo() end)
wrap()
couroutine.resume(cour) -- this is the diffrence and this
if (coroutine.status(cour) == "dead") then print("dead thread") end
easy huh?
you can also do more with it like .yield .resume and more
caution you can't yield a yielded thread a yielded thread is when
in a loop or thread is yielded because of wait or calling another function
for best results check coroutine.isyieldable(cour) before calling the yield
method
best uses:
when i want to multitask without yielding the main thread