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

Remotely running commands on a turtle

Started by Zoinky, 31 August 2012 - 08:09 AM
Zoinky #1
Posted 31 August 2012 - 10:09 AM
Well, Firstly, Let me explain what I'm trying to do. I play on my friends server and since it's only hourly sometimes he turns in off and my mining turtle gets stuck down a area it has been excavating. So I went into single player and I've been trying to come up with some code that sends a command to the turtle via rednet. I had SOME luck but it's nothing practical (Eg. If I sent 'go forward 1' it would run turtle.forward(). But, that would work unless I typed every number that I needed. ). So I was hoping that someone would help me find some code or give me some advice on how to split up a rednet message and have the shell api run it. Lastly, If anyone gets the wrong idea and thinks that I'm just asking for code.. I am :)/>/> But, I have searched around for something to help. Sadly, I couldn't find anything that I could wrap my head around. Although I am fairly new to Lua.
KaoS #2
Posted 31 August 2012 - 10:17 AM
use the loadstring command

local tempfunction=loadstring("print('me')")
creates a function that will print 'me' so what you do is

local id,msg,dist=rednet.receive()
loadstring(msg)()
and it will execute anything it receives as code, you can then fiddle around to filter out spam etc but that is the basics, please note that because the shell commands are not in the apis folder they are not recognised by loadstring
Zoinky #3
Posted 31 August 2012 - 10:24 AM
Wow. Thanks alot B)/>/> Didn't realize it was that simple. I owe you one :)/>/>
Zoinky #4
Posted 31 August 2012 - 11:10 AM
Erm.. One problem. If I run 'go forward 1' I don't get anything. It just ends the program :S
KaoS #5
Posted 31 August 2012 - 11:13 AM
you use functions there, not programs so do

rednet.broadcast('turtle.up()')
and it will move up. give me a minute to work on something to run programs
BigSHinyToys #6
Posted 31 August 2012 - 11:16 AM
Try this
run it on a turtle and a computer.
Spoiler

local sSide = "top" -- side that moden is on computer
term.clear() -- clear screen
term.setCursorPos(1,1) -- sets cursos position to top left
if turtle then -- test if it is a turtle
rednet.open("right")
while true do -- starts loop
  local event,arg1,arg2,arg3 = os.pullEvent("rednet_message")
  pcall( function()
    local tWords = {}
    for match in string.gmatch(arg2, "[^ t]+") do
	 table.insert( tWords, match )
    end
    for i = 1,#tWords do
	 if tonumber(tWords[i]) then
	 tWords[i] = tonumber(tWords[i])
	 end
    end
    shell.run(unpack(tWords))
   end
  )
end -- end of loop
else -- if not a turtle then
rednet.open(sSide)
while true do
  rednet.broadcast(read())
end
end
Zoinky #7
Posted 31 August 2012 - 11:16 AM
you use functions there, not programs so do

rednet.broadcast('turtle.up()')
and it will move up. give me a minute to work on something to run programs

Oh alright. Thanks B)/>/> and sorry for the trouble :)/>/>
KaoS #8
Posted 31 August 2012 - 11:17 AM
Receiver

local id,msg,dist=rednet.receive()
local msg=textutils.unserialize(msg)
local prog=msg[1]
local params={}
for k,v in pairs(msg) do
if k~=1 then
params[#params+1]=tonumber(v) or v
end
end
shell.run(prog,unpack(params))

send code

local cmd={'go','up',5}
rednet.broadcast(textutils.serialize(cmd))

will run as you wanted
Edited on 31 August 2012 - 09:20 AM
Zoinky #9
Posted 31 August 2012 - 11:22 AM
Thank you both B)/>/> It's working now. :)/>/>
Zoinky #10
Posted 31 August 2012 - 11:30 AM
Try this
run it on a turtle and a computer.
Spoiler

local sSide = "top" -- side that moden is on computer
term.clear() -- clear screen
term.setCursorPos(1,1) -- sets cursos position to top left
if turtle then -- test if it is a turtle
rednet.open("right")
while true do -- starts loop
  local event,arg1,arg2,arg3 = os.pullEvent("rednet_message")
  pcall( function()
	local tWords = {}
	for match in string.gmatch(arg2, "[^ t]+") do
	 table.insert( tWords, match )
	end
	for i = 1,#tWords do
	 if tonumber(tWords[i]) then
	 tWords[i] = tonumber(tWords[i])
	 end
	end
	shell.run(unpack(tWords))
   end
  )
end -- end of loop
else -- if not a turtle then
rednet.open(sSide)
while true do
  rednet.broadcast(read())
end
end

Alright. Last time. I'm not going to say it's done until I'm 100% sure. Lol. This is working great except for when I type 'go up 10'. It doesn't do anything. Not sure why… I haven't messed with any of the code. Help would be much appreciated :)/>/>

Edit: 'go up 10' was a example. 'go up' doesn't work at all.
Edit 2: Never mind. Restarted the world and now it's working. Odd?
BigSHinyToys #11
Posted 31 August 2012 - 11:37 AM
did you type in

'go up 10'
or

go up 10
the second one is the correct format for you sender.

Sender has modem on top yes. turtle is active and running the code too .