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

Is this code correct?

Started by MightyCoollego, 20 October 2014 - 06:34 PM
MightyCoollego #1
Posted 20 October 2014 - 08:34 PM
Hi, I am trying to make a Felling Turtle chop a tree down but, the code has an error that I can't find.



turtle.refuel()
while turtle.detect() do
   turtle.dig()
   turtle.up()
   turtle.dig()
   turtle.digUp()
   turtle.up()
   turtle.dig()
   turtle.digUp()
   turtle.up()
   turtle.dig()
   turtle.digUp()
   turtle.up()
   turtle.dig()
   turtle.digUp()
   turtle.up()
   turtle.dig()
   turtle.down()
   turtle.down()
   turtle.down()
   turtle.down()
end

Does anybody know how to fix this code? Thank you.
TheOddByte #2
Posted 20 October 2014 - 09:25 PM
You need to specify more what problem you're getting, are you getting an error code? If so then I'd suggest you'd post it.
And another suggestion, use loops. http://www.lua.org/pil/4.3.4.html

for i = 1, 4 do
    turtle.down()
end
valithor #3
Posted 20 October 2014 - 09:26 PM
Are you receiving a error when you run it? If so what is it? Or is it just not doing what you expect it to do?

edit: ninja'd
Edited on 20 October 2014 - 07:27 PM
Dragon53535 #4
Posted 20 October 2014 - 09:27 PM
Is your error the fact that it only runs once? The way your code is, is that if it sees a block infront, then do that entire thing, and then check again for a block in front and if it's there do it again, so on and so forth. However since you're digging all the blocks in front, then there's not going to be a block to detect at the end.
Edited on 20 October 2014 - 07:28 PM
MightyCoollego #5
Posted 21 October 2014 - 03:32 AM
I am not getting an error message, nothing happens when I run the program.
Bomb Bloke #6
Posted 21 October 2014 - 04:06 AM
What do you mean by "nothing happens"? Be precise! Something will happen.

It could be that the turtle stalls.

It could be that you're immediately returned to the command prompt.

Reading the code, it looks to me like the script will immediately end unless there's a block immediately in front of the turtle when you run it. Think about the behaviour you want, and describe how the behaviour you're getting differs from that.
valithor #7
Posted 21 October 2014 - 01:18 PM
Did you make sure that the turtle has fuel? If it doesn't then it will not move.
TheOddByte #8
Posted 21 October 2014 - 08:34 PM
I believe that Bomb Bloke probably has the answer for your problem, when you say that "nothing happens" I believe it's because you're trying to run that code without any block infront of it, and the way you set the behaviour of the program it will continue until there's no block directly infront of it.
Meaning that no block infront=nothing happens.
Cycomantis #9
Posted 22 October 2014 - 02:56 AM
Here is an example of how to achieve what I believe you are trying to do. You may have to tweak the for loops to run the correct amount of times that you want them to.

turtle.refuel()
while true do --infinite loop
  if turtle.detect() then --check to see if there is a block infront of the turtle
    for y=1, 5 do
	  turtle.dig()
	  turtle.digUp()
	  turtle.up()
    end    
    turtle.dig()
    for x=1, 5 do --changed to 5 since you moved up 5 times.
      turtle.down()
    end
  end
  sleep(1) --so your not constantly running the if statement, remove or adjust as you see fit.
end