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

How can I get rednet.receive(x) to pause the program until a timeout/receive?

Started by evanbrown24, 20 February 2013 - 03:55 PM
evanbrown24 #1
Posted 20 February 2013 - 04:55 PM
Title: How can I get rednet.receive(x) to pause the program until a timeout/receive?

I need my app to confirm its rednet connection by sending a rednet string to the server, and for the server to respond with another word, so that it knows it's working. It checks if it receives by checking if the variable "message" returned by rednet.receive() is nil or the string. However, it begins to check this before the server even has a chance to respond. I know the signal is getting to the "ping" server, because it prints on the screen a notification that it's receiving and sending the signal.
OmegaVest #2
Posted 21 February 2013 - 04:40 AM
Well, I would use a pullEvent loop instead of rednet.receive.

Something like this,

timr = os.startTimer(x)  --  Set x to however long you want it to check for.
while true do
   evt, id, msg = os.pullEvent()
   if evt == "rednet_message" then
	  if id == server then  -- The numerical id for the server computer
		 -- Confirmed Ping
	  else
		 -- Still waiting for confirmation
   elseif evt == "timer" and id == timr then
	  -- Confirmation failed
	  break
   end
end


My assumption is that you are trying to filter out certain other messages. This will help that.
Lyqyd #3
Posted 21 February 2013 - 06:36 AM
I think you underestimate how fast rednet is.
AnDwHaT5 #4
Posted 22 February 2013 - 12:11 PM
If it is a time issue try a repeat and until like if you are sending it a message then

rednet.open("top")
repeat
id, message = rednet.recieve()
if message == "something" then
rednet.send("id", "i got your message")
else
shell.run("program")
until message == "something"




there was something else i had but i forgot while making the code :P/>
immibis #5
Posted 22 February 2013 - 09:16 PM
If it is a time issue try a repeat and until like if you are sending it a message then

rednet.open("top")
repeat
id, message = rednet.recieve()
if message == "something" then
rednet.send("id", "i got your message")
else
shell.run("program")
until message == "something"




there was something else i had but i forgot while making the code :P/>/>
Maybe the thing you forgot was that running the program inside itself is stupid and pointless and leads to stack overflows?
Btw your code doesn't even compile, it's missing an end.

OP: You're supposed to start waiting for messages, so if you're doing that, it's not wrong. If you weren't waiting for messages when the server sent the message, then how would the client know it's interested in that message? Pastebin your code.