Parallel.waitForAny
From ComputerCraft Wiki
Revision as of 06:26, 12 February 2014 by Bomb Bloke (Talk | contribs) (Some attempts at clarification.)
Function parallel.waitForAny | |
Lets you run multiple functions at the same time, alternating between them until any happen to finish. Note that it takes function names as parameters (eg "function1", "function2", etc), as opposed to function calls (eg "function1()", "function2()", etc). The functions are not actually executed simultaniously, but rather this API will automatically switch between them whenever they yield (eg whenever they call coroutine.yield(), or functions that call that - eg os.pullEvent() - or functions that call that, etc - basically, anything that causes the function to "pause"). | |
Syntax | parallel.waitForAny(function1, function2, so on) |
Returns | A number indicating which function completed based on argument order |
Part of | ComputerCraft |
API | parallel |
Examples
Example | |
Tells the computer to run function1 and function2 at the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first. | |
Code |
function1 = function() -- Define function1. print (rednet.receive()) end function function2() -- Define function2. rednet.broadcast (read()) end parallel.waitForAny (function1, function2) -- Run both functions in parallel until one finishes. |
Output | Either it prints what it received via rednet, or it broadcasts the message the user typed. |