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

Is it perfectly OK to kill a script using coroutines?

Asked by 6 years ago

I'm making a driver to a game.

local function Driver()
    --do stuff
end

It needs to be able to escape the sequence at any time. Nomally this is done by checking a variable in a loop, but the driver I have create is much too complex for this repetitive checking.

Coroutines can suspend themselves at any given time. Knowing this, I made the driver into a coroutine that will suspend itself.

local function Driver(co)
    --do stuff
    --Some very long stack later
    coroutine.yield(co);
    --more stuff
end

Driver = coroutine.wrap(Driver)
Driver(Driver); --Start the driver

So, essentially this driver will be able to escape at any time it wants. Even though the thread is "suspended" and not "dead", it can be garbage collected later since it will be unreferenced.

Is this an acceptable practice? or is there a flaw with it's implementation?

0
Perfect in idea! It's not safe to have a corutine yield itself. there are times when that will make you "lose" the routine and start creating lag. Other than just reading into coroutines a bit more, this is a great example of how to learn to code! Great question too! Bellyrium 310 — 6y
0
Even then, that was a roblox specific bug so it may be fixed. Just try to not keep creating coroutines every time and you'll do golden! Bellyrium 310 — 6y

Answer this question