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

Help me with args

Started by tonkku107, 31 May 2013 - 11:21 AM
tonkku107 #1
Posted 31 May 2013 - 01:21 PM
So, Would someone help me getting started with args = {…} thing. and/or whatever tArgs?
I would like it on my Cobble Turtle program. Linky
Like
>Cobble [a/t] [value]
a = amount
t = time
(can I make it mine a specified time too?)
W00dyR #2
Posted 31 May 2013 - 01:30 PM
Please read this topic. I just explained this person how to use arguments.

You can also do something for a specific time using the os.startTimer() function.
Exerro #3
Posted 31 May 2013 - 01:31 PM
I am not really sure what you're asking, but I think it is something along the lines of "how does the line args = { … } work".
What is does it capture all of the arguments passed in from the shell ( or whatever is calling it ) for example "myprogram arg1 arg2 arg3" would call myprogram with the args { [1] = "arg1", [2] = "arg2", [3] = "arg3" }
This is also the case in functions:
function tester( ... )
   local args = { ... }
end 
This would mean you could put an infinite number of args into the function when you call it and use/receive all of them
tonkku107 #4
Posted 31 May 2013 - 01:35 PM
Please read this topic. I just explained this person how to use arguments.

You can also do something for a specific time using the os.startTimer() function.
I am not really sure what you're asking, but I think it is something along the lines of "how does the line args = { … } work".
What is does it capture all of the arguments passed in from the shell ( or whatever is calling it ) for example "myprogram arg1 arg2 arg3" would call myprogram with the args { [1] = "arg1", [2] = "arg2", [3] = "arg3" }
This is also the case in functions:
function tester( ... )
   local args = { ... }
end 
This would mean you could put an infinite number of args into the function when you call it and use/receive all of them
Would you help me do that?
I'll try my own first
W00dyR #5
Posted 31 May 2013 - 01:38 PM
Would you help me do that?
I'll try my own first

In the links I posted its explained pretty well, you should know how to figure it out yourself :)/> Post your code when you have something and we will help you with your problems :)/>
tonkku107 #6
Posted 31 May 2013 - 01:44 PM
Spoiler

--[[ Cobble Turtle ]]--

local mined = 0
local amount = false
local args = {...}

if #args ~=2 then
    print("Usage: cobble [A/T] [value]")
    print("A = Amount")
    print("T = Time")
    return
end

function dropItems()
    turtle.turnRight()
    turtle.turnRight()
    print("Dropping Items...")
    for i = 1, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.select(1)
    print("Dropped!")
    turtle.turnRight()
    turtle.turnRight()
end

local process = tonumber(args[1])
local value = tonumber(args[2])

print("Amount set to " .. tostring(value))

repeat
        if turtle.dig() then
         print("Mined!")
                mined = mined + 1
        end
        sleep(0.2)
        local full = true
for i = 1, 16 do
    full = (turtle.getItemSpace(i) == 0) and full
end
if full then
    print("Inventory is full!")
    dropItems()
end
until amount == mined
print("Successfully mined "..mined.." cobblestone!")
    dropItems()
I got this now but i don't know what to do next
W00dyR #7
Posted 31 May 2013 - 01:49 PM
It might be me, but I don't quite get the part of your syntax:


Usage: cobble [A/T] [value]
A = Amount
T = Time

What exactly are you trying to give it as an argument? Like, is your syntax supposed to be something like:

cobble A 50 – And it will mine 50 cobble
cobble T 10 – It will mine cobble for 10 seconds

First of all, you never ended your if statement ;)/> The if loop you posted is pretty much a check for the input, if its incorrect, it will end the loop using "return", but it has nothing further to do with how the arguments work.

Also, I copy pasted this from the topic I linked you to, you should be able to edit what I posted there into your situation:

The way it works is just like this:


local args = {...} -- Puts the arguments into a table
local variableOne = tonumber(args[1]) -- Calls the first item in the table and changes it to a number
local variableTwo = tonumber(args[2]) -- Calls the second item in the table and changes it to a number

If the syntax for the code to work would be "dig <length> <depth>", then if I type "dig 10 5", in the code above the variableOne = 10, and variableTwo = 5. So the way it works is that it its just like typing variableOne = 10, but instead of that the code reads the users input and sets that to be the variable.
tonkku107 #8
Posted 31 May 2013 - 01:53 PM
First of all, you never ended your if statement ;)/> The if loop you posted is pretty much a check for the input, if its incorrect, it will end the loop using "return", but it has nothing further to do with how the arguments work.

Also, I copy pasted this from the topic I linked you to, you should be able to edit what I posted there into your situation:

The way it works is just like this:

[color=#000088]local[/color][color=#000000] args [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]{...} -- Puts the arguments into a table[/color]
[color=#000088]local[/color][color=#000000] variableOne [/color][color=#666600]=[/color][color=#000000] tonumber[/color][color=#666600]([/color][color=#000000]args[/color][color=#666600][[/color][color=#006666]1[/color][color=#666600]]) -- Calls the first item in the table and changes it to a number[/color]
[color=#000088]local[/color][color=#000000] variableTwo [/color][color=#666600]=[/color][color=#000000] tonumber[/color][color=#666600]([/color][color=#000000]args[/color][color=#666600][[/color][color=#006666]2[/color][color=#666600]]) [/color][color=#666600]-- Calls the second item in the table and changes it to a number[/color]

If the syntax for the code to work would be "dig <length> <depth>", then if I type "dig 10 5", in the code above the variableOne = 10, and variableTwo = 5. So the way it works is that it its just like typing variableOne = 10, but instead of that the code reads the users input and sets that to be the variable.
I don't know what to do outside. I don't see anywhere to put the args. and how does that os.startTimer() work?
and i edited the code with the ending

and yes.
W00dyR #9
Posted 31 May 2013 - 02:10 PM
In the spoiler the code for setting a mode with the arguments + a set variable that holds how much/how long to mine for.

Spoiler

local args = {...}
local mode = nil
 
local function printUsage()
  print("Usage: cobble [A/T] [value]")
  print("A = Amount")
  print("T = Time")
  return
end

if #args ~= 2 then
  printUsage()
end

if args[1] == A then
  mode = "amount"
elseif args[1] == T then
  mode = "timer"
else
  printUsage()
end

local toMine = tonumber(args[2])

The os.startTimer() could be used together with the parallel API, you can make two functions, one for mining (with a while loop), and one simple one looking something like:


local function timer()
  os.startTimer(toMine)
end

local function digFunction()
  while true do
	-- whatever happens for the digging here
  end
end

Then using the parallel api something like this would make it run both functions, while waiting for the timer to finish (since the mining never finishes due to the while loop):


parallel.waitForAny(timer, digFunction)

Using an if statement you can determine which mode to use:


if mode == "amount" then
  for i = 1, toMine do
	-- mining stuff
  end
elseif mode == "timer" then
  -- stuff that needs to happen for a certain time (posted above)
else
  return
end
H4X0RZ #10
Posted 31 May 2013 - 07:36 PM
In the spoiler the code for setting a mode with the arguments + a set variable that holds how much/how long to mine for.

Spoiler

local args = {...}
local mode = nil

local function printUsage()
  print("Usage: cobble [A/T] [value]")
  print("A = Amount")
  print("T = Time")
  return
end

if #args ~= 2 then
  printUsage()
end

if args[1] == A then
  mode = "amount"
elseif args[1] == T then
  mode = "timer"
else
  printUsage()
end

local toMine = tonumber(args[2])

The os.startTimer() could be used together with the parallel API, you can make two functions, one for mining (with a while loop), and one simple one looking something like:


local function timer()
  os.startTimer(toMine)
end

local function digFunction()
  while true do
	-- whatever happens for the digging here
  end
end

Then using the parallel api something like this would make it run both functions, while waiting for the timer to finish (since the mining never finishes due to the while loop):


parallel.waitForAny(timer, digFunction)

Using an if statement you can determine which mode to use:


if mode == "amount" then
  for i = 1, toMine do
	-- mining stuff
  end
elseif mode == "timer" then
  -- stuff that needs to happen for a certain time (posted above)
else
  return
end
In your first snippet (the thing in the spoiler) you don't have defined A/T (If it's a variable). I think you have forgotten the " " ^_^/>
tonkku107 #11
Posted 01 June 2013 - 04:07 AM
In the spoiler the code for setting a mode with the arguments + a set variable that holds how much/how long to mine for.

Spoiler

local args = {...}
local mode = nil

local function printUsage()
  print("Usage: cobble [A/T] [value]")
  print("A = Amount")
  print("T = Time")
  return
end

if #args ~= 2 then
  printUsage()
end

if args[1] == A then
  mode = "amount"
elseif args[1] == T then
  mode = "timer"
else
  printUsage()
end

local toMine = tonumber(args[2])

The os.startTimer() could be used together with the parallel API, you can make two functions, one for mining (with a while loop), and one simple one looking something like:


local function timer()
  os.startTimer(toMine)
end

local function digFunction()
  while true do
	-- whatever happens for the digging here
  end
end

Then using the parallel api something like this would make it run both functions, while waiting for the timer to finish (since the mining never finishes due to the while loop):


parallel.waitForAny(timer, digFunction)

Using an if statement you can determine which mode to use:


if mode == "amount" then
  for i = 1, toMine do
	-- mining stuff
  end
elseif mode == "timer" then
  -- stuff that needs to happen for a certain time (posted above)
else
  return
end
Is the API inside the computercraft mod?
tonkku107 #12
Posted 01 June 2013 - 04:22 AM
Spoiler

--[[ Cobble Turtle ]]--
local mined = 0
local amount = false
local args = {...}
local mode = nil
local function printUsage()
	print("Usage: cobble [A/T] [value]")
	print("A = Amount")
	print("T = Time")
	return
end
if #args ~=2 then
	printUsage()
end
function dropItems()
	turtle.turnRight()
	turtle.turnRight()
	print("Dropping Items...")
	for i = 1, 16 do
		turtle.select(i)
		turtle.drop()
	end
	turtle.select(1)
	print("Dropped!")
	turtle.turnRight()
	turtle.turnRight()
end
if args[1] == A then
	mode = "amount"
elseif args[1]  == T then
	mode = "timer"
else
	printUsage()
end
local toMine = tonumber(args[2])
local function timer()
	os.startTimer(toMine)
end
local function digFunction()
	while true do
		if turtle.dig() then
			print("Mined!")
			mined = mined + 1
		end
		sleep(0.2)
		local full = true
		for i = 1, 16 do
			full = (turtle.getItemSpace(i) == 0) and full
		end
		if full then
			print("Inventory is full!")
			dropItems()
		end
		until toMine == mined
			print("Successfully mined "..mined.." cobblestone!")
			dropItems()
	end
end

print("I will mine " ..tostring(toMine) " cobblestone")
I got this now. I didn't do the last code box. Tell me if something is wrong
wohoo! 100 posts and scripter status
Bomb Bloke #13
Posted 01 June 2013 - 04:24 AM
Is the API inside the computercraft mod?
Yep.
tonkku107 #14
Posted 01 June 2013 - 05:29 AM
Is the API inside the computercraft mod?
Yep.
good