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

how do you make something like the pre-made tunnel x program where x = read()?

Started by CCJJSax, 29 December 2012 - 06:36 PM
CCJJSax #1
Posted 29 December 2012 - 07:36 PM
I have been trying to figure out how Dan200 made the tunnel program where in the directory, you can type in the desired program, and then a number (still in the directory) and then run that program using that number in the program. I have been instead running the program then have it ask how many times to run. I'd like to be able to be able to do stay in the directory and type "staircase x" where x=read() but I can't figure it out. any help would be much appreciated! :D/>
Luanub #2
Posted 29 December 2012 - 08:16 PM
Read up on using arguments.

Briefly the program will capture the arguments into a table called tArgs{} so you will need to declare that at the start of your program. Then anywhere that you would have used the x from the read() you will want to use the data from the table instead. Here is a short example to get you started.


local tArgs = {...} -- to accept any number of arguments

--do some error checking
if #tArgs < 1 then -- if no args sent(can adjust this to be more specific to your program)
  error("Usage: staircase x")
end

--the data will be stored using standard indicies so it will start with 1
x = tArgs[1] -- to replace the read
ChunLing #3
Posted 29 December 2012 - 08:23 PM
local tArgs = { ... }
x = tArgs[1]
y = tArgs[2]
z = tArgs[3]
Note that the command line parameters are always passed as strings, if you need them to be numbers you must tonumber them.