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

Please help!

Started by RoyalKingZB, 13 October 2014 - 12:04 AM
RoyalKingZB #1
Posted 13 October 2014 - 02:04 AM
Hi. Ive been trying to program a mining turtle to mine a 2x1 tunnel. Ive tried to program it to place torches, place blocks underneith it as it goes and I tried to make it go to a chest when its done and put everything in it and then to move on to the next tunnel. Ecerytime I try to mine a certain distance long tunnel, it doesnt mine the amount of blocks I want and when it tries to go back to the chest, it doesnt make it the whole way there. Heres the pastebin link:
Http://pastebin.com/CMFGZLDR
Please Help!
AssossaGPB #2
Posted 13 October 2014 - 04:08 AM
Well, your first problem is your using the variable "i" for multiple for loops. Make sure to use a different variable for each loop. Also having a loop like this:

for i = 1, 1 do
    --Codez
end
Is unnecessary, you can just remove that for loop since it's only looping once.

I'll look over your code again tomorrow. But that's mainly what I've found. Also the turtle only returns to the chest partway because it runs out of fuel most likely.
Bomb Bloke #3
Posted 13 October 2014 - 06:29 AM
Well, your first problem is your using the variable "i" for multiple for loops. Make sure to use a different variable for each loop.

While it's a bad idea to use the same variable for nested loops (because it makes for a confusing mess to read), because the counter variable for a given "for" loop is automatically localised to that loop, it's technically valid code.

This block of code here:

     while not turtle.forward() do --Here is where I've tried to add the gravel remove part.
     turtle.dig()
     end

     turtle.dig()
     turtle.digUp()
     turtle.forward()
     turtle.digUp() 

… will cause the turtle to first attempt to move forward (digging until it succeeds), dig some more, then attempt to go forward again (without caring whether it succeeds). The end result is that it'll either go forward once or twice (usually twice, assuming there isn't any gravel to worry about).

Remember that each line of code runs in order, and the script won't carry on from a loop until that loop is complete. There's no point in telling the turtle to place x amount of blocks underneath itself all in one loop. Move one, place one, then let the loop repeat.

If you're still not getting the logic to loops, write a script without them which makes the turtle dig a five block passage then return. Once you've done that, you should notice you've written one bit of code, repeated it five times, then written another bit and repeated that five times as well. All you need to do is then put those two blocks into two different loops, and you're done. :)/>
RoyalKingZB #4
Posted 13 October 2014 - 08:04 PM
Wow you guys are very helpful!! Thank y'all so much!! I'll reply again if it starts working :)/>
RoyalKingZB #5
Posted 13 October 2014 - 10:26 PM
Hey I got it to mostly work. Its all working fine except the distance that I type in for it to go isnt getting read. Its just going on infinitely I think. And it also wont place blocks underneith it as it goes. Can you help me find out why? Here's the updated link:
Http://pastebin.com/Zrf2gCUn


I hope you can help :)/>
Edited on 13 October 2014 - 08:33 PM
Bomb Bloke #6
Posted 14 October 2014 - 01:08 AM
Here's a basic pair of loops that'll make a pre-fuelled turtle dig a 2-by-whatever tunnel while placing blocks under itself, then return:

local function forward()
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
  end
end

local distance = tonumber(read())

for i=1,distance do
  forward()
  turtle.placeDown()
end

turtle.turnLeft()
forward()
turtle.turnLeft()

for i=1,distance do
  forward()
end

Try expanding on that.
RoyalKingZB #7
Posted 14 October 2014 - 07:49 PM
Hey. I actually got it all working before I looked at that message but thank you very much!!
RoyalKingZB #8
Posted 14 October 2014 - 09:55 PM
Omg its not working again. The entire all loop isnt working! Help! Pastebin link:
Http://pastebin.com/msVDnu6e
KingofGamesYami #9
Posted 14 October 2014 - 10:18 PM

local function forward()
  while not turtle.forward() do turtle.dig() end
end

local dist
repeat
  dist = tonumber( read() )
until dist

for i = 1, dist do
  repeat until not turtle.digUp()
  if not turtle.detectDown() then
    turtle.placeDown()
  end
  forward()
end
print( 'returning' )
turtle.select( 16 ) --#if there are torches in slot 16
for i = 1, dist do
  if i % 10 == 0 then
    turtle.place()
  end
  turtle.back()
end
I'm hoping this does what you need. I thought your code looked a bit long for this.
RoyalKingZB #10
Posted 14 October 2014 - 10:33 PM
Hey KingOfGames. I got everything working except the facts that it completely ignores the all loop. Can you look at this and see why its skipping the all link please?
Http://pastebin.com/msVDnu6e
AssossaGPB #11
Posted 15 October 2014 - 02:35 AM
Well, your first problem is your using the variable "i" for multiple for loops. Make sure to use a different variable for each loop.

While it's a bad idea to use the same variable for nested loops (because it makes for a confusing mess to read), because the counter variable for a given "for" loop is automatically localised to that loop, it's technically valid code.

Wow, I did not realize that. Makes sense though