p.s Please try to make it easy to understand from a noobs point of view :)/>/>
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Rednet Help
Started by xWoody25, 15 April 2012 - 03:32 AMPosted 15 April 2012 - 05:32 AM
Hi i am trying to make a program that allows me send a command to a wireless mining turtle from a computer, i am fairly new to the mod, i have only been using it for about 5 days and finding the redset command/program very hard, i'm fed up of making basic password doors and want a bigger challenge, thank you all very much.
p.s Please try to make it easy to understand from a noobs point of view :)/>/>
p.s Please try to make it easy to understand from a noobs point of view :)/>/>
Posted 15 April 2012 - 06:09 AM
Here's a simple example that will move a turtle forward 1 space. It should give you the general idea and you can go from there.
turtle
Computer
Havent tried it but it should work.
turtle
rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message") -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end -- end the if
end -- end the loop
Computer
rednet.open("side") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send("turtleID", "forward") -- change turtleID to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end
Havent tried it but it should work.
Posted 15 April 2012 - 04:07 PM
I have put the code in exactly how you have done it, and when i press the key to send the command is says "Positive Number expected", does this mean i have to put the key id for the up arrow on the keyboardHere's a simple example that will move a turtle forward 1 space. It should give you the general idea and you can go from there.
turtlerednet.open("right") -- turn on the turtles modem while true do -- infinite loop to keep the turtle "listening" for commands local event, id, msg = os.pullEvent("rednet_message") -- wait for a rednet message event if msg == "forward" then -- check what the message/command was turtle.forward() -- execute what the command was ordering done end -- end the if end -- end the loop
Computerrednet.open("side") -- change to the side your modem is on shell.run("clear") while true do print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff os.pullEvent("key") -- wait for a key event rednet.send("turtleID", "forward") -- change turtleID to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening end
Havent tried it but it should work.
Posted 15 April 2012 - 05:43 PM
No. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
Posted 15 April 2012 - 05:45 PM
i have done that, do i need to include the '#' symbol?No. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
Posted 15 April 2012 - 05:47 PM
i have done that with "" but it still comes up with that error, do i need to include the # in the ID of the turtle aswellNo. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
Posted 15 April 2012 - 06:11 PM
right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>
Posted 15 April 2012 - 07:40 PM
Computer (with rednet modem on RIGHT):
print("Type 'forward' to move turtle forward, 'back' to move turtle back, etc.")
while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
write("Command: ")
input = read()
rednet.broadcast(input)
end
Turtle (WIRELESS turtle. mining turtle):
while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
print("Listening on port 80...")
local id, msg, distance = rednet.receive()
print("We've got a message from computer ID #"..id.."!")
print("The message is:")
print(msg)
print("This message was received from "..distance.." blocks away!")
if msg == "forward" then
turtle.forward()
elseif msg == "back" then
turtle.back()
elseif msg == "dig" then
turtle.dig()
elseif msg == "right" then
turtle.turnRight()
elseif msg == "left" then
turtle.turnLeft()
elseif msg == "down" then
turtle.down()
elseif msg == "up" then
turtle.up()
elseif msg == "exit" then
error()
end
Posted 15 April 2012 - 07:54 PM
Thanks but can you simplifie it a bit :)/>/>Computer (with rednet modem on RIGHT):Turtle (WIRELESS turtle. mining turtle):print("Type 'forward' to move turtle forward, 'back' to move turtle back, etc.") while true do sleep(0.1) term.clear() term.setCursorPos(1,1) write("Command: ") input = read() rednet.broadcast(input) end
while true do sleep(0.1) term.clear() term.setCursorPos(1,1) print("Listening on port 80...") local id, msg, distance = rednet.receive() print("We've got a message from computer ID #"..id.."!") print("The message is:") print(msg) print("This message was received from "..distance.." blocks away!") if msg == "forward" then turtle.forward() elseif msg == "back" then turtle.back() elseif msg == "dig" then turtle.dig() elseif msg == "right" then turtle.turnRight() elseif msg == "left" then turtle.turnLeft() elseif msg == "down" then turtle.down() elseif msg == "up" then turtle.up() elseif msg == "exit" then error() end
Posted 15 April 2012 - 10:36 PM
right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>
The code I posted should work as long as your putting in the turtle id as say 1 not "1" or #1. Also make sure you are changing the side of the modem on the computer script to the side that your modem is on. Make sure the red bands are on all of the modems.
Sorry for the errors yesterday like I said I couldn't test it. I have now and it works. I put what I used in the spoiler.
Spoiler
Computer
rednet.open("right") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send( 19, "forward") -- change 19 to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end
Turtle
rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message") -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end -- end the if
end -- end the loop
Posted 16 April 2012 - 12:24 AM
right i have checked over my code, it is identical to yours (except the bits that are not meant to be like the turtle ID but every time i press any key the text "press any key . ect" keeps popping up and the turtle does not move) i have made sure that the red band has lit up on both the computer and turtle but the turtle doesnt move, i dont have a clue :S thanks for the help by the way, i would show you my code but its on a online server, if you tell me how to copy it that would be great (do i just have to type it manually?)right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>
The code I posted should work as long as your putting in the turtle id as say 1 not "1" or #1. Also make sure you are changing the side of the modem on the computer script to the side that your modem is on. Make sure the red bands are on all of the modems.
Sorry for the errors yesterday like I said I couldn't test it. I have now and it works. I put what I used in the spoiler.Spoiler
Computerrednet.open("right") -- change to the side your modem is on shell.run("clear") while true do print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff os.pullEvent("key") -- wait for a key event rednet.send( 19, "forward") -- change 19 to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening end
Turtlerednet.open("right") -- turn on the turtles modem while true do -- infinite loop to keep the turtle "listening" for commands local event, id, msg = os.pullEvent("rednet_message") -- wait for a rednet message event if msg == "forward" then -- check what the message/command was turtle.forward() -- execute what the command was ordering done end -- end the if end -- end the loop
Posted 16 April 2012 - 01:09 AM
i have fixed the code so it works, now i want to make it so when i press a different arrow it will go forward, back, left and right plz can you post an easy example
Posted 16 April 2012 - 01:12 AM
How is that not simple? Its as simple as simple can be.Thanks but can you simplifie it a bit :)/>/>
Posted 16 April 2012 - 01:58 AM
i have fixed the code, please read my last post :)/>/>How is that not simple? Its as simple as simple can be.Thanks but can you simplifie it a bit :)/>/>
Posted 16 April 2012 - 08:09 AM
You will want to use a "key" event and then send the appropriate command to the turtle for the key that is pressed.
while true do
local evt, key = os.pullEvent("key")
if key == 200 then -- up arrow pressed
rednet.send(id, "command")
elseif key == 208 then -- down arrow pressed
rednet.send(id, "command")
elseif key == 205 then -- right arrow
rednet.send(id, "command")
elseif key == 203 then -- left arrow
rednet.send(id, "command")
end
Posted 22 October 2012 - 03:39 AM
I used Lua Nub's code, but whenever I type in the command on the computer it says:
turtle:5: attempt to call nil
What is wrong, and how can I fix it?
turtle:5: attempt to call nil
What is wrong, and how can I fix it?
Posted 22 October 2012 - 03:53 AM
His line five doesn't call anything, so obviously, you're not using his code. Post the complete code that you are using.
Posted 23 October 2012 - 01:09 AM
rednet.open("Right")
shell.run("clear")
while true do
print ("Press Any Key to Send the Command")
os.pullEvent("key")
rednet.send("9", "forward")
end
This is the code I used
shell.run("clear")
while true do
print ("Press Any Key to Send the Command")
os.pullEvent("key")
rednet.send("9", "forward")
end
This is the code I used
Posted 23 October 2012 - 01:16 AM
And that code doesn't have any problems on line five, unless you had overriden os.pullEvent() elsewhere. Are you retyping the code or copying and pasting it? We need the exact code that you're using, not retyped code.
Posted 23 October 2012 - 01:16 AM
Okay, there are some problems. You didn't post the entire error message, though. When you use a function from an API, the error message includes the name and line of the API where the error actually occurred. I'm guessing the problem was from the rednet API, because your rednet.send("9", "forward") call is borked. It should be rednet.send(9, "forward"). I'm also not sure that rednet.open("Right") works the same as rednet.open("right").
Posted 23 October 2012 - 01:27 AM
First, not sure how to copy and paste yet, yes I am a little new, but i did copy what i wrote word for word, and i did change the Right to right, but i get the same message:
Press any key to send the command
turtle:5: attempt to call nil
Press any key to send the command
turtle:5: attempt to call nil
Posted 23 October 2012 - 01:35 AM
Go to your saves directory and look in worldname/computer/ID (where worldname is the name of the world and ID is the ID of the computer you stored this program on). Open the file with notepad or any other text editor, then copy and paste the entire thing. But if that error message is right, you're using an API or something that has borked os.pullEvent().
Posted 23 October 2012 - 01:52 AM
rednet.open("right")
shell.run("clear")
while true do
print ("Press any key to send the command")
os.pullevent("key")
rednet.send(9,"forward")
end
shell.run("clear")
while true do
print ("Press any key to send the command")
os.pullevent("key")
rednet.send(9,"forward")
end
Posted 23 October 2012 - 02:03 AM
There's the problem. Line five should have a capital E. os.pullEvent(), not os.pullevent().
Posted 23 October 2012 - 02:03 AM
os.pullevent("key") should be os.pullEvent("key"). But that's good news, because it means that your API isn't borked up.
^_^/>/>
^_^/>/>
Edited on 23 October 2012 - 12:04 AM
Posted 23 October 2012 - 03:02 AM
Thank you!!!!
Posted 22 November 2013 - 11:03 PM
The code everyone has is good, but does anyone have some code that's not a giant if loop? eg
Thanks!
if message == "command"
command
Does anyone have some that is similar to a peripheral.wrap() but for turtles? So that you could go turtleName.command()Thanks!