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

[SOLVED][Lua][Thread] Help With Coroutine

Started by ryan0788, 16 March 2012 - 01:14 AM
ryan0788 #1
Posted 16 March 2012 - 02:14 AM
I'm trying to write to a monitor as well as the screen at the same time but when I call coroutine.create(myDrawFunction()) it doesn't run simultaneously.
Call my function:

local monitor = coroutine.create(misc.writeMonitor)
coroutine.resume(monitor)

My function:

function writeMonitor()
local sides = {
"top",
"bottom",
"left",
"right",
"front",
"back"
}
local validSide = ""

for i = 1, table.getn(sides) do
  if peripheral.getType(sides[i]) == "monitor" then
   validSide = sides[i]
   break
  end
end

if validSide ~= "" then
  local monitor = peripheral.wrap(validSide)
  while true do
   pcall(sleep, 0.1)
   screen.drawFullBorder(monitor)
  end
end
end
Casper7526 #2
Posted 16 March 2012 - 03:03 AM
http://www.computercraft.info/forums2/index.php?/topic/679-is-it-possible-to-run-two-lines-at-once/

Co-routines don't run at the same time, they just have the illusion of running at the same time based on knowing when a function yields without being inside that function.
rerere284 #3
Posted 16 March 2012 - 04:33 AM
if you do it like:


while true do
sleep(1)
term.redirect( monitor )
--print your stuffs
term.restore()
--print same stuff on main monitor
end

Then it will print what you want on both. I think it will anyway.
ryan0788 #4
Posted 16 March 2012 - 03:52 PM
Alright, but the problem is that I need to run 2 while true loops at the same time… :s Is that even possible?
Thanks for the help btw :D/>/>
rickydaan #5
Posted 16 March 2012 - 04:08 PM
parrallel.waitForAny(functionone, functiontwo)
IDK with or without strings, you need to put the loops inside!!!
ryan0788 #6
Posted 16 March 2012 - 04:17 PM
Alright, it works! Thanks! :D/>/>