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

Help Rednet

Started by tbyoran, 27 November 2012 - 03:55 AM
tbyoran #1
Posted 27 November 2012 - 04:55 AM
Why this basic program not work?

Computer:

rs.setOutput("left",true)
rednet.open("right")
write "Message: "
input = read()
rednet.broadcast(input)

Turtle:

term.clear()
print ("Write Turtle Commands From Pc")
sleep(0)
  action, senderID, text= os.pullEvent()
rednet.open("right")
if action == "rednet_message" then
  if text == "Forward" then
  turtle.forward()
  end
end
remiX #2
Posted 27 November 2012 - 05:11 AM
You need to use a while true do loop for the turtle so it doesn't just run once and the program stops. Also for os.pullEvent() returns [ event, senderID, message, dist ] you have it wrong there.
This should work:

rednet.open("right") -- Does not have to be in the loop because you only need to open it once

while true do
    term.clear()
    print ("Write Turtle Commands From Pc")
    sleep(0)
    ev, senderID, msg, dist = os.pullEvent("rednet_message")
    if msg == "Forward" then
        turtle.forward()
    end
end
bjornir90 #3
Posted 27 November 2012 - 05:12 AM
It's maybe because on the computer you type forward instead of Forward, Lua is cass sensitive.
Kingdaro #4
Posted 27 November 2012 - 05:15 AM
Rednet has to be opened before receiving rednet messages. Just switch the two lines around.


rednet.open("right")
action, senderID, text= os.pullEvent()

Also, you should probably use rednet.receive() instead of os.pullEvent(), because you only want to get rednet messages.


senderID, text = rednet.receive()
tbyoran #5
Posted 27 November 2012 - 05:16 AM
thx a lot