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

Is it possible to make this coroutine anonymous or do I have to use the variable?

Asked by 7 years ago

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
0
You shouldn't be using coroutine for this. Use `spawn` BlueTaslem 18071 — 7y

2 answers

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

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.

Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

If you're not meaningfully1 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 

0
Neat LightModed 81 — 7y

Answer this question