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

Problem with a while loop

Started by Hishighness, 05 October 2015 - 04:18 PM
Hishighness #1
Posted 05 October 2015 - 06:18 PM
Hey all, I've got a mining shaft program on my turtle but the timing gets messed up by gravel, so I've tried making it so it detects gravel and clears it before continuing but for some reason it gets stuck in the while loop when it detects the gravel and even when the gravel is gone it won't go out of the loop. I've confirmed this by adding a variable and incrementing it and printing it out. I also set it to print the data.name and it reads gravel for as manay gravel as there are to be mined but then it just spits out emptyness.

I can't figure out what I'm doing wrong, I create the while loop, set the condition and then have it recheck the condition each time it loops then set it to end when it's false.

Anyway, here's my code.


local success, data = turtle.inspect()
  while data.name == "minecraft:gravel" do
  turtle.dig()
  local success, data = turtle.inspect()
  sleep (0,10)
  end
turtle.forward

Thanks for your time.
TYKUHN2 #2
Posted 05 October 2015 - 07:45 PM
Remove the local before the second success,data assignment. See if that helps. I suspect it may never be changing the data variable, but I may be wrong.

Edit: sleep(0,10) only does sleep(0.05)
Edited on 05 October 2015 - 05:54 PM
Dustmuz #3
Posted 05 October 2015 - 08:56 PM

function fallingblocks()
        local success, data = turtle.inspect()
        local successa, dataa = turtle.inspectUp()
        local fBlocks =
                {
                ["minecraft:sand"] = true,
                ["minecraft:gravel"] = true
                }
        if fBlocks[data.name] or fBlocks[dataa.name] then
                return true
        else
                return false
        end
end


while fallingblocks() == true do
  turtle.dig()
  turtle.digUp()
  sleep(1)
end

those 2 are the ones i use, and it never fails for me with the program i have :)/>
Hishighness #4
Posted 05 October 2015 - 11:14 PM
That did it, thanks. :)/>
Dustmuz #5
Posted 06 October 2015 - 08:54 AM
Youre welcome :)/>