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

Coroutine Dead unexpectedly

Started by Creeper9207, 05 November 2016 - 03:28 PM
Creeper9207 #1
Posted 05 November 2016 - 04:28 PM
I'm still fairly new to coroutines, so this weekend I am trying to further my knowledge of them by trying to create a multishell-esque program, and it was going well until I tried to emulate a sleep function.

http://pastebin.com/nAz7G7Qj

I was testing the new function in function "task1", but task1's coroutine is dead, while the other ones keep going, no error or anything, (execution entry point is wrapper.lua).

Thanks in advance!
Edited on 05 November 2016 - 08:20 PM
Creeper9207 #2
Posted 05 November 2016 - 09:17 PM
update: I had just forgotten to load a file, but now the screen just comes up blank with no error when attempting to load functions.lua
Edited on 05 November 2016 - 08:23 PM
Lupus590 #3
Posted 05 November 2016 - 09:40 PM
put some prints in problem areas as status reports.
Bomb Bloke #4
Posted 05 November 2016 - 11:49 PM
task1() calls print() and newSleep() ten times, but and neither they nor it yield - this means it'll run to completion the very first time you resume it. Presumably you intended to have newSleep() wait for some timer events…?
Creeper9207 #5
Posted 06 November 2016 - 03:17 PM
some newly emerging problems: so when I attempt to over-write coroutine.yield, with newYield, it gives me a parallel:29 error: attempt to call nil.

I also updated newSleep to have a yield, and then presumably coroutine "t" which launches the "Timed" function should have in theory resumed it after the given time, but does not.

new PB: http://pastebin.com/UktQ6XZD
Edited on 06 November 2016 - 02:17 PM
Bomb Bloke #6
Posted 07 November 2016 - 11:47 AM
Timed() is calling both coroutine.yield() and os.pullEvent() - stick with one or the other. If you don't know the difference between the two (or more importantly, the similarities), read section four here. You may be better off reading the whole post, but that section onwards is essential to plugging the holes in your code.

Timed() also tries to resume the functions task1() / task2() / task3() - you can only resume coroutines!

You need to look into localised variables - your tTask table is a mistake. When newSleep is called, it should store its timer ID in a local variable, meaning that multiple coroutines can call the function at once without causing conflicts within the global scope.