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

Using tArgs for the first time, confused and lost. Help?

Started by VampCC, 02 December 2013 - 07:03 AM
VampCC #1
Posted 02 December 2013 - 08:03 AM
I have downloaded a program for my mining turtle, it works great but every time it finishes, it goes back to its starting point, forcing me to break it and place it in the next spot or mess with raw commands.
That pushed me into making a program I call "next", and you guessed it, it moved the turtle to the next spot.

I'm not satisfied with it, because I have to run it a few times to move more than one spot, so I digged into tArgs and read about it, and still am confused.
————————————————————————————————————————————————————————————————-
I want my program to be able to read how much times to run (i.e "next 3" to loop 3 times)

That's what I got so far, I know its messy and bad, and it doesnt even work. :/ Not even talking about efficiency.


local tArgs = {...}
if tArgs == 0 then
   tArgs = 1
end

function next(x)
   for times = 1, x do
	  turtle.turnRight()
	  for i = 1, 3 do
		 if turtle.forward() == false then
			turtle.dig()
			turtle.forward()
		 end
	  end
   turtle.turnLeft()
   end
end

next(tArgs)

Can someone help me? I'm really confused :/
LBPHacker #2
Posted 02 December 2013 - 11:04 AM
tArgs is a table (you set it up with the curly brackets). As a table, it has indices (numeric indices, in this case). Since you put the vararg ( the … thingy ) in it, it now contains all the variables passed to the program. If you type "next 3" into the Shell, the first argument passed to the program will be "3"; therefore it's the first index of tArgs (you can refer to that as tArgs[1]). Now all of that in code:
local args = {...} -- some prefer to call it tArgs - the T at the beginning means it is a table
if not args[1] then args[1] = 1 end -- that will make args[1] default to 1
print("Gonna do something " .. args[1] .. " times") -- first argument

-- implementation of "nextThing" (the function you call "next")
-- don't call it just "next", I'll explain why not

nextThing(tonumber(args[1])) -- args[1] is a string, so we have to convert it to a number

About not naming a function "next": next is a special function in Lua. It's a useful function, don't override it.

EDIT: Forgot to mention what to do if no argument is given. Fixed.
Edited on 02 December 2013 - 10:06 AM
VampCC #3
Posted 02 December 2013 - 11:13 AM
Thanks alot for the detailed explanation, that helps alot :D/>
I'm going to actually try that in game, I'll edit if anything went wrong :)/>
apemanzilla #4
Posted 02 December 2013 - 12:29 PM
Expanding a bit more on the actual table itself, you don't need to name it "tArgs" or "args". Any table defined as {…} will grab the arguments given at the start of the program.
VampCC #5
Posted 02 December 2013 - 01:35 PM
That's a thing I grabbed from direwolf20 on youtube, I just used it without understanding.
awsmazinggenius #6
Posted 03 December 2013 - 08:44 PM
That's a thing I grabbed from direwolf20 on youtube, I just used it without understanding.
First of all, you shouldn't do that :)/>

You don't need a table, it's the … operator that gets the command line params. You could use this (simple program, has a limit on blocks because once I typed "up 8329" into a Wireless Mining Turtle accidentally, lost a modem, a diamond pick, and anything in the Turtle's inventory.)

local blocks = ...
if tonumber(blocks) > 30 then
  blocks = 30
end
for i=1, blocks do
  turtle.up()
  if turtle.getFuelLevel() <= 10 then
    turtle.refuel(1)
  end
end