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

[LUA]Problem with creating a "if holf=true loop"

Started by Gathoc, 29 January 2013 - 08:51 PM
Gathoc #1
Posted 29 January 2013 - 09:51 PM
ok so, i want to create a turtle that does some mining and as soon as it finds a block of diamonds i want it to hold up with everything its doing and wait for the user to either just remove the block or enter some input to the computer.

I thought i had it but it turns out it doesnt quite do what i want :P/>



    --[[Mining Stip Program]]--
	
    --[[Variables]]--
	
    hold=false
	
	
    --[[Functions]]--
	
    function detectDiamond()
	  turtle.select(16)
	  if turtle.compare(true) then
		    hold=true
	  elseif turtle.compareDown(true) then
		    hold=true
	  else
	  turtle.select(1)
	  end
    end
	
	
    function digForward()
	  while turtle.forward() do
		    detectDiamond()
	    turtle.digDown()
	  end
	  for i=1, 3 do
	    detectDiamond()
	    turtle.dig()
	    turtle.forward()
		    detectDiamond()
	    turtle.digDown()
	  end
	  turtle.turnLeft()
	  for i=1, 5 do
		    detectDiamond()
		    turtle.dig()
		    turtle.forward()
	  end
	  for i=1, 5 do
		    turtle.back()
	  end
	  turtle.turnRight()
	  turtle.turnRight()
	  for i=1, 5 do
	    detectDiamond()
	    turtle.dig()
		    turtle.forward()
	  end
	  for i=1, 5 do
	    turtle.back()
	  end
	  turtle.turnLeft()
    end
	
    --[[Execution]]--
	
    while not hold do
	  digForward()
    end
slabb3r #2
Posted 29 January 2013 - 10:05 PM
in your function digForward you dig down but move forward, you must change the call to turtle.digDown() by turtle.dig()
Gathoc #3
Posted 29 January 2013 - 10:09 PM
Yea because if it can move forward it should and then after it has it's supposed to dig down. nothing wrong with that. And thats not the issue here, the issue at hand is that i cant figure out to keep the program at a hold and then have it continue where it left off.
remiX #4
Posted 30 January 2013 - 03:06 AM

while true do -- infinite loop
    if hold then
        -- if hold is true
    else
       -- if hold is false
    end
end

Do you want something like that?
slabb3r #5
Posted 30 January 2013 - 06:20 AM
The problem is that you have a long sequence inside the loop, and the condition is only verified when you go outside the loop. To call detectDiamond won't stop immediately the loop, the digForward will finish before.
Gathoc #6
Posted 30 January 2013 - 10:12 AM
The problem is that you have a long sequence inside the loop, and the condition is only verified when you go outside the loop. To call detectDiamond won't stop immediately the loop, the digForward will finish before.

Exactly.

while true do -- infinite loop
	if hold then
		-- if hold is true
	else
	   -- if hold is false
	end
end

Do you want something like that?

yea something like that
OmegaVest #7
Posted 30 January 2013 - 10:15 AM
Perhaps what you should do instead is have detectDiamond and digForward return a boolean, which you can pass to hold, so the change stays in the loop.

ie.

function detectDiamond()
   turtle.select(16)
   if turtle.compare(true) then
      return true
   elseif turtle.compareDown(true) then
      return true
   else
      turtle.select(1)
   end
end
ChunLing #8
Posted 30 January 2013 - 11:14 AM
You can write detectDiamond so that it has a boolean return as above, to be used in a conditional before digging. If you wait until the loop completes after each detectDiamonds check before it affects the turtle's actions, then of course it's going to dig out the diamonds before stopping.