215 posts
Location
Netherlands
Posted 02 June 2014 - 03:22 PM
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.
7083 posts
Location
Tasmania (AU)
Posted 02 June 2014 - 04: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).
1852 posts
Location
Sweden
Posted 02 June 2014 - 09:23 PM
Just gonna leave this useful
link here
215 posts
Location
Netherlands
Posted 03 June 2014 - 05:43 PM
Just gonna leave this useful
link here
I'm reading it and trying to understand. Maybe something for the future to understand ;)/>
1852 posts
Location
Sweden
Posted 03 June 2014 - 06:11 PM
I'm reading it and trying to understand. Maybe something for the future to understand ;)/>
Coroutines isn't the easiest thing to understand but with that tutorial it gets easier :P/>