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

[Lua] Making a turtle do something a specific number of times

Started by Nyny2121, 11 January 2013 - 04:12 PM
Nyny2121 #1
Posted 11 January 2013 - 05:12 PM
Hey, just wanted help on creating a program with user inputs.
For example, when using the tunnel command, you type tunnel 3
How do I make it read how many times to do it?
For example, how would I make a program that does turtle.up() for a program called flymeaway? Example: flymeaway 5 makes the turtle move up 5 times.
Heracles421 #2
Posted 11 January 2013 - 05:17 PM
Hey, just wanted help on creating a program with user inputs.
For example, when using the tunnel command, you type tunnel 3
How do I make it read how many times to do it?
For example, how would I make a program that does turtle.up() for a program called flymeaway? Example: flymeaway 5 makes the turtle move up 5 times.

Simple, use loops

function flymeaway(blocks)
  blocks = blocks or 1 -- Make this to default the amount of blocks to 1 if we don't get amount
  for i = 1,blocks do
    turtle.up()
  end
end
theoriginalbit #3
Posted 11 January 2013 - 05:32 PM
there are 3 types of primitive loops: for, while and repeat.

For loops repeat the instructions for the given amount of times

for i = 1, 9 do -- for 1 - 9 do
for i = 1, 9, 2 do -- for 1, 3, 5, 7, 9 do
for i = 9, 1, -1 do -- for 9-1 do

while loops run until the conditional is true

i = 1
while 1 ~= 9 do
  -- do something
  i = i + 1
end

repeat loops always run the instructions at least once, and after completion of the instructions will evaluate if it should continue

i = 1
repeat
  -- do something
  i = i + 1
until i == 8
NOTE: we use 8 here because it already has run it once, so we want it to run n - 1 times…
Lyqyd #4
Posted 11 January 2013 - 08:30 PM
Clearly, neither of you read the actual question. To get the argument from the command line, you use "…". When using "…", it is usually easiest to put it into a table. So, this might be the start of your program:


local args = {...} --get the arguments from the command line and put them in the table named args

if #args < 1 then --if we did not get any arguments, tell the user to provide them
  print("Usage: "..fs.getName(shell.getRunningProgram()).." <length>") --fancy stuff to print the name of the program.
  return --quit early, since we got no arguments.
elseif tonumber(args[1]) == nil then --the user gave us an argument, but it was not a number! Print usage again.
  print("Usage: "..fs.getName(shell.getRunningProgram()).." <length>")
  return
end

--now, since the code made it here, we must have at least one argument that can be made into a number. 

local length = tonumber(args[1]) --take the first argument (that is the [1] part, the first index of the table), convert it to a number, and store the result in our length variable.

Hope that helps! Feel free to ask more questions if any of my code there is confusing. :)/>
remiX #5
Posted 12 January 2013 - 03:20 AM
Clearly, neither of you read the actual question. To get the argument from the command line, you use "…". When using "…", it is usually easiest to put it into a table. So, this might be the start of your program:


local args = {...} --get the arguments from the command line and put them in the table named args

if #args < 1 then --if we did not get any arguments, tell the user to provide them
  print("Usage: "..fs.getName(shell.getRunningProgram()).." <length>") --fancy stuff to print the name of the program.
  return --quit early, since we got no arguments.
elseif tonumber(args[1]) == nil then --the user gave us an argument, but it was not a number! Print usage again.
  print("Usage: "..fs.getName(shell.getRunningProgram()).." <length>")
  return
end

--now, since the code made it here, we must have at least one argument that can be made into a number.

local length = tonumber(args[1]) --take the first argument (that is the [1] part, the first index of the table), convert it to a number, and store the result in our length variable.

Hope that helps! Feel free to ask more questions if any of my code there is confusing. :)/>

Why use an elseif?



local args = {...} --get the arguments from the command line and put them in the table named args

local length = tonumber(args[1]) --take the first argument (that is the [1] part, the first index of the table), convert it to a number, and store the result in our length variable.
-- put length here so we don't have to check if tonumber(args[1]) is nil...

if #args ~= 1 or not length then --if we did not get any arguments, tell the user to provide them
  print("Usage: "..fs.getName(shell.getRunningProgram()).." <length>") --fancy stuff to print the name of the program.
  return --quit early, since we got no arguments.
end

--now, since the code made it here, we must have at least one argument that can be made into a number.
Lyqyd #6
Posted 12 January 2013 - 03:36 AM
When I'm commenting code that heavily, I mostly try to keep the complexity of each line to a minimum.
Nyny2121 #7
Posted 12 January 2013 - 04:33 PM
Thanks Lyqyd. Also, like I asked, is there any way for the turtle to read the variable and do something like turtle.dig() that many times? That's really all that's left that I need to know.
Kingdaro #8
Posted 12 January 2013 - 05:04 PM

for i=1, var do
  turtle.dig()
end

Where var is the number of times you want it to dig.
theoriginalbit #9
Posted 12 January 2013 - 05:14 PM

for i=1, var do
  turtle.dig()
end

Where var is the number of times you want it to dig.

I think thats been covered several times now ;)/>
EpicTreeMiner #10
Posted 13 January 2013 - 09:06 AM
Thanks Lyqyd. Also, like I asked, is there any way for the turtle to read the variable and do something like turtle.dig() that many times? That's really all that's left that I need to know.

just use a for loop :)/>
code :
print("Dig forward how may blocks?")
local x = tonumber( read() )
function digger()
for i = 1,(x) do
turtle.dig
end
end
digger()

Back to me :P/>
What that does is waits untill user inputs something, in this case how many blocks to mine.
it store the number you type as the variable "x", and then uses the "for" loop to re do the function x amount of times, this will be what ever you typed in the beginning.
its all stored in a function called digger
the last line runs the function;)
remiX #11
Posted 13 January 2013 - 10:39 AM
– snip

I think thats been covered several times now ;)/>
Thanks Lyqyd. Also, like I asked, is there any way for the turtle to read the variable and do something like turtle.dig() that many times? That's really all that's left that I need to know.

just use a for loop :)/>
code :
print("Dig forward how may blocks?")
local x = tonumber( read() )
function digger()
for i = 1,(x) do
turtle.dig
end
end
digger()

Back to me :P/>
What that does is waits untill user inputs something, in this case how many blocks to mine.
it store the number you type as the variable "x", and then uses the "for" loop to re do the function x amount of times, this will be what ever you typed in the beginning.
its all stored in a function called digger
the last line runs the function;)

Okay, I think the whole world knows how to do this now xD

But just for the sake of it:

Spoiler

x = tonumber(read()) -- converts the number string to a number
for i = 1, x do
  -- code
end
;)/>