Hello everyone!
I was wondering if I could have a computer waiting for an event to happen (in this case, the event would be monitor touch) while the program is still running and calculating, etc. How would I do this? I have read about the Parallel and Coroutine API's, but I don't understand them fully.
Multitasking in CC
Started by GamerNebulae, Jun 02 2014 02:22 PM
4 replies to this topic
#1
Posted 02 June 2014 - 02:22 PM
#2
Posted 02 June 2014 - 03:21 PM
function pullEvents()
while true do
event, etc1, etc2, etc3 = os.pullEvent()
-- Do stuff with the events that get pulled.
end
end
function doOtherStuff()
while true do
-- Draw calculations etc
end
end
parallel.waitForAny(pullEvents, doOtherStuff)
Bearing in mind that no ComputerCraft function ever runs while another is running (be it on the same computer or otherwise), parallel.waitForAny() queues however many functions you pass it to run - automatically switching between them whenever they yield.
Pretty much anything that causes a function to "wait" will trigger a yield. Sleeping, for example, or just using os.pullEvent().
A handy feature is that all functions executed in this manner get their own event queues - events pulled by the other functions are only removed from their own copy of the queue. This makes it very useful for eg having turtles receive RedNet messages while moving (considering that they'd otherwise discard all events received during the movement process).
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users












