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

Rednet.receive arg

Started by CastleMan2000, 08 July 2012 - 03:21 AM
CastleMan2000 #1
Posted 08 July 2012 - 05:21 AM
My idea is for rednet.receive to have another argument: ID. I know you can do this with a little extra coding, but having the filter built in to the same line would make it a bit more convenient, like:


local id, msg = rednet.receive(nil,21) -- would nil work as a placeholder?

It would allow "private" rednet conversations to be a bit easier.
Pinkishu #2
Posted 08 July 2012 - 01:16 PM
interesting not sure what the best way to do something like that is :)/>/>
maybe


local orec = rednet.receive
function rednet.receive(ID,timeout)
  local start = os.clock()
  local sid,msg,dist = nil,nil,nil
  while os.clock() < start+timeout do
    sid,msg,dist = orec(start+timeout-os.clock())
    if not ID or sid == ID then break end
  end
  return sid,msg,dist
end
(untested)
Xfel #3
Posted 08 July 2012 - 01:29 PM
Or one could adapt the original function (which wouldn't require saving the old one):

function receive( nTimeout, nID )
local timer = nil
if nTimeout then
  timer = os.startTimer( nTimeout )
end
while true do
  local e, p1, p2, p3 = os.pullEvent()
  if e == "rednet_message" and ( not nID or p1 == nID ) then
   return p1, p2, p3
  elseif e == "timer" and p1 == timer then
   return nil, nil, nil
  end
end
end

Actually, id should be the first parameter, but for compability, it's better that way round.
Pinkishu #4
Posted 08 July 2012 - 01:35 PM
Or one could adapt the original function (which wouldn't require saving the old one):

function receive( nTimeout, nID )
local timer = nil
if nTimeout then
  timer = os.startTimer( nTimeout )
end
while true do
  local e, p1, p2, p3 = os.pullEvent()
  if e == "rednet_message" and ( not nID or p1 == nID ) then
   return p1, p2, p3
  elseif e == "timer" and p1 == timer then
   return nil, nil, nil
  end
end
end

Actually, id should be the first parameter, but for compability, it's better that way round.

Sure one could, was too lazy to pull that out though :)/>/>
CastleMan2000 #5
Posted 08 July 2012 - 07:01 PM
That's what I had in mind. That's why I asked if rednet.receive(nil,id) would work for indefinite times.