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

Do Coroutines need to be Removed?

Asked by
Xduel 211 Moderation Voter
8 years ago

So lets say I have:

for i = 1,100 do
    local x = coroutine.create(function()
        print("Alright")
    end)

    coroutine.resume(x)
end

Once the coroutine is made and has run, do I have to remove it to prevent script lag? I think of it as creating 100 functions only to be used once, and once run are just a waste of space. But I think I'm wrong; can someone clear this up for me? Thanks!

0
You don't need to remove it. Acheo 230 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

No - the thread scheduler and garbage collector should do all the clean-up for you. In any case, there isn't really a way to "remove" coroutines in that sense -- it will just reach the dead state and no longer be affecting your game in any way.

You can prove this to yourself by opening studio and enabling "Script Performance" in the VIEW tab. Put this Script into workspace:

for i = 1, 500 do
    coroutine.resume(coroutine.create(function()
        wait(1);
        warn(i);
    end))
    wait();
end

and play test the game. Check out the row for the Script in the Script Performance window -- it will get up to about 18 threads/second with 0.1% activity while running, but once its done it will read 0's across the board.

0
Wow, wasn't expecting an answer with solid proof. Thanks a Bunch! Xduel 211 — 8y
Ad

Answer this question