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

Random Rednet Too Long Without Yeilding

Started by applesauce10189, 06 May 2014 - 09:21 PM
applesauce10189 #1
Posted 06 May 2014 - 11:21 PM
I have a single player world in which I code things for the sake of coding and improving a computercraft facility I've made. Of all the times I've had this world open/close one time out of all the cumputers/programs I have this one randomly got a too long without yeilding from rednet. Not quite sure how to fix so here's my code:


rednet.open("left")
local lights = false
while true do
local id, message = rednet.receive()
  if message == "glcheck" then
	rednet.broadcast("glgood")
  end
  if id == 4 then
	if message == "l1" then
	  lights = true
	elseif message == "l2" then
	  lights = false
	end
  end
  if lights then
	rs.setOutput("right", true)
  elseif not lights then
	rs.setOutput("right", false)
  end
end

EDIT: Hmm. Just noticed that with the font code uses L and 1 look the exact same.
Edited on 06 May 2014 - 09:24 PM
Bomb Bloke #2
Posted 06 May 2014 - 11:43 PM
That code shouldn't have that problem. What version of CC are we talking about?

Given that you're saying it's only happened once, it might be worth mentioning that sometimes rednet messages sent to a given system won't be received for no good reason. It doesn't really seem to matter here, but in cases where a script absolutely hinges on messages getting through, you might consider making use of timeouts to resend messages if the correct replies don't come back.

The only real improvement I can see would be to turn this sort of thing:

  if lights then
        rs.setOutput("right", true)
  elseif not lights then
        rs.setOutput("right", false)
  end

… into this:

  rs.setOutput("right", lights)

Edit: Well I suppose you could do away with "lights" altogether.

  if message == "l1" or message == "l2" then rs.setOutput("right", message == "l1") end
Edited on 06 May 2014 - 09:48 PM
applesauce10189 #3
Posted 09 May 2014 - 09:33 AM
That code shouldn't have that problem. What version of CC are we talking about?
1.61
but in cases where a script absolutely hinges on messages getting through, you might consider making use of timeouts to resend messages if the correct replies don't come back.
Actually I already do use timeouts, you see, the only reason I actually noticed the program bugged out was because I have one central computer that sends out checks and will wait a few seconds and if it doesn't get a certain reply it prints on a monitor that that program is offline.
The only real improvement I can see would be to turn this sort of thing:
if lights then
rs.setOutput("right", true)
elseif not lights then
rs.setOutput("right", false)
end

… into this:

rs
.setOutput("right", lights)
Oh well, thanks for the help though! By the way, sorry for the 3 day late reply xD various things distracted me.

EDIT: Jesus christ. Something about the way I did this just flat out doesn't like me…
Edited on 09 May 2014 - 07:34 AM