Jump to content




Problem with a while loop


4 replies to this topic

#1 Hishighness

  • New Members
  • 2 posts

Posted 05 October 2015 - 05: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.

#2 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 05 October 2015 - 06: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 by TYKUHN2, 05 October 2015 - 06:54 PM.


#3 Dustmuz

  • Members
  • 174 posts
  • LocationDenmark

Posted 05 October 2015 - 07: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 :)

#4 Hishighness

  • New Members
  • 2 posts

Posted 05 October 2015 - 10:14 PM

That did it, thanks. :)

#5 Dustmuz

  • Members
  • 174 posts
  • LocationDenmark

Posted 06 October 2015 - 07:54 AM

Youre welcome :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users