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

os.pullEvent vs rednet.receive()

Started by jelibo, 25 September 2012 - 07:11 PM
jelibo #1
Posted 25 September 2012 - 09:11 PM
Hello,

is there any difference or benefit when implementing rednet listeners between these two? Naturaly they would be placed inside infinite loop…
Cranium #2
Posted 25 September 2012 - 09:34 PM
Theoretically you could do either. rednet.receive() waits SPECIFICALLY for a rednet message, whereas os.pullEvent() waits for ANY event. One disadvantage is, if you do not filter out unwanted events, it could bog down your computer. Think of it this way:
You have os.pullEvent() as your listener. It is waiting for any event. I could set up several redstone repeaters and timers to spam it with redstone events. The real rednet message may be lost in that. You can filter out rednet message by doing this: event,p1,p2,p3 = os.pullEvent("rednet_message"). That would listen ONLY for rednet messages. But if you do that, why wouldn't you use rednet.receive()? It all comes down to what exactly you are trying to listen to.
KaoS #3
Posted 26 September 2012 - 07:53 AM
in some cases you want your PC to listen for rednet messages and check them & process them etc for only a certain amount of time, you could try rednet.receive(time) but that does not work with high traffic areas so what you do is


os.startTimer(time)
while true do
  local input={os.pullEvent()}
  if input[1]=='rednet_message'  then
    --process input here
  elseif input[1]=='timer' then
    break
  end
end