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

Either my Loops won't Run or my Function won't get called... Help please?

Started by cardmime, 12 October 2012 - 06:01 PM
cardmime #1
Posted 12 October 2012 - 08:01 PM
Okay so I'm creating a program to have a mining turtle mine out a 15x15 area this is the code I have:

function movement(length)
   movement = 0
	  while movement < length do
		 turtle.digDown()
		 turtle.forward()
		 movement = movement + 1
	  end
end
while turtle.detectDown() do
   move = 15
	  while move > 0 do
		 distance = move
		 turnTimes = 3
		    while turnTimes > 0 do
			   movement(distance)
			   turtle.turnRight()
			   turnTimes = turnTimes - 1
		    end
		 move = move - 1
	  end  
end

Now my problem is that when I run this the turtle stops working after moving forward the first 15 spaces and turning right. So either I'm thinking that my loop that starts "while turnTimes > 0" isn't working or I can't call the function movement() multiple times. If anyone has a solution to this that'd be great. Thank you!

- Cardmime
Cranium #2
Posted 12 October 2012 - 08:05 PM
You could benefit from using for loops instead of while loops.
An example is this:

for i = 1,10 do
    -- " i " is the iterator, 1 is the starting point, and 10 is the ending point.
    -- in this instance, the loop will run 10 times and automatically stop
    turtle.forward()
end
This will make the turtle move forward 10 times, then automatically stop.
Lyqyd #3
Posted 12 October 2012 - 08:12 PM
Try not using the variable movement or changing the name of the function to something other than movement. You're reassigning the variable holding the function to 0 the first time it's called. Also, local is your friend.