←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft


Multitasking in same shell menu

TechnicalCoding's Photo TechnicalCoding 08 Jun 2016

​MultiTasking in same panel?

Is it possible?
I have an small example of multitasking which I wonder is possible.
local message = read()

-- Now it normaly would wait for a response for the read function,
-- but is it possible to make it do functions before the function
-- recieve any data? ex.

local message = read()

-- V This will happen when its waiting for response.
while message = false then
 -- Do something
-- In the while statement I guess there would be something else ofcourse, but how?

-- V Will happen when its getting response for read()
if message=="String" then
 -- Do something
-- Is it possible?


Bomb Bloke's Photo Bomb Bloke 08 Jun 2016

The usual way of things is to write an event listener loop which can handle every event type your script is interested in (as opposed to calling a pre-made function such as read(), which can only handle a subset of events). Eg:

while true do
  local event, par1, par2, etc = os.pullEvent()

  if event == "key" or event == "char" then
    add stuff to input string

  elseif event == "rednet_message" then
    process incoming rednet traffic


However, the parallel API allows you to run multiple functions as coroutines, and allows execution control to switch between them whenever they yield.

    call read and store the result somewhere

    handle incoming rednet traffic