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

How do you 'tell' a turtle to run a program with the parameters?

Started by Rocko_Dan, 18 January 2016 - 12:12 PM
Rocko_Dan #1
Posted 18 January 2016 - 01:12 PM
Hello experts, lately I have downloaded a program witch lets you remotely control a turtle. I have made a few tweaks to it and I've been trying to make the turtle excavate over wireless. 'Telling' the Turtle to run the program is easy, but it requires parameters. I've tried sending over a variable, but I don't think it's working, I'm not really sure how to send a variable actually but here's my code:

Computer:

function RCmaster()
while true do
local sEvent, param = os.pullEvent("key")
if(sEvent == "key") then
if(param == 200) then
rednet.broadcast("TS Forward")
elseif (param == 57) then
rednet.broadcast("TS Up")
elseif (param == 42) then
rednet.broadcast ("TS Down")
elseif (param == 208) then
rednet.broadcast("TS Backward")
elseif (param == 203) then
rednet.broadcast("TS TurnLeft")
elseif (param == 205) then
rednet.broadcast("TS TurnRight")
elseif (param == 28) then
rednet.broadcast("TS PlaceBlock")
elseif (param == 18) then
textutils.slowPrint ("Excavation size?")
local ExcavateSize = read()
rednet.broadcast ("TS Excavate (ExcavateSize)")
end
end
end
end
rednet.open("back")
term.clear()
textutils.slowPrint("TurtleControl Initiated.")
print ("Use arrow keys, space and Lshift to move and Enter to place blocks.")
RCmaster()

Turtle:

function RCslave()
while true do
local scrap, message = rednet.receive()
if message == "TS Forward" then
print("Forward")
if turtle.detect() == true then
turtle.dig()
end
turtle.forward()
elseif message == "TS Backward" then
print("Backward")
turtle.back()
elseif message == "TS TurnLeft" then
print("Turn Left")
turtle.turnLeft()
elseif message == "TS TurnRight" then
print("Turn Right")
turtle.turnRight()
elseif message == "TS Up" then
print ("Upward")
turtle.up()
elseif message == "TS Down" then
print ("Downward")
turtle.down()
elseif message == "TS Excavate (ExcavateSize)" then
print ("Excavate (ExcavateSize)")
shell.run ("excavate")
elseif message == "TS PlaceBlock" then
if turtle.detect() == true then
print("Block Present")
else
print("Place Block")
turtle.place()
end
end
end
end
rednet.open("right")
textutils.slowPrint("TurtleReceive Initiated.")
RCslave()

Just to make sure I'm not taking all the credit, I downloaded code from here: http://www.computercraft.info/forums2/index.php?/topic/1373-remote-control-turtle-program/
I did make some tweeks like I said. Please help me! I'm quiet new to this thing! I'm very desparate. Please respond, thank you!
valithor #2
Posted 18 January 2016 - 04:59 PM
There are two practical ways to do what you are wanting. The first is using a string, and using the string api to get what you want out of it (requires more work, but not as much work to figure it out), and the other is using tables (much easier to use once you have a basic understanding of tables).

String api example:

For computer:

local ExcavateSize = read() --# getting user input for ExcavateSize
rednet.broadcast("TS Excavate "..ExcavateSize) --# the ".." is the concatination operator, which is just a long way of saying put these two things together into one string

For Turtle:

local scrap, message = rednet.receive() --# getting the message sent by computer
--# string.match is a function, which will search the first argument for whatever you put as the second, and if it is in the string then it will return what it found
if string.match(message,"TS Excavate") then --# is it a excavate message?
  local size = string.sub(message,12,#message) --# getting the end half of the message
  size = tonumber(size) --# converting size to a number instead of a string
  shell.run("excavate",size) --# any argument after the first is passed as an argument to the program
end

table example:

computer:


local ExcavateSize = read()
rednet.broadcast({["TS Excavate"] = ExcavateSize}) --# sending a table with the key as TS Excavate, and the value as ExcavateSize

turtle:

local scrap, message = rednet.receive()
if type(message) == "table" and message["TS Excavate"] ~= nil then --# checking to make sure the message is a table, and that it has a key named TS Excavate
  size = tonumber(message["TS Excavate"]) --# converting it to a number
  shell.run("excavate",size) --# running the program with the arg
end
Edited on 18 January 2016 - 04:00 PM