Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function my_function( ) -- this is just a demo function
- while true do
- local event, key = coroutine.yield( ) -- this stops the coroutine and waits for it to be resumed. It will set event, key to the arguments that it is resumed with
- if event == "key" then
- print( key )
- else
- error( "You didn't press a key!" )
- end
- end
- end
- local function my_other_function( )
- while true do
- sleep( 10000 ) -- would normally make the other function wait 10000 seconds
- end
- end
- local my_coroutine = coroutine.create( my_function ) -- creates a coroutine from my_function
- local my_other_coroutine = coroutine.create( my_other_function ) -- ditto
- while true do -- an infinite loop
- local event = { os.pullEvent( ) } -- captures the event in a table
- local ok, err = coroutine.resume( my_coroutine, unpack( event ) ) -- runs the coroutine with the event like [event, key, etc]
- coroutine.resume( my_other_function, unpack( event ) ) -- runs this too, and when sleep is called, it stops running because sleep->os.pullEvent->os.pullEventRaw->coroutine.yield( )
- if not ok then -- if the coroutine errored then...
- print( err ) -- print the error
- break -- exit from the loop
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement