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

[Lua] rednet.receive() vs rednet_message

Started by MrJohnson, 01 January 2013 - 02:29 PM
MrJohnson #1
Posted 01 January 2013 - 03:29 PM
Hi,

I started using ComputerCraft a couple of days ago, and today, I decided to setup some turtles to write a new year message, in lights, to the world. Although I have a lot of programming experience, I know virtually nothing about Lua, and as time was limited to get the message up on my server before people joined, I've kept things very simple. It all works fine but it brings up a question for me: how efficient is rednet.receive() when compared with subscribing to rednet_message?

Essentially, I'm doing this:


function listen()
  rednet.open("right")

  while true do
   id, message = rednet.receive()
   parse(message)
  end
end

With 100 turtles, it has a noticeable effect on performance, and rather than changing all 100 turtles to find out if subscribing to rednet_message would be better, I was hoping someone would be able to tell me first.

Anyway, here's what it looks like for anyone that's interested and I hope you all have a happy 2013. :)/>

Spoiler



Dlcruz129 #2
Posted 01 January 2013 - 04:22 PM
I believe they do pretty much the same thing, but rednet.receive supports timeout. I usually use rednet.receive unless I'm also pulling other events.
ChunLing #3
Posted 02 January 2013 - 03:16 AM
If you're not using a timeout, then os.pullEvent("rednet_message") is better. But not by any grand amount, performance-wise.
MrJohnson #4
Posted 03 January 2013 - 12:08 AM
Thanks for the help. I'll give it a shot at some point.
theoriginalbit #5
Posted 03 January 2013 - 01:15 AM
also using rednet.receive() if you don't provide a timeout then the program will wait until it gets one before moving on (unless its in a separate coroutine) as apposed to checking if the event is rednet_message, it can continue to run while still having the event stack get items pushed on.

EDIT: Obviously if you use os.pullEvent("rednet_message") it will not continue, only if you allow all events and just check its the rednet event that you can continue with other things.
ChunLing #6
Posted 03 January 2013 - 02:40 AM
Huh? What's this about moving on and continue with other things?