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

How do I add per loop

Started by Cheesety210, 29 March 2013 - 08:21 PM
Cheesety210 #1
Posted 29 March 2013 - 09:21 PM
I'm trying to create a mining system with an auto torch placement system, and I want to place a torch every 8 times I move (AKA, 8 times per loop). Here's the code:

local mf = 1
print("Please put fuel in slot 1")
print("Length?")
local length = read()
print("Do you want to place torches? (yes or no, no uppercase)")
local torchYN = read()
for i = 1, length do
if turtle.detect() == true then
			    turtle.dig()
	    end
	    turtle.forward()
	    if turtle.detectUp() == true then
			    turtle.digUp()
	    end
	    if turtle.getFuelLevel() < mf then
			    turtle.refuel(1)
	    end
[color=#0000ff]if torchYN == "yes" then
turtle.turnRight()
turtle.turnRight()
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
end[/color]
end
turtle.turnRight()
turtle.turnRight()
print("I'm on my way back")
for i = 1, length do
turtle.forward()
if turtle.getFuelLevel() < mf then
turtle.refuel(1)
end
end
print("I'm back at my starting position")
turtle.turnRight()
turtle.turnRight()
I was thinking when it started the torch placement part, it would do something like:
x+1
if x == 8 then
local x = 0
…….

So it would reset itself, but I would have to declare X being 0 at the top, so it would reset itself every time.
faubiguy #2
Posted 29 March 2013 - 09:33 PM
Yes. You can put
local x = 0
at the top, right after declaring mf, then in the loop do
x = x + 1
if x == 8 then
  x = 0
  -- Place torch
end
.

It will run each time it loops, and reset back to 0 each time it places a torch, so it will do it every 8 blocks.
Mads #3
Posted 29 March 2013 - 10:01 PM
You can have this in your loop:

if i % 8 == 0 then
    -- Place torch
end
Cheesety210 #4
Posted 30 March 2013 - 06:11 AM
You can have this in your loop:

if i % 8 == 0 then
	-- Place torch
end
Can you explain to me how this works? I think it's something like "If I is divisible by 8 and equal to 0 then?" But that doesn't make sense.

Yes. You can put
local x = 0
at the top, right after declaring mf, then in the loop do
x = x + 1
if x == 8 then
  x = 0
  -- Place torch
end
.

It will run each time it loops, and reset back to 0 each time it places a torch, so it will do it every 8 blocks.
Oh ya, because the local x = 0 won't be in the loop so it won't be reset.It works!
Mads #5
Posted 30 March 2013 - 06:39 AM
When diving by 8, if 0 remains(meaning it's part of the, well, "8 table" or something. I have no idea how to say it in English, but part of the 0-8-16-24-32-etc.)
remiX #6
Posted 30 March 2013 - 06:45 AM
It's to check if i is a multiple of 8, if i % 8 == 0 then i goes into 8 a whole number amount of times, with no remainders.
Known as modulus.
Cheesety210 #7
Posted 30 March 2013 - 06:50 AM
thanks
leftler #8
Posted 30 March 2013 - 07:25 AM
The % operator is the Modulo opperator which is a fancy way of saying "remainder after dividing"

so the expression

if i % 8 == 0 then

Would mean "Divide i by 8, if the remainder of the that operation is 0 do the if block"

So say you wanted somthing to happen at the halfway point between the tourches you could do


if i % 8 == 4 then

which would be "Divide i by 8, if the remainder of the that operation is 4 do the if block"