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

help with rednet please?

Started by samdeman22, 29 March 2012 - 08:20 PM
samdeman22 #1
Posted 29 March 2012 - 10:20 PM
ok, I am trying to make a program that sends a message back when it receives a message called mobile (as a test), and yes i have connected them both, and the modems are opened, my code for the first receiver is as follows:
———————————————————————
sender:

rednet.send ( 3, "hi" ) rednet.receive(timeout)
–I just did this in the lua console–
———————————————————————
first receiver:

rednet.receive(timeout)
if rednet.receive == "hi" then
rednet.send( 2, "hi to you too" )
end
———————————————————————
this should then send a message back to the sender, right?
I come back to the receiver and the console is blank, it just says "> mobile" (because that's what the program is called)
any help, please? I would also like some help into writing all the rednet stuff.
Luanub #2
Posted 29 March 2012 - 10:51 PM
Are you running the receiver from a script of the lua shell? You probably need to put it in a script and add some variables.

The receiver should probably look something like…

local id, msg = rednet.receive()
if msg == "hi" then
rednet.send( 2, "hi to you too" )  -- could also replace the 2 with the id var and send the message back to whatever computer sent it rather then 2 everytime.
end

rednet.receive() will pick up two bits of information the 1st being the sending computers ID, and the second being the message. You can see how I captured that information in two seperate vars id and msg.
samdeman22 #3
Posted 30 March 2012 - 08:50 PM
Are you running the receiver from a script of the lua shell? You probably need to put it in a script and add some variables.

The receiver should probably look something like…

local id, msg = rednet.receive()
if msg == "hi" then
rednet.send( 2, "hi to you too" )  -- could also replace the 2 with the id var and send the message back to whatever computer sent it rather then 2 everytime.
end

rednet.receive() will pick up two bits of information the 1st being the sending computers ID, and the second being the message. You can see how I captured that information in two seperate vars id and msg.

errm, still does nothing :o/>/>
what would you write if you were making a program like this?
Luanub #4
Posted 30 March 2012 - 10:21 PM
I personally would use an event vs the rednet.receive()



local evt, id, msg = os.pullEvent("rednet_message")
if msg == "hi" then
rednet.send( 2, "hi to you too" )
end


Try doing this then send a broadcast out from the sender and make sure the receiver is picking it up


while true do
local evt, id, msg = os.pullEvent("rednet_message")
print (evt)
print (id)
print (msg)
end

If not make sure the two computers are in range of each other. Unless you have modified the config file I believe the default range is 60 blocks in good weather and only 15 or something like that if it is storming. Also make sure the modems are enabled. Should have a red ring around them when they are on. if not do rednet.open("back") to open a modem on the back.