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

Coroutines....

Started by TheJebForge, 09 January 2015 - 07:43 PM
TheJebForge #1
Posted 09 January 2015 - 08:43 PM
I'm writing the OS with windows and multitasking. How to run the 2 or more programs at once! I know that uses the coroutines, but how to use the coroutines with program?
I tried that one time, but when i ran it, it errors all of the custom functions… I'm using the public functions.
There's the code:

fil = fs.open("testProgram","r")
text = fil.readLine()
fil.close()
local func,err = loadstring(text)

local con = coroutine.create(func)
while true do
tpr("lool",1,1)
con.resume()
sleep(0.1)
end

tpr(text,x,y) - Print text in specific coordinates

The file with functions is already ran.
It errors all of the custom functions within the coroutine.

Please help with that.
Edited on 10 January 2015 - 06:58 AM
SquidDev #2
Posted 09 January 2015 - 09:06 PM
I would suggest reading the source for multishell (/rom/programs/advances/multishell) and looking and the parallel API
TheJebForge #3
Posted 10 January 2015 - 07:23 AM
I don't understand the multishell code…
Lyqyd #4
Posted 10 January 2015 - 07:27 AM
What about parallel?

We're here to help you learn, but just giving you code to copy that you don't understand doesn't help you in the long run, and makes more work for us. Try reading through the PIL chapter on coroutines, and check out some related articles on the lua-users wiki. A solid understanding of coroutines is essential to understanding how ComputerCraft's event-based model works.
TheJebForge #5
Posted 10 January 2015 - 07:43 AM
I'll try it. The parallel api, for example: parallel.waitForAny(windowDraw, programFunc)
Will that run windowDraw() first and then programFunc()?
Bomb Bloke #6
Posted 10 January 2015 - 07:51 AM
More or less. It'll execute windowDraw until the first time that function yields. When that happens, it'll start executing programFunc, then it'll attempt to carry on with windowDraw from where it left off, and so on.
Lyqyd #7
Posted 10 January 2015 - 07:52 AM
Depending, of course, on what type of event comes in next and whether or not either or both coroutines passed filters back.
TheJebForge #8
Posted 10 January 2015 - 07:58 AM
OK, Thanks! I'll try do something with this.