Jump to content




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


  • You cannot reply to this topic
10 replies to this topic

#1 Nyny2121

  • New Members
  • 3 posts

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.

#2 Heracles421

  • Members
  • 258 posts

Posted 11 January 2013 - 05:17 PM

View PostNyny2121, on 11 January 2013 - 05:12 PM, said:

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


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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...

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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. :)

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 12 January 2013 - 03:20 AM

View PostLyqyd, on 11 January 2013 - 08:30 PM, said:

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.


#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#7 Nyny2121

  • New Members
  • 3 posts

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.

#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 January 2013 - 05:14 PM

View PostKingdaro, on 12 January 2013 - 05:04 PM, said:

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 ;)

#10 EpicTreeMiner

  • Members
  • 17 posts
  • LocationSouth Africa

Posted 13 January 2013 - 09:06 AM

View PostNyny2121, on 12 January 2013 - 04:33 PM, said:

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;)

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 13 January 2013 - 10:39 AM

View PostTheOriginalBIT, on 12 January 2013 - 05:14 PM, said:

View PostKingdaro, on 12 January 2013 - 05:04 PM, said:

-- snip

I think thats been covered several times now ;)

View PostEpicTreeMiner, on 13 January 2013 - 09:06 AM, said:

View PostNyny2121, on 12 January 2013 - 04:33 PM, said:

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
;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users