Am I right by stating I can only have one single instance of a coroutine running at a time? This question may be difficult to interpret, so I have included an example below.
co = coroutine.create(function() print("hi scripting helpers") wait(10) end) for i = 1, 10 do coroutine.resume(co) end
This would only print "hi scripting helpers" once and then error once 10 seconds has elapsed.
How do I go about solving this?
NOTE: This isn't the actual purpose of why I'm asking, I just included this example to make the question easier to understand.
Thanks for your help.
EDIT:
Sorry bit of a noob here, not used to being able to edit my initial question.
I found a more relevant solution to my problem so someone said it would be best if I add it here.
function newcor(arguments) coroutine.resume(coroutine.create(function() print(arguments) wait(10) end)) end for i = 1, 10 do newcor("hi scripting helpers") end
The above example lets me enter in arguments and will run an infinite number of times, even when the original coroutine is still running.
If you're just using coroutines to schedule threads in parallel, don't use coroutines. Just use delay
or spawn
.
If you want something to happen every 10 seconds:
function thing() print("Hi") end while wait(10) do spawn( thing ) end
If you want this loop to be on its own thread, then just spawn that:
spawn(function() while wait(10) do print("Hi") end end)
Resuming the same coroutine multiple times doesn't restart it, it continues from the last coroutine.yield
. ... If you're not using coroutines for that purpose, then just stick with spawn
-- it's a little easier to understand less information
Alright, I found a more relevant solution to my problem, and this lets me enter arguments too.
function newcor(arguments) coroutine.resume(coroutine.create(function() print(arguments) wait(10) end)) end for i = 1, 10 do newcor("hi scripting helpers") end