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

Using tArgs?

Started by slango20, 30 March 2013 - 12:07 PM
slango20 #1
Posted 30 March 2013 - 01:07 PM
how do I use them? my code:

shell.run("clear")
rednet.open("right")
turtle.select(1)
tArgs = { ... }
lvl = #tArgs --sets the variable lvl to number entered
print("Enchanting at "..lvl)
m = peripheral.wrap("left") --wraps the enchanting table as a periperal
function getLevel() -- creates the function getLevel
if m.getLevels() < lvl then --checks if the turtle has the levels required
  sleep(5)
  getLevel()
else
  m.enchant(lvl)
end
end
--calls said function
function pull()
if turtle.getItemCount(1) < 1 then
turtle.suckDown()
return true
else
return true
end
end
if pull() == true then
getLevel()
print("Ejecting item into Chest")
turtle.dropDown()
sleep(2)
shell.run("clear")
end
the goal is to make the turtle enchant somthing-shovel of the earthmover, anyone?-at the level specified in the argument, and be able to do it via enderchest and enchanting terminal, note: I have miscperipherals
Sammich Lord #2
Posted 30 March 2013 - 01:09 PM
tArgs is a table. Using the # symbol will grab the number of the table and not the arg. Use this instead:

tArgs = {...}
local lvl = tonumber(tArgs[1])
slango20 #3
Posted 30 March 2013 - 01:13 PM
tArgs is a table. Using the # symbol will grab the number of the table and not the arg. Use this instead:

tArgs = {...}
local lvl = tonumber(tArgs[1])
that should work
jag #4
Posted 30 March 2013 - 01:19 PM
Yes so the … returns the argument a function gets (in this case the program is represented as a function). If you conclude it in curley brackets {} it will basically return all arguments in a table.

If you don't know how tables works then I highly recommend you to go to the Lua or CC wiki.

Let's say you run the program like this:

myProgram hi hello bowl

Then because the shell seperates arguments with spaces the returned table would look a lot like following:

var = {
  [1] = "hi",
  [2] = "hello",
  [3] = "bowl",
}

As a test you can try do this:

tArgs = {...}

for count = 1,#tArgs do
  print( tostring( tArgs[count] ) )
end

This will simply just print out each argument on a line.

I hope I helped you!
(And that I don't get ninja'd :I )

EDIT: Damn, when I started typing noone had yet answered.
jag #5
Posted 30 March 2013 - 01:22 PM
tArgs is a table. Using the # symbol will grab the number of the table and not the arg. Use this instead:

tArgs = {...}
local lvl = tonumber(tArgs[1])

I think it's funny how you define "lvl" as a local variable but not "tArgs".
Sammich Lord #6
Posted 30 March 2013 - 01:22 PM
tArgs is a table. Using the # symbol will grab the number of the table and not the arg. Use this instead:

tArgs = {...}
local lvl = tonumber(tArgs[1])

I think it's funny how you define "lvl" as a local variable but not "tArgs".
I was tired. IRC + Forums don't mix when you are trying to answer a question.