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

Rednet broadcast delay

Started by AliasXNeo, 09 February 2013 - 04:12 PM
AliasXNeo #1
Posted 09 February 2013 - 05:12 PM
Greetings,

I have two computers running a simple os.pullEvent() loop:

rednet.open("top")

while true do
  event, id, msg = os.pullEvent()

  if event == "rednet_message" then
    if msg == "test"
      doStuff()
    end
  end
end

Then I have another computer broadcasting rednet messages:

rednet.open("top")
rednet.broadcast("test")
rednet.broadcast("test1")

Now, the above code doesn't fail, but only the first computer looking for the message "test" actually does anything. The second one remains idle as if it never got the message (even though it was sent). So, I tried this:

rednet.open("top")
rednet.broadcast("test")
sleep(1)
rednet.broadcast("test1")

And it worked perfectly. I tried changing it to sleep(0.5) and anything below one second and it had the same issue before. Is this intended, lag, or am I simply doing something wrong? It's really frustrating to have to have a second delay between time sensitive steps.
AliasXNeo #2
Posted 09 February 2013 - 05:23 PM
After doing more testing, I found the problem. Since it's broadcasting each computer gets the broadcast and processes the first broadcast at the same time. It seems while they are processing the first broadcast, the second one simply goes unnoticed. I figured there'd be some sort of internal stack/queue for events, but apparently not. Is there a way around this limit? (Without having to use a unique ID to send the message each time)
ChiknNuggets #3
Posted 09 February 2013 - 05:35 PM
From what i can see in your code, you are sending both at nearly the same time, and the reason i believe it doesn't pick up on the second one is that at the point of broadcasting your doing the function "dostuff()" for the first information, so while its doing this its not actually searching/waiting for another broadcast, i think that if you look into the parallel API, it might fix this problem.
ChunLing #4
Posted 09 February 2013 - 09:06 PM
There is an event queue, but many functions will discard events other than the one they want.

For instance, sleep() sets a timer event and then pulls events till it finds the timer event…it doesn't do anything with the other pulled events, so they end up being discarded.

The rednet.receive() function does the same thing, only looking for a rednet_message event rather than a timer. It will discard everything else.

The commonly used input function read()…again, discards events other than keyboard input.

Most of the turtle commands also discard events until they get a turtle event telling whether the called function succeeded or failed (and the successful function calls take up to .4 seconds).

So go through doStuff() and check for calls to functions that wait for a specific event…cause while they're waiting, they pull/discard other events.