local main_cmd="turtle.dig" local direction="Down" local params="()" shell.run(main_cmd..direction..params)To be more straightforward, I want a way to use a string like "turtle.digDown()" and make the turtle execute that.
#1
Posted 22 July 2015 - 07:30 PM
#2
Posted 22 July 2015 - 08:12 PM
local trusted_id = 5 --# the id of the sending computer while true do local id, message = rednet.receive() if id == trusted_id then if message = "MOVE_FORWARD" then turtle.forward() end end end
You could even take this design further by putting all the commands in a table and simplifying everything:
local trusted_id = 5 --# the id of the sending computer local commands = {} --# a table holding all the commands as keys and their respective functions as values command["MOVE_FORWARD"] = function () --# make a function with some logic inside if not turtle.detect() then turtle.forward() end end command["MOVE_BACK"] = turtle.back --# or simply assign one of the functions directly while true do local id, message = rednet.receive() if id == trusted_id then local command = commands[message] --# get the command from the table if command then --# if there is a command command() --# execute it end end end
Of course, you may actually need to send custom code to the turtle, but from your example I figured you want to run pretty straight forward commands.
#3
Posted 22 July 2015 - 08:24 PM
#4
Posted 22 July 2015 - 08:57 PM
MKlegoman357, on 22 July 2015 - 08:12 PM, said:
local trusted_id = 5 --# the id of the sending computer while true do local id, message = rednet.receive() if id == trusted_id then if message = "MOVE_FORWARD" then turtle.forward() end end end
You could even take this design further by putting all the commands in a table and simplifying everything:
local trusted_id = 5 --# the id of the sending computer local commands = {} --# a table holding all the commands as keys and their respective functions as values command["MOVE_FORWARD"] = function () --# make a function with some logic inside if not turtle.detect() then turtle.forward() end end command["MOVE_BACK"] = turtle.back --# or simply assign one of the functions directly while true do local id, message = rednet.receive() if id == trusted_id then local command = commands[message] --# get the command from the table if command then --# if there is a command command() --# execute it end end end
Of course, you may actually need to send custom code to the turtle, but from your example I figured you want to run pretty straight forward commands.
Instead of using IDs (= rednet) it would be a bit more secure to use the modem API and a "random" channel. If you want to go even further, you could use Diffie Hellman to find a private, random channel.
But, well ,this random channel could still be "bruteforced" as in using ~500 computers listening to all channels.
Edited by H4X0RZ, 22 July 2015 - 08:58 PM.
#5
Posted 23 July 2015 - 06:26 AM
H4X0RZ, on 22 July 2015 - 08:57 PM, said:
But, well ,this random channel could still be "bruteforced" as in using ~500 computers listening to all channels.
Err, wouldn't you only need one? Just open every channel and wait for a message?
Plus, someone would have to know about your program, its flaw, and have a motive to infiltrate it.
Though I agree its better to store functions locally and wait for a message to run them, rather than sending code around.
#6
Posted 23 July 2015 - 07:30 AM
HPWebcamAble, on 23 July 2015 - 06:26 AM, said:
The maximum number of channels you can open on a single modem is 128, so you'd need a bunch of computers and modems to sniff all 65535 channels.
#7
#8
Posted 23 July 2015 - 05:17 PM
jerimo, on 23 July 2015 - 03:29 PM, said:
turtle[input]()
Which would run any turtle cmand you sent it, without the need for expansive if statements
My example code does pretty much the same thing, except rather than calling functions of the turtle API directly, letting you create your own commands and functions.
#9
Posted 24 July 2015 - 07:59 AM
MKlegoman357, on 23 July 2015 - 05:17 PM, said:
if turtle[cmd] then turtle[cmd]() endOf course you could expand that to ALSO allow extra functions, by combining the both ideas.
if turtle[cmd] then turtle[cmd]() elseif tbl[cmd] then tbl[cmd]() end
#10
Posted 24 July 2015 - 09:42 PM
MKlegoman357, on 23 July 2015 - 05:17 PM, said:
jerimo, on 23 July 2015 - 03:29 PM, said:
turtle[input]()
Which would run any turtle cmand you sent it, without the need for expansive if statements
My example code does pretty much the same thing, except rather than calling functions of the turtle API directly, letting you create your own commands and functions.
If you wanted to add.more APIs you could also do
Function run(APIName, funcName) if _G[APIName][funcName] then _G[APIName][funcName]() End EndWhich would allow you to use any function in any loaded API
Edited by jerimo, 24 July 2015 - 09:42 PM.
#11
Posted 25 July 2015 - 01:24 AM
jerimo, on 24 July 2015 - 09:42 PM, said:
MKlegoman357, on 23 July 2015 - 05:17 PM, said:
jerimo, on 23 July 2015 - 03:29 PM, said:
turtle[input]()
Which would run any turtle cmand you sent it, without the need for expansive if statements
My example code does pretty much the same thing, except rather than calling functions of the turtle API directly, letting you create your own commands and functions.
If you wanted to add.more APIs you could also do
Function run(APIName, funcName) if _G[APIName][funcName] then _G[APIName][funcName]() End EndWhich would allow you to use any function in any loaded API
#12
Posted 25 July 2015 - 02:39 AM
jerimo, on 24 July 2015 - 09:42 PM, said:
I wouldn't say it's "easier" - and I wouldn't say Dragon said that, either.
I mean, yeah, the code ends up shorter on the turtle's side, but as a result you need to send a lot more data over rednet to get the job done. Would you rather you just sent a "go forward" command and the turtle went forward, or would you rather that you had to send a "go forward" command, check on the result, send another command to make the turtle dig / attack if the movement failed, send another "go forward" command, etc, etc, etc...?
It's "easier" to handle such things locally, on the turtle itself. Granted, having both options (which is what Dragon is suggesting) is better again, but if I were going to choose between the two methods, I'd do the same as MKlegoman357.
Edited by Bomb Bloke, 25 July 2015 - 02:40 AM.
#13
Posted 25 July 2015 - 06:27 AM
Bomb Bloke, on 25 July 2015 - 02:39 AM, said:
jerimo, on 24 July 2015 - 09:42 PM, said:
I wouldn't say it's "easier" - and I wouldn't say Dragon said that, either.
I mean, yeah, the code ends up shorter on the turtle's side, but as a result you need to send a lot more data over rednet to get the job done. Would you rather you just sent a "go forward" command and the turtle went forward, or would you rather that you had to send a "go forward" command, check on the result, send another command to make the turtle dig / attack if the movement failed, send another "go forward" command, etc, etc, etc...?
It's "easier" to handle such things locally, on the turtle itself. Granted, having both options (which is what Dragon is suggesting) is better again, but if I were going to choose between the two methods, I'd do the same as MKlegoman357.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users