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

turtle repeat

Started by Hydrotronics, 29 August 2015 - 02:15 PM
Hydrotronics #1
Posted 29 August 2015 - 04:15 PM
This may be considered a revive of an old topic, but it wasn't quite what I was looking for… I found a topic about a turtle repeating a specific amount of times, but it wasn't in the context that I was looking for, as I want a code to repeat 5 times. Any ideas on how to do this? If there is a post with this topic and context, please give a link?

E.G: shell.run("go down") X5

thats essentially what I want it to do. I don't want to write the same string over and over again as if it is in high quantities, it'll get pretty boring. This is for the current project and future reference
Lignum #2
Posted 29 August 2015 - 05:02 PM
Since you're already using the command, this should work:

shell.run("go down 5")

Though it's generally a bad idea to use shell.run for simple tasks like this, use the turtle api instead. You can use a for loop to repeat the action:

for i=1,5 do
   turtle.down()
end
Hydrotronics #3
Posted 29 August 2015 - 06:17 PM
Ah! I see! well, i shall be using the turtle API instead from now on for tasks like this!! Thx for the help, but for future reference, what does the i=1,5 mean? ik it means to repeat x amount of times but what does the I part mean? if you could tell me that that'd be fantastic!! if not, then thx for the help anyway!!!
KingofGamesYami #4
Posted 29 August 2015 - 06:37 PM
It's a numeric for loop. 'i' can be any variable you want, and will be local to the scope of the loop. It will contain the current iteration:


for i = 1, 5 do
  print( "i is now: " .. i )
end
Hydrotronics #5
Posted 30 August 2015 - 06:19 PM
It's a numeric for loop. 'i' can be any variable you want, and will be local to the scope of the loop. It will contain the current iteration:

sooooooo your saying that it defines the amount of times to loop? right?
KingofGamesYami #6
Posted 30 August 2015 - 07:45 PM
Sort of. i = 1 means the loop will start at 1. The second value, 5, means it will iterate until i >= 5. The lack of a third value means the loop will default to +1 increments. If you add a third value, you could have it count by different increments, including negative numbers.
Edited on 30 August 2015 - 05:46 PM
powerboat9 #7
Posted 07 September 2015 - 04:42 AM
Output:

i is now: 1
i is now: 2
i is now: 3
i is now: 4
i is now: 5
Hydrotronics #8
Posted 30 October 2015 - 03:30 PM
Sort of. i = 1 means the loop will start at 1. The second value, 5, means it will iterate until i >= 5. The lack of a third value means the loop will default to +1 increments. If you add a third value, you could have it count by different increments, including negative numbers.

ohhhh right, k, i get it now! thx :D/>