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

Multitasking

Started by ElmuKelmuZ, 17 July 2014 - 10:42 PM
ElmuKelmuZ #1
Posted 18 July 2014 - 12:42 AM
I want to have 3 functions/blocks of code running at the same time/parallel.

I need to have a mouse event listener, and a keyboard event listener working parallel, separately from the main loop i'm working in.

How would I achieve this? I tried coroutine.wrap but it had no successful results

Code I tried but didn't have the 2 runtime functions running parallel:


function runtimeMouse()
while true do
  local event, button, xPos, yPos = os.pullEvent("mouse_click")
  print("Mouse button clicked: ", button, " => Click Position X: ", xPos, " => Click Position Y: ", yPos)
end
end

function update()
print("hey gimme input;")
local i = read()
if i == "exit" then
  return false
end
return true
end

function runtime()
while true do
  local cont = update()
  if not cont then
   break
  end
end
end

coroutine.resume(coroutine.create(runtimeMouse))
runtime()
Edited on 17 July 2014 - 10:51 PM
MKlegoman357 #2
Posted 18 July 2014 - 01:19 AM
The easiest solution here would be to use ComputerCraft's built-in Parallel API. Have a read on this tutorial to learn about how to properly implement a coroutine system in CC.
Imred Gemu #3
Posted 19 July 2014 - 05:33 AM

function runtimeMouse()
while true do
  local event, button, xPos, yPos = os.pullEvent("mouse_click")
  print("Mouse button clicked: ", button, " => Click Position X: ", xPos, " => Click Position Y: ", yPos)
end
end
function update()
print("hey gimme input;")
local i = read()
if i == "exit" then
  return false
end
return true
end
function runtime()
while true do
  local cont = update()
  if not cont then
   break
  end
end
end
--Runs both functions at once.
parallel.waitForAny(runtime, runtimeMouse)