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

[help] rednet, turtles

Started by cheekycharlie101, 18 September 2012 - 05:06 PM
cheekycharlie101 #1
Posted 18 September 2012 - 07:06 PM
ok, so i have been working on making some programs with arguments. i finally got round it with the help from the awesome people on the forums and experimenting my self. anyway now i wanna know how to turn a rednet argument for a program. for instance i got this code:

local params={...}
local distance=tonumber(  params[1] )
for i = 1, distance do
  turtle.forward()
end
so basically you would type programname(5) and the turtle would move forward 5 blocks. but how can i send a message to the turtle and use the message as an argument. so i send a message to the turtle. the message is '6' and it makes the turtle move forward 6 blocks.

i have a basic rednet listener here :

rednet.open("top")
term.clear()
term.setCursorPos(1,1)
while true do
id, msg = rednet.receive()
if msg == "test" then
print("received")
end
end

any help would be appreciated ! :)/>/> thanks -Cheeky
Cranium #2
Posted 18 September 2012 - 07:32 PM
Turtle:

while true do
local id,msg,dist = rednet.receive()
turtle.forward(tonumber(msg))
end
Terminal:

rednet.open("top")
while true do
term.clear()
term.setCursorPos(1,1)
print("How far do you want the turtle to go?")
write("distance: ")
local input = tonumber(read())
rednet.send(turtleid,input)
end
Basic setup, but I'm sure you're smart enough to expand upon it.
Cranium #3
Posted 18 September 2012 - 07:47 PM
Changed the code a little. Couldn't help myself. :)/>/>

Turtle:

function forward(distance)
for i = 1,distance do
if not turtle.forward() then
turtle.dig()
turtle.forward()
else turtle.forward()
end
end
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
end
end

Terminal:

rednet.open("top")
while true do
term.clear()
term.setCursorPos(1,1)
print("What direction do you want to go?")
write("direction: ")
local command = read()
term.clear()
term.setCursorPos(1,1)
print("How far do you want the turtle to go?")
write("distance: ")
local distance= tonumber(read())
rednet.send(turtleid,command..","..distance)
end

This should be a little more advanced, and you can expand upon it by adding other directions.
cheekycharlie101 #4
Posted 18 September 2012 - 08:15 PM
Changed the code a little. Couldn't help myself. :)/>/>

Turtle:

function forward(distance)
for i = 1,distance do
if not turtle.forward() then
turtle.dig()
turtle.forward()
else turtle.forward()
end
end
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
end
end

Terminal:

rednet.open("top")
while true do
term.clear()
term.setCursorPos(1,1)
print("What direction do you want to go?")
write("direction: ")
local command = read()
term.clear()
term.setCursorPos(1,1)
print("How far do you want the turtle to go?")
write("distance: ")
local distance= tonumber(read())
rednet.send(turtleid,command..","..distance)
end

This should be a little more advanced, and you can expand upon it by adding other directions.
thanks for the help, just one question. isent there a string that you can use so like. you can broadcast the rednet messsage. then the turtle would check the id of the computer and only if the id was correct it would execute the commadns? could you show me this ?:D/>/>
Cranium #5
Posted 18 September 2012 - 08:28 PM
Yeah, it's simple. Just add this:

if id == correct id here then
--do your stuff here
end
You are just comparing the correct ID to what you have in the code.
cheekycharlie101 #6
Posted 18 September 2012 - 09:07 PM
Yeah, it's simple. Just add this:

if id == correct id here then
--do your stuff here
end
You are just comparing the correct ID to what you have in the code.

ok one last question. people put spaces in there code. i dont know how that works or where to put spaces. a example would be your signiture.
you have spaces. i dont know where to put them though? could you help me out ? thanks for all the help :)/>/>
-Cheeky
Cranium #7
Posted 18 September 2012 - 09:19 PM
Spaces can be anywhere. Lua does not care wheter you format it so it is readable. In fact, you can take out all unnecessary spacing and indentation, and make the program that would normally be 2500 lines into one giant line. This is not ideal by any means, and is hell for debugging. I like to put a line break(enter) everytime I start a newcommand, and I like to indent everytime I start a new function or operation. For example:

term.setCursorPos(1,1) print("This is just the same")
--and
term.setCursorPos(1,1)
print("as this command.")
and

while true do
  --add spaces here to separate from the while loop, easier to read
end --remove spaces to know which end correlates to which statement
--This is just easier to read, but you can put the end anywhere
--at the end of your statement
Also:

if value == equation then doStuff() end
--This works just fine, and is generally accepted due to the short length of this statement.
--If it is a very long statement, try to space it out.
--It's also good practice to put blank line breaks between functions,
--so it is easier to read. Not super necessary, but it's helpful.
cheekycharlie101 #8
Posted 18 September 2012 - 09:43 PM
Spaces can be anywhere. Lua does not care wheter you format it so it is readable. In fact, you can take out all unnecessary spacing and indentation, and make the program that would normally be 2500 lines into one giant line. This is not ideal by any means, and is hell for debugging. I like to put a line break(enter) everytime I start a newcommand, and I like to indent everytime I start a new function or operation. For example:

term.setCursorPos(1,1) print("This is just the same")
--and
term.setCursorPos(1,1)
print("as this command.")
and

while true do
  --add spaces here to separate from the while loop, easier to read
end --remove spaces to know which end correlates to which statement
--This is just easier to read, but you can put the end anywhere
--at the end of your statement
Also:

if value == equation then doStuff() end
--This works just fine, and is generally accepted due to the short length of this statement.
--If it is a very long statement, try to space it out.
--It's also good practice to put blank line breaks between functions,
--so it is easier to read. Not super necessary, but it's helpful.
thanks for all the help :)/>/>. but i tried adding a new direction to the above code. i basicly copied it but changed the function to backward and the if command == "backward" but when i tried to run it it did not do anything. could you show me the right way of adding directions. if you do that i should be able to work out up down left and right my self. Thanks again :D/>/> -Cheeky
Cranium #9
Posted 18 September 2012 - 11:00 PM

function forward(distance)
for i = 1,distance do
if not turtle.forward() then
turtle.dig()
turtle.forward()
else turtle.forward()
end
end
end
function back(distance)
for i = 1,distance do
turtle.back()
end
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
elseif command == "back" then
back(tonumber(dist))
--add other elseifs here
end
end
I'm sorry I can't test this, but I think this should do for your turtle. Otherwise, I need to get home and do some testing. :)/>/>
cheekycharlie101 #10
Posted 19 September 2012 - 05:47 PM

function forward(distance)
for i = 1,distance do
if not turtle.forward() then
turtle.dig()
turtle.forward()
else turtle.forward()
end
end
end
function back(distance)
for i = 1,distance do
turtle.back()
end
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
elseif command == "back" then
back(tonumber(dist))
--add other elseifs here
end
end
I'm sorry I can't test this, but I think this should do for your turtle. Otherwise, I need to get home and do some testing. :)/>/>
most of it worked like a charm. i had to remove and add a couple of things though. i had to add rednet.open("right") and had to remove
else turtle.forward() and if not turtle.forward() then
because the turtle was going twice as many blocks as i told it to. but removing these fixed it. anyway, one last question. i want to make a function that will break the block in front of the turtle above and below it. but it wont work. i have up/down left/right and forward/backward all working great. but i really need to know how to make a function that mines the blocks. i added the elseif statement and i made the function correctly using turtle.dig() turtle.digUp() and turtle.digDown() with a sleep(1.5) between each one. i dident use a 'for loop' because i dident see the need for it. anyway please could you help me out on that :D/>/> thanks -
-Cheeky
Cranium #11
Posted 19 September 2012 - 06:21 PM
I'd love to help! Since you said you made some changes, would you mind posting your new code? I'd like to work with what you have, instead of you having to go back and change what I give you again.
cheekycharlie101 #12
Posted 19 September 2012 - 08:39 PM
I'd love to help! Since you said you made some changes, would you mind posting your new code? I'd like to work with what you have, instead of you having to go back and change what I give you again.
sure this is my code so far. if your wondering what payload does its a thing where put tnt in slot 9 and then it will drop the ammount of tnt that is put in the distance space. also i understand what things like functions and for loops so i can usally understand the code that people give me as samples.

note this is the code for the turtle only. i did not make changes to the client code.

rednet.open("right")
function forward(distance)
for i = 1,distance do
turtle.forward()
end
end
function back(distance)
for i = 1,distance do
turtle.back()
end
end
function mine()
turtle.dig()
sleep(1.5)
turtle.digUp()
sleep(1.5)
turtle.digDown()
end
function up(distance)
for i = 1,distance do
turtle.up()
end
end
function left(distance)
turtle.turnLeft()
for i = 1,distance do
turtle.forward()
end
turtle.turnRight()
end
function down(distance)
for i = 1,distance do
turtle.down()
end
end
function right(distance)
turtle.turnRight()
for i = 1,distance do
turtle.forward()
end
turtle.turnLeft()
end
function payload(distance)
rs.setOutput("bottom", true)
turtle.select(9)
for i = 1,distance do
turtle.placeDown()
end
rs.setOutput("bottom", false)
turtle.select(1)
end
while true do
local id,msg,dist = rednet.receive()
local command,dist = string.match(msg, "(%w+),(%w+)")
if command == "forward" then
forward(tonumber(dist))
elseif command == "back" then
back(tonumber(dist))
elseif command == "dig" then
mine(tonumber(dist))
elseif command == "up" then
up(tonumber(dist))
elseif command == "down" then
down(tonumber(dist))
elseif command == "left" then
left(tonumber(dist))
elseif command == "right" then
right(tonumber(dist))
elseif command == "payload" then
payload(tonumber(dist))
end
end
Cranium #13
Posted 19 September 2012 - 09:17 PM
Hmm… I don't see any problems with your code. You say it is not digging at all?
cheekycharlie101 #14
Posted 19 September 2012 - 09:29 PM
Hmm… I don't see any problems with your code. You say it is not digging at all?
just figured it out:P it was trying to dig in an unprotected area. sorry for the noobie question but thanks for all your help :)/>/>. but there is one problem. if i make it drop 10 peices of tnt it will only drop 9. this is the same for all numbers. only if i put 1 in it seems to work. could you look into it for me :D/>/> thanks
-Cheeky
Cranium #15
Posted 19 September 2012 - 09:39 PM
You may want to put a sleep(1) or sleep(.5) after your turtle.placeDown() function. Other than that, it should work.
cheekycharlie101 #16
Posted 19 September 2012 - 09:44 PM
You may want to put a sleep(1) or sleep(.5) after your turtle.placeDown() function. Other than that, it should work.
Thanks sooo much. im starting to love lua and im getting more advanced with it. im glad there is a awesome forum here with awesome people to help me out :)/>/>
Cranium #17
Posted 19 September 2012 - 10:00 PM
Just keep working on it! I can tell you are learning fast, and you might want to look at some of the Lua sites out there. It might give you some ideas. If all else fails, feel free to come back to our warm, knowledgable embrace and ask away. There are no "stupid" questions here…(well, maybe some of them).
cheekycharlie101 #18
Posted 19 September 2012 - 10:05 PM
Just keep working on it! I can tell you are learning fast, and you might want to look at some of the Lua sites out there. It might give you some ideas. If all else fails, feel free to come back to our warm, knowledgable embrace and ask away. There are no "stupid" questions here…(well, maybe some of them).
Thanks Do you recommend any lua sites? note that i have basicly taught my self with the help of you guys so nothing to advanced :)/>/> any sites would be appreciated though thanks -Cheeky
Cranium #19
Posted 19 September 2012 - 10:14 PM
Lua-Users Wiki
Lua main site
I use both of these sites regularly when I want to learn something that is more Lua-based rather than ComputerCraft-based.
cheekycharlie101 #20
Posted 20 September 2012 - 05:25 PM
Lua-Users Wiki
Lua main site
I use both of these sites regularly when I want to learn something that is more Lua-based rather than ComputerCraft-based.
Thanks, just one question. i only ever use lua in computercraft. so my question is. how different is lua from computercraft?
Cranium #21
Posted 20 September 2012 - 05:29 PM
Lua is the same. As far as I know, any functions in Lua can be used in ComputerCraft. ComputerCraft uses LuaJ which is made for use within Java. I have yet to find any function that is on those sites that cannot be used within ComputerCraft.