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

[Lua] [Question] While loop problem

Started by juiceblox, 23 September 2012 - 10:39 PM
juiceblox #1
Posted 24 September 2012 - 12:39 AM
My function that I call "forward" tells my miner turtle to move forward one place once all blocks are clear from path. So in my function I put a while loop to detect and dig up any falling blocks from the front before moving forward. This function is in a self made api file named "m".


function forward()
while turtle.detect() do
  turtle.dig()
  print ("I'm digging!")
end
turtle.forward()
print ("I'm moving forward")
os.sleep(1)
end

The following is the loop that the turtle program uses to call on the function in my api file.

x = 0
while x < 10 do
m.forward()
x = x + 1
end

It turns out that the miner works perfectly from the very start, but if there are any falling blocks after it has moved forward once, the bot falls short of 10 paces. It seems after going forward once, any dig actions count towards a forward action, and I suppose dings up my x counter.

I put the one second timeout in the function to gauge this. The first tower of gravel gets mined really fast, but the second tower of gravel is mined with the one second time out and counts up my x counter. I don't see how, since it should be doing the while loop inside the function. And I don't see how it's not if it IS mining! I'm baffled.
Lettuce #2
Posted 24 September 2012 - 12:50 AM
While I'm not sure exactly why this happens, I have a theory. When the turtle digs, perhaps for a split second, it no longer detects the block. I always use the while not turtle.forward() do check instead. I never have trouble with gravel anymore. My usual "moveForward" code is this:


function moveForward()
repeat
turtle.dig()
until turtle.forward()
end

*edit: MysticT, you've been ninja'd.
MysticT #3
Posted 24 September 2012 - 12:50 AM
Put a sleep inside the while loop, so it waits for the blocks to fall before detecting. Otherwise it might not detect the block and try to go forward, wich will fail and count as if it moved.
Another option would be to do a loop like:

while not turtle.forward() do -- while it can't move forward
  turtle.dig()
end
juiceblox #4
Posted 24 September 2012 - 12:54 AM
Ya, I have a sleep function to try that fix. But despite the miner clearly pausing for the dropped blocks to fall, it didn't change a thing. I will try the 'while not'. I didn't know it existed. Thanks.
juiceblox #5
Posted 24 September 2012 - 12:59 AM
K, so I put another sleep function in the while loop when it's digging, and it works now. I just don't know how the first tower of gravel didn't cause the mishap before…wierd. Anyway, it's less efficient, so balls to it.
Lettuce #6
Posted 24 September 2012 - 01:05 AM
Try the "while not" loop, or "repeat," it doesn't need to sleep. MysticT and I gave you the most efficient possible way to do it. 3 lines, 5 if you include function, and end. What's not efficient about it?
juiceblox #7
Posted 24 September 2012 - 01:20 AM
No, I meant my way being inefficient.

Btw, now my bot goes an extra step at the end. What is the reason? Here's my new code for the function forward.
*Edit. It doesn't go an extra step if there's a block in front of it.*


function forward()
a = 0
while not turtle.forward() do
  turtle.dig()
  print ("I'm digging!")
  a = a + 1
  if a > 32 then
   break
  end
end
turtle.forward()
print ("I'm moving forward!")
end
MysticT #8
Posted 24 September 2012 - 01:30 AM
You don't need the second turtle.forward, the one in the while loop already moves the turtle.
Lettuce #9
Posted 24 September 2012 - 01:31 AM
Try using the >= operator instead, and see where that gets you. In fact, let me stop you there. try this:

function forward()
   while not turtle.forward() do
	  turtle.dig()
	  print "I'm digging!"
	  a = a + 1 --in case it's important for something else, if it's just to control how far it goes, delete.
   end
end

for i = 1,32 do
forward()
end

Now you can enter how far you want it to go in "1,32" (the second number) and use a different length elsewhere, if desired. It's not set at 32, and it's also more efficient.

Hope that helped.

–Lettuce

*edit:
P.S. Try typing that yourself, you remember better that way.

Ninja wars, eh MysticT?
juiceblox #10
Posted 24 September 2012 - 02:59 AM
Ok, I am not understanding what the while not line is communicating, then. Could someone phrase it in a way I can understand. Is it, "if the return on forward is false (didn't go), then dig until until it's true and break loop?
Fatal_Exception #11
Posted 24 September 2012 - 04:35 AM
Yes.