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

Wireless Modems and listening

Started by Dmac9244, 01 July 2014 - 10:37 PM
Dmac9244 #1
Posted 02 July 2014 - 12:37 AM
Is there any way I can make a computer wait for a response from another computer? I know how to send a bunch of information from one computer to another, but what I want is a computer listening for a certain phrase to do something. Can I do this?
NanoBob #2
Posted 02 July 2014 - 11:14 AM
to have a computer receive a message you should do:

id,message=rednet.receive()
Dmac9244 #3
Posted 03 July 2014 - 03:50 PM
That is for rednet though, right? I'm talking about wireless modems, if it make any difference. Sorry if it does make a really big difference.
Lyqyd #4
Posted 03 July 2014 - 03:51 PM
The rednet API uses wireless and wired modems.
Dmac9244 #5
Posted 03 July 2014 - 06:34 PM
Ok… Thanks, I guess… but will the line for receiving messages in the modem API wait for a message?
NanoBob #6
Posted 03 July 2014 - 06:39 PM
Yes, it will wait untill it receives a message, however you could specify a timeout as first argument
for example:

id,message=rednet.receive(1)
This would wait for a message for 1 second, if there is none both id and message will be nil and it will move on.
TheOddByte #7
Posted 03 July 2014 - 09:08 PM
You also want to make sure your modem is open, otherwise it would be useless to wait for a message :P/>

rednet.open( "<SIDE>" )
and then to listen( as said above )

local id, message = rednet.receive()

Now you can check what the variable 'message' was equal to

if message == "Hello" then
    print( "Hello!" )
end