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?)
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 themPlease 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.
Would you help me do that?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:This would mean you could put an infinite number of args into the function when you call it and use/receive all of themfunction tester( ... ) local args = { ... } end
Would you help me do that?
I'll try my own first
--[[ 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()
Usage: cobble [A/T] [value]
A = Amount
T = Time
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.
I don't know what to do outside. I don't see anywhere to put the args. and how does that os.startTimer() work?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.
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])
local function timer()
os.startTimer(toMine)
end
local function digFunction()
while true do
-- whatever happens for the digging here
end
end
parallel.waitForAny(timer, digFunction)
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 " " ^_^/>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?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
--[[ 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")
Yep.Is the API inside the computercraft mod?
goodYep.Is the API inside the computercraft mod?