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

How to send programs to turtles with rednet

Started by Joe3000, 16 August 2012 - 05:07 AM
Joe3000 #1
Posted 16 August 2012 - 07:07 AM
Ok, first off, im new to this, im a total noob, and i want to learn how to do this, so far what i know makes this mod really fun. Back to my question, I am trying to be able to control my turtle using rednet and a computer. What I thought of is something along the lines of this just to make the turtle go forward to see if it would work

Turtle:
rednet.recieve()
if message == "turtle forward" then
turtle.forward()
else
end

Again, I am a total noob and this seems way to easy to work… I cant get it to work but how would I?

Also a secondary question, where can i learn some more of the commands? I just learned "for n=… do" and i finally found out "While not ….. do" but where can I learn more?
Luanub #2
Posted 16 August 2012 - 07:21 AM
Try doing something like this

Computer

rednet.send(turtleid, "forward")

Turtle

local id, msg = rednet.receive() -- need to capture the rednet.receive() output in vars
if msg == "forward" then
  turtle.forward()
end
Joe3000 #3
Posted 16 August 2012 - 07:24 AM
I still get an attempt to call nill error
Kazimir #4
Posted 16 August 2012 - 07:26 AM
Read the wiki and tutorials on forum, there is everything necessary for a beginner


id, message = rednet.recieve()
if message == "turtle forward" then
turtle.forward()
end
BigSHinyToys #5
Posted 16 August 2012 - 07:26 AM
replace turtleid with a number the Turtles ID you can get this by running "ID" program
KaoS #6
Posted 16 August 2012 - 07:41 AM
have you even opened a modem?
Joe3000 #7
Posted 16 August 2012 - 07:46 AM
yes i have opened the modem and i have replaced turtleid with the turtles id…. still doesnt work….
Luanub #8
Posted 16 August 2012 - 07:49 AM
Do this to check and make sure the turtle is receiving the message


local id, msg = rednet.receive()
print("ID is "..id)
print("Message is "..msg)

If you could post the current code you are using and I'll take a look at it for errors.
KaoS #9
Posted 16 August 2012 - 07:50 AM
and you could just use loadstring, eg:

CLIENT
Spoiler

rednet.open(side)
local id,msg,dist=rednet.receive()
loadstring(msg)

That way it will just treat anything as a command and try to execute it, this could be a problem as you would then be executing broadcasted messages too so instead you tell it to unserialize the message and then check if the first value is a certain keyword, if so then it executes the second value. now you send it serialized tables from the host PC and you're sorted, this way you can execute almost any lua script (a few don't work, I think shell.run() is one of them)
Pharap #10
Posted 16 August 2012 - 09:25 AM
Ok, enough confusion:

Turtle(must be activated before computer)

rednet.open("right") --modem is always on the right side of the turtle
while true do --activates endless loop
local id, msg == os.pullEvent("rednet_message") -- when a message is received, these variables are written to
if msg == "forward" then -- test message
turtle.forward() --corresponding action
elseif msg = "end" then -- other option
break --breaks out of endless loop
end -- end if
end -- ends a cycle of the loop

Computer:

rednet.open("top") --assuming modem is on top
while true do
term.clear() -- clear screen
term.setCursorPos(1, 1) -- reset cursor
print("Enter a command")
local input = read()
if input == "forward" or input == "end" then --checks input is valid
rednet.send(turtleid, input) --sends input to turtle
--alternatively, if there are multiple turtles running the turtle program above, use rednet.broadcast(input) to reach them all easily
else --anything that isn't an aforementioned condition
print("Invalid command")
sleep(2)
end

There, now even the most beginnerist beginners should understand (I hope)