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

Sending program variables to turtles via computer?

Started by Diethro, 27 January 2013 - 10:12 PM
Diethro #1
Posted 27 January 2013 - 11:12 PM
Title: Sending program variables to turtles via computer?

Been doing some testing with turtles over the past few days, testing out the different commands, writing some minor programs and such. Got around to doing things with the wireless modem and I've hit a roadblock. I'm trying to set it up so that I can send the turtle a message from a source computer and it will run a program after receiving the message. Thing is, the program I want to run is the Excavate, which requires a variable for the size. My goal was to make it so that I could send:

rednet.send(turtleid, Excavate #)

I can confirm that its receiving transmissions successfully, but can't figure out how to transfer the number for the Excavate program. Any ideas?
Engineer #2
Posted 28 January 2013 - 08:38 AM
Of course you have to open rednet on both, but I think you already know that

Server:

  print("Whats the radius of excavate?")
  reader = read()
  if tonumber(reader) ~= nil then
	 rednet.send(ID, tostring(reader))
  else
	  print("Invalid radius")
   end

Client:

local p1, p2 = rednet.receive()
local input = ("excavate "..p2)
shell.run(input)

Dont want to be greedy, but it took some time to figure out the excavate..p3 thingie. Please 1 reputation :P/>
Orwell #3
Posted 28 January 2013 - 08:44 AM
Of course you have to open rednet on both, but I think you already know that

Server:

  print("Whats the radius of excavate?")
  reader = read()
  if tonumber(reader) ~= nil then
	 rednet.send(ID, tostring(reader))
  else
	  print("Invalid radius")
   end

Client:

local p1, p2 = rednet.receive()
local input = ("excavate "..p2)
shell.run(input)

Dont want to be greedy, but it took some time to figure out the excavate..p3 thingie. Please 1 reputation :P/>
That's too bad because you could just have done:

shell.run("excavate", p2)
Engineer #4
Posted 28 January 2013 - 09:01 AM
Of course you have to open rednet on both, but I think you already know that

Server:

  print("Whats the radius of excavate?")
  reader = read()
  if tonumber(reader) ~= nil then
	 rednet.send(ID, tostring(reader))
  else
	  print("Invalid radius")
   end

Client:

local p1, p2 = rednet.receive()
local input = ("excavate "..p2)
shell.run(input)

Dont want to be greedy, but it took some time to figure out the excavate..p3 thingie. Please 1 reputation :P/>
That's too bad because you could just have done:

shell.run("excavate", p2)

Dont we all learn a bit every day? Thank you
Diethro #5
Posted 28 January 2013 - 02:21 PM
Thank you both, that worked wonders. Now to just try and figure out a way to put the client side command into a program with other things to run. Would the following work?

Turtle program "Online"
local p1, p2 == rednet.receive()
while true do
  if p1 == Excavate then
	shell.run("excavate"..p2)
  end
end

Hopefully this would allow me to add additional if then lines in for situations where I want to make the turtle run, say, tunnel.
Orwell #6
Posted 28 January 2013 - 02:28 PM
Thank you both, that worked wonders. Now to just try and figure out a way to put the client side command into a program with other things to run. Would the following work?

Turtle program "Online"
local p1, p2 == rednet.receive()
while true do
  if p1 == Excavate then
	shell.run("excavate"..p2)
  end
end

Hopefully this would allow me to add additional if then lines in for situations where I want to make the turtle run, say, tunnel.
No. You'd want to open rednet first. So you'd have rednet.open("side") at the top. Also, Excavate in your code is a variable that doesn't exist. You want that to be "Excavate". And finally, if you keep that while loop, it will excavate the same area over and over again. You can put the while loop around everything though, so that it will listen on rednet again. So this is what your code becomes after these 3 improvements:

rednet.open("top") -- replace "top" with the side your modem is on
local bRunning = true
while bRunning do
  local p1, p2 == rednet.receive()
  local p3,p4 = string.gmatch(p2,"(%w):(/>%w)?)
  if p3 == "Excavate" then
	shell.run("excavate", p4)
  elseif p3 == "otherCommand" then
	-- shell.run("other", p4)
  elseif p3 == "exit" then
	bRunning = false
  end
end
rednet.close("top")
NeverCast #7
Posted 28 January 2013 - 02:33 PM
Wouldn't p1 be the id you received from?
O.o Or did I miss something?
Diethro #8
Posted 28 January 2013 - 02:36 PM
Thank you both, that worked wonders. Now to just try and figure out a way to put the client side command into a program with other things to run. Would the following work?

Turtle program "Online"
local p1, p2 == rednet.receive()
while true do
  if p1 == Excavate then
	shell.run("excavate"..p2)
  end
end

Hopefully this would allow me to add additional if then lines in for situations where I want to make the turtle run, say, tunnel.
No. You'd want to open rednet first. So you'd have rednet.open("side") at the top. Also, Excavate in your code is a variable that doesn't exist. You want that to be "Excavate". And finally, if you keep that while loop, it will excavate the same area over and over again. You can put the while loop around everything though, so that it will listen on rednet again. So this is what your code becomes after these 3 improvements:

rednet.open("top") -- replace "top" with the side your modem is on
local bRunning = true
while bRunning do
  local p1, p2 == rednet.receive()
  if p1 == "Excavate" then
	shell.run("excavate", p2)
  elseif p1 == "otherCommand" then
	-- shell.run("other", p2)
  elseif p1 == "exit" then
	bRunning = false
  end
end
rednet.close("top")

Ah cool, yeah I had the rednet open in the code already, just figured that was an easy way to turn the modem back on when I place the turtle back down. Never even occurred to me to use elseif, that should run just the way I was hoping it would. Thanks for all the help!
Orwell #9
Posted 28 January 2013 - 02:42 PM
Wouldn't p1 be the id you received from?
O.o Or did I miss something?
Hehe, don't be sarcastic, you know that I've made a typo. :P/> I'll edit it in my previous post.

Edit: oh, it wasn't a typo in fact. I copied Diethro's code too literally.
@Diethro: I edited the code. You should know that rednet.receive() returns id, message and distance.
In this case, you need to send the message in the following form: "program:parameter". With the column to divide the two. You can also make it accept multiple parameters, but I'm getting a tad tired, so I'll do that later if you need it.
NeverCast #10
Posted 28 January 2013 - 02:44 PM
I wasn't being sarcastic, it was an honest question. There was a possibility, although unlikely that you had overridden the rednet table with your own custom receive function :P/>
I don't assume, I ask xD