This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Konlab's profile picture

Coroutine help

Started by Konlab, 04 June 2014 - 06:22 PM
Konlab #1
Posted 04 June 2014 - 08:22 PM
Hi!
I need some coroutine help.
If I have this code:

function func1()
  read()
end
function func2()
  sleep(inf)
end
cor1 = coroutine.create(func1)
cor2 = coroutine.create(func2)
coroutine.resume(cor1)
coroutine.resume(cor2)
The 2 functions are running both at same time?
And how can I get, that coroutine is ended or not?
And how can I 'destroy' a coroutine?
And with coroutine.yield() and then coroutine.resume() I can 'pause' functions?

Please help me, and tell me how this works in CC.

Konlab
Bubba #2
Posted 04 June 2014 - 08:31 PM
I've made a tutorial for this here. Hopefully it will help you out - all of those questions and more are answered there. If you still need help with anything go ahead and post again in this thread.

Edit: Actually, your question about "destroying coroutines" is not answered there because Lua does not have a method for destroying coroutines. Dead coroutines are automatically handled by the garbage collector. As to differences between CC coroutines and regular coroutines, there are none that I'm aware of.
Edited on 04 June 2014 - 06:51 PM
Konlab #3
Posted 04 June 2014 - 09:04 PM
Thanks!
theoriginalbit #4
Posted 05 June 2014 - 02:51 AM
it should be mentioned in regards to the destroying of coroutines. as Bubba stated you can't specifically destroy a coroutine, however you can remove your reference to it so that you essentially 'forget' it was ever there. so set the variables they're assigned in to nil, example from your examples

cor1 = nil
by doing this the garbage collector (gc) will eventually come along and cleanup the coroutine (in this case I think Minecraft is fairly inefficient and invokes the gc every few game loops). the gc will clean up the coroutine even if its not dead (just suspended).
Edited on 05 June 2014 - 12:52 AM
Yevano #5
Posted 05 June 2014 - 03:50 AM
You're trying to do a pattern you would almost always do with the parallel library (though emulating parallel is mentioned in Bubba's doc). You should never try starting your own coroutines when they will end up calling os.pullEvent (both read and sleep do that) unless you know what to do with the return values and you know what arguments to give coroutine.resume. os.pullEvent will end up yielding the coroutines and expect to be resumed again with proper arguments, and unless you were expecting to have to do that, you'll probably just be calling coroutine.resume in an infinite loop without any arguments, which will cause errors in the API code which probably doesn't check for incorrect values being returned from os.pullEvent.

Use parallel.waitFor* and read Bubba's doc or one of the many Lua documentations on the Internet which talk about coroutines.
Edited on 05 June 2014 - 01:51 AM
Konlab #6
Posted 05 June 2014 - 03:17 PM
But I understand the parallel API, but I want to create that in coroutine API!
theoriginalbit #7
Posted 05 June 2014 - 03:21 PM
So you're attempting to write your own Coroutine Manager? Perhaps you should read the source for the Parallel API.
Konlab #8
Posted 05 June 2014 - 03:54 PM
I want to write my own multitaking API.
Yevano #9
Posted 05 June 2014 - 04:11 PM
I want to write my own multitaking API.

As theoriginalbit said, you should take a look at the source for the parallel API. The basic idea is that you just resume coroutines forever in a loop, sending them the correct event arguments and setting up filters in the correct way.
Lyqyd #10
Posted 05 June 2014 - 06:20 PM
I want to write my own multitaking API.

Yes, we understand that, and that's fine. The problem is that you don't seem to have a good understanding of ComputerCraft's coroutine model, or even a good foundational knowledge of how coroutines work. We are trying to point out resources such as the parallel API source code that can help you to learn how these things work, since you will need a solid understanding of them before you can write a useful multitasking API.