Is it possible to make this coroutine anonymous or do I have to use the variable? Thanks!
while true do
local newThread = coroutine.wrap(function()
for i = 1, 10 do
end
coroutine.yield()
end)
newThread()
end
Is it possible to make this coroutine anonymous or do I have to use the variable? Thanks!
while true do
local newThread = coroutine.wrap(function()
for i = 1, 10 do
end
coroutine.yield()
end)
newThread()
end
All you have to do is call the result of coroutine.wrap directly.
while true do
coroutine.wrap(function()
for i = 1, 10 do
end
coroutine.yield()
end)()
end
Hope this helped.
If you're not meaningfully[^1] using coroutine.yield and coroutine.resume (or, if you use wait() or :wait()) then you should not use coroutine at all.
If you want to start a "background thread", use spawn or delay. spawn starts a "thread" that will be started soon™. delay starts a "thread" that will be started after the specified amount of delay.
while wait(1) do
spawn(function()
-- do stuff
end)
end
[^1]: in particular, you should call resume at least twice on the same coroutine to justify the use of coroutine.create over spawn
Neat
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
You shouldn't be using coroutine for this. Use
spawn