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

Multitasking in same shell menu

Started by TechnicalCoding, 08 June 2016 - 06:04 AM
TechnicalCoding #1
Posted 08 June 2016 - 08:04 AM
​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
end
-- 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
end
-- Is it possible?

Bomb Bloke #2
Posted 08 June 2016 - 09:04 AM
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

  etc

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

parallel.waitForAny(
  function()
    call read and store the result somewhere
  end,

  function()
    handle incoming rednet traffic
  end
)