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

Program infinate loop

Started by Rieu, 18 May 2015 - 02:25 AM
Rieu #1
Posted 18 May 2015 - 04:25 AM
Hello everyone. I just wrote a program that is supposed to harvest obsidian that I make in two 2x18 areas side by side separtated by a wall of dirt. It is now stuck in an infinate loop on the first row. It is supposed to go 18 blocks forward and then turn right. However it never turns right, it keeps going straight mining away. I have included the code below. Any help you guys could give me would be appreciated. Also any tips to make the code cleaner are always welcome.

Thank you,
Ferrwolf


Spoiler–Author: Ferrwolf
–This program is designed for user to place lava in two 2x18 areas
–connected by one block removed in the center on the end where the user started digging.
–Pour water on lava and run turtle program. Instant obsidian. :)/>
– this is an example ==================
– ==================
– =
– ==================
– ==================
– each equal sign is where the blocks should be removed and lava placed.
for i=0,18 do
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
end
turtle.turnRight()
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
turtle.turnRight()
for i=0,17 do
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
end
turtle.turnLeft()
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
turtle.turnLeft()
for i=0,17 do
turtle.dig()
turtle.forward()
while turtle.forward() – false do
turtle.dig()
turtle.forward()
end
end
turtle.turnRight()
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
turtle.turnRight()
for i=0,17 do
turtle.dig()
turtle.forward()
while turtle.forward() == false do
turtle.dig()
turtle.forward()
end
end
Edited on 18 May 2015 - 06:05 PM
Lyqyd #2
Posted 18 May 2015 - 04:58 AM
Moved to Ask a Pro.

Please post the code you're using.
Waitdev_ #3
Posted 18 May 2015 - 06:07 AM
please add the code, but are you doing turtle.turnright() or turtle.turnRight()? because turtle.turnRight() is correct, and also you might not be using "break" in your loop.
Rieu #4
Posted 18 May 2015 - 07:17 AM
please add the code, but are you doing turtle.turnright() or turtle.turnRight()? because turtle.turnRight() is correct, and also you might not be using "break" in your loop.

I am using turtle.turnRight as that is standard camel casing, but as far as break, I am still learning lua, but C++, which I use most of the time, break is only used with a switch. But please feel free to take a look at the code and let me know.

Thanks,
Ferrwolf
Lupus590 #5
Posted 18 May 2015 - 11:43 AM
pastebin link says that the paste has been removed
Bomb Bloke #6
Posted 18 May 2015 - 02:08 PM
Try this version.

Anyway;


for i=0,18 do
  turtle.dig()
  turtle.forward()  -- Try to go forward, and ignore the result.
  while turtle.forward() == false do  -- Try to go forward again, and use the result to determine whether to repeat this loop.
    turtle.dig()
    turtle.forward()  -- Try to go forward yet again!
  end
end

This will make the turtle attempt to go forward an indefinite number of times; at least 38, but depending on how many obstructions it hits, potentially much more. You'd be better off with:

for i=0,18 do
  while not turtle.forward() do
    turtle.attack()
    turtle.dig()
  end
end
Rieu #7
Posted 18 May 2015 - 08:21 PM
Try this version.

Anyway;


for i=0,18 do
  turtle.dig()
  turtle.forward()  -- Try to go forward, and ignore the result.
  while turtle.forward() == false do  -- Try to go forward again, and use the result to determine whether to repeat this loop.
	turtle.dig()
	turtle.forward()  -- Try to go forward yet again!
  end
end

This will make the turtle attempt to go forward an indefinite number of times; at least 38, but depending on how many obstructions it hits, potentially much more. You'd be better off with:

for i=0,18 do
  while not turtle.forward() do
	turtle.attack()
	turtle.dig()
  end
end

Awesome! Thank you. It worked perfectly. :)/>