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

Coroutines

Started by Sewbacca, 15 March 2016 - 07:06 PM
Sewbacca #1
Posted 15 March 2016 - 08:06 PM
The parallel API allows to use functions with yieldings and it uses coroutines, but I don't understand, how coroutines can run for example sleep(1) or rednet.receive() (os.pullEvent())?
Edited on 15 March 2016 - 07:08 PM
Dragon53535 #2
Posted 15 March 2016 - 08:15 PM
Coroutine tutorial made by BB.

It's all just coroutine.yield

code for os.pullEvent:

function os.pullEvent( sFilter )
    local eventData = { os.pullEventRaw( sFilter ) }
    if eventData[1] == "terminate" then
	    error( "Terminated", 0 )
    end
    return table.unpack( eventData )
end

Code for os.pullEventRaw:


function os.pullEventRaw( sFilter )
    return coroutine.yield( sFilter )
end

So os.pullEvent is really just coroutine.yield with extra fluff to allow for termination.


Sleep and rednet.receive:


function receive( sProtocolFilter, nTimeout )
    --#snip. There was more stuff here to deal with starting the timer for your timeout and stuff, but right now it's fine.
while true do
  local sEvent, p1, p2, p3 = os.pullEvent( sFilter )
  if sEvent == "rednet_message" then
	  -- Return the first matching rednet_message
   local nSenderID, message, sProtocol = p1, p2, p3
   if sProtocolFilter == nil or sProtocol == sProtocolFilter then
	   return nSenderID, message, sProtocol
		 end
  elseif sEvent == "timer" then
	  -- Return nil if we timeout
	  if p1 == timer then
	   return nil
	  end
  end
end
end

Sleep:

function sleep( nTime )
    local timer = os.startTimer( nTime or 0 )
    repeat
	    local sEvent, param = os.pullEvent( "timer" )
    until param == timer
end
Oh hey, os.pullEvent again in both, they're just pre built functions that utilize os.pullEvent for tasks you're probably going to want to do anyways.
Sewbacca #3
Posted 15 March 2016 - 09:07 PM
And who resumes the coroutine?
And how can I resume my coroutines?
Edited on 15 March 2016 - 08:08 PM
Lyqyd #4
Posted 15 March 2016 - 09:13 PM
Check out the tutorial linked above and don't forget to open the spoilers. After you've read through it, we'll be happy to answer any specific questions you have about anything that was unclear.
Sewbacca #5
Posted 16 March 2016 - 12:45 PM
Okay, okay, …


When I discovered the coroutines, I didn't understood the whole function :(/> ,

but know I think they are much better than functions (not each time) and coroutines can handle functions very well. :D/>


:D/> :D/> :D/> !!!THANKS!!! :D/> :D/> :D/>
Edited on 16 March 2016 - 11:45 AM
Bomb Bloke #6
Posted 16 March 2016 - 12:54 PM
They serve a purpose, but if you can avoid using them, do it. They execute much slower than calling functions directly.
Sewbacca #7
Posted 16 March 2016 - 12:59 PM
They serve a purpose, but if you can avoid using them, do it. They execute much slower than calling functions directly.

I said, not each time.