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

Program input

Started by nikolay04, 17 September 2016 - 06:40 PM
nikolay04 #1
Posted 17 September 2016 - 08:40 PM
il try to explain this the best way i can. If i want to make a program that works kinda like edit, you put a string/int behind the program name and use that input to do something, like this.

commandturtle <workerId>
also if you need the program this is used for.

workerId = 98
function toMoveCommand(keycode)
if keycode == keys.w then
  return {"go", "forward"}
elseif keycode == keys.a then
  return {"go", "left"}
elseif keycode == keys.d then
  return {"go", "right"}
elseif keycode == keys.s then
  return {"go", "back"}
elseif keycode == keys.space then
  return {"go", "up"}
elseif keycode == keys.leftShift then
  return {"go", "down"}
else
  return nil
  end
end
function remoteMove()
local action, keycode = os.pullEvent("key")
command = toMoveCommand(keycode)
if command == nil then
  print("Ukjent knapp: " .. keycode)
else
  print("Sender ".. unpack(command))
  rednet.send(workerId, command)
end
end
function main()
print"Venter på kommando ..."
rednet.open"back"
while true do
remoteMove()
end
end
main()
so basicly i want workerId to be the input behind the program name to become what the workerId wil be in the script

commandturtle <workerId>
Bomb Bloke #2
Posted 18 September 2016 - 09:04 AM
When you run a script from the CraftOS commandline, it compiles it up as a function and executes that, passing in the command line arguments as string-based varargs.

Hence you might do something like:

local args = {...}

local workerId = tonumber(args[1])

if not workerId then error("Please run me in the form of \"commandturtle <workerIdNum>\".") end