This is a read-only snapshot of the ComputerCraft forums,
            taken in April 2020.
        
        
    
     
        Remotely running commands on a turtle
Started by Zoinky, 31 August 2012 - 08:09 AMPosted 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.
                
            Posted 31 August 2012 - 10:17 AM
                use the loadstring command
                
            
local tempfunction=loadstring("print('me')")
local id,msg,dist=rednet.receive()
loadstring(msg)()
Posted 31 August 2012 - 10:24 AM
                Wow. Thanks alot B)/>/> Didn't realize it was that simple. I owe you one :)/>/>
                
            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
                
            Posted 31 August 2012 - 11:13 AM
                you use functions there, not programs so do
                
            
rednet.broadcast('turtle.up()')
Posted 31 August 2012 - 11:16 AM
                Try this
run it on a turtle and a computer.
                
            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
Posted 31 August 2012 - 11:16 AM
                you use functions there, not programs so doand it will move up. give me a minute to work on something to run programsrednet.broadcast('turtle.up()')
Oh alright. Thanks B)/>/> and sorry for the trouble :)/>/>
Posted 31 August 2012 - 11:17 AM
                Receiver
send code
will run as you wanted
                
                    
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
                
            Posted 31 August 2012 - 11:22 AM
                Thank you both B)/>/> It's working now. :)/>/>
                
            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?
Posted 31 August 2012 - 11:37 AM
                did you type in
Sender has modem on top yes. turtle is active and running the code too .
                
            
'go up 10'
go up 10
Sender has modem on top yes. turtle is active and running the code too .
 
                