214 posts
Posted 13 September 2012 - 08:50 PM
ok, so i want to make programs with arguements. a prime example would be the tunnel program built into a mining turtle. like you type
tunnel <length>
how does the <length> bit work !?!
please can someone show me a tutorial or explain to me in detail because i have no idea. i really wanna know how this is done and i know its probaly way above my level but i really wanna learn the language of lua. please someone help !
thanks -Cheeky
318 posts
Location
Somewhere on the planet called earth
Posted 13 September 2012 - 09:16 PM
2 ways, function or asking for input.
function tunnel(lenght)
for i = 1, lenght do
turtle.digUp()
turtle.dig
turtle.forward()
end
end
2:
write("lenght: ")
lenght = tonumber(read())
for i = 1, lenght do
turtle.digUp()
turtle.dig
turtle.forward()
end
3790 posts
Location
Lincoln, Nebraska
Posted 13 September 2012 - 09:26 PM
You can also do this:
local tArgs = {...} --takes any arguments from starting the program
if tArgs[1] == "argumentvalue" then --can be any number of arguments, tArgs[1],[2],etc
--do stuff here
elseif tArgs[2] == "argumentvalue2" then
--do other stuff here
end
Each argument is separated into table cells. Such as for "copy program1 program2", would run the "copy" program, with tArgs[1] being program1, and tArgs[2] being program2.
214 posts
Posted 14 September 2012 - 07:18 PM
er thanks, il try this out
1548 posts
Location
That dark shadow under your bed...
Posted 15 September 2012 - 06:22 AM
basically if you make a program with the code:
local tArgs={...}
print(tArgs[1])
name the program test and then enter in
test argument1 argument2
then it will only print 'argument1' as that is the first argument, a good way to use both methods is this:
Program usage: tunnel <length>
local tArgs={...}
if tArgs[1] then
length=tonumber(tArgs[1])
else
print('you have not specified a length, enter it now')
write('Length: ')
length=tonumber(read())
end