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

Problem with my tree cutter

Started by Klausar, 29 September 2012 - 01:31 PM
Klausar #1
Posted 29 September 2012 - 03:31 PM
I have problems with my tree cutter, I want it to stop at the typed in amount. For example: If you type in 5 for x it should stop after 5 trees, but it doesn't. Here ist the code:

Spoiler
local i = 1
print("How many trees do you want to cut down?")
local x = io.read()
repeat
turtle.select(2)
turtle.place()
sleep(1)
turtle.select(1)
turtle.place()

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.digUp()
turtle.up()

turtle.dig()
turtle.digUp()
turtle.up()

turtle.dig()
turtle.digUp()

turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
i = i + 1
until i == x

jag #2
Posted 29 September 2012 - 03:51 PM
Can you take a picture of the turtle's inventory?
So we can see what's in slot 1 and 2 (because you use that in your code)
And do you get any error? (in case you do, what are the error?)
What's the major problem? Does it cut down 6 trees instead of 5 or what?
jag #3
Posted 29 September 2012 - 03:55 PM
Does you turtle got any fuel?
And at the far bottom, it says
i = i + 1
until i == x
So if you say that x = 5, and ↑ there it says increase i by one.
So at this state, x is 5 and i is 2.
Then your code checks if i == x.
So ~= would work better!

If i ~= (does not equal to) x then repeat the code agian.

EDIT: derp derp, I forgot how the repeat until loop works :/ i == x is the right way of doing it
Anonomit #4
Posted 29 September 2012 - 03:56 PM
I have problems with my tree cutter, I want it to stop at the typed in amount. For example: If you type in 5 for x it should stop after 5 trees, but it doesn't. Here ist the code:

Spoiler
local i = 1
print("How many trees do you want to cut down?")
local x = io.read()
repeat
turtle.select(2)
turtle.place()
sleep(1)
turtle.select(1)
turtle.place()

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.digUp()
turtle.up()

turtle.dig()
turtle.digUp()
turtle.up()

turtle.dig()
turtle.digUp()

turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
i = i + 1
until i == x


calling read() returns a string.
Try this:

Spoiler

local x = nil
while x == nil do
    term.clear()
    term.setCursorPos(1,1)
    print("How many trees should be cut down?")
    x = tonumber(io.read())
end
term.clear()
term.setCursorPos(1,1)
print("Cutting down "..x.." trees")
for i = 1, x do
    turtle.select(2)
    turtle.place()
    sleep(1)
    turtle.select(1)
    turtle.place()
    while turtle.detect() do
        turtle.dig()
        turtle.digUp()
        turtle.up()
    end
    while not turtle.detectDown() do
        turtle.down()
    end
end
term.clear()
term.setCursorPos(1,1)
print("Cut down "..x.." trees.")

EDIT: The problem was that you were trying to compare a string and a number on the line:
until i == x
but,
i = 5
x = "5"

5 will never = "5"

Edit: Fixed typo in code causing infinite loop
Klausar #5
Posted 29 September 2012 - 04:05 PM
Thanks you really! But I get the following error with your code: Tree:7: attempt to call nil

Edit: Fixed it, thank you very much!
Edit: Now it always asks me howmany trees I want to cut down but it doesn't start
jag #6
Posted 29 September 2012 - 04:07 PM
You could make this code much easier like this:
Spoiler
local x = 0
print("How many trees do you want to cut down?")
x = read()


for i = 1, x do
  turtle.select(2)
  turtle.place()
  sleep(.2)
  turtle.select(1)
  turtle.place()

  turtle.dig()
  turtle.forward()

  while turtle.detectUp() do
	turtle.digUp()
	turtle.up()
  end
  while not turtle.detectDown() do
	turtle.down()
  end

  turtle.back()
end
Klausar #7
Posted 29 September 2012 - 04:10 PM
You could make this code much easier like this:
Spoiler
local x = 0
print("How many trees do you want to cut down?")
x = read()


for i = 1, x do
  turtle.select(2)
  turtle.place()
  sleep(.2)
  turtle.select(1)
  turtle.place()

  turtle.dig()
  turtle.forward()

  while turtle.detect do
	turtle.digUp()
	turtle.up()
  end
  while not turtle.detectDown() do
	turtle.down()
  end

  turtle.back()
end

Your code works fine, but it keeps going up, it doesn't go down :)/>/>
Anonomit #8
Posted 29 September 2012 - 04:14 PM
Thanks you really! But I get the following error with your code: Tree:7: attempt to call nil

Edit: Fixed it, thank you very much!
Edit: Now it always asks me howmany trees I want to cut down but it doesn't start

Try the code now. I fixed the typo.
Klausar #9
Posted 29 September 2012 - 04:17 PM
Thanks! Now it really works.
jag #10
Posted 29 September 2012 - 06:59 PM
Your code works fine, but it keeps going up, it doesn't go down :)/>/>
It does! It goes up as long as there's a block above it!
Klausar #11
Posted 29 September 2012 - 07:03 PM
Your code works fine, but it keeps going up, it doesn't go down :(/>/>
It does! It goes up as long as there's a block above it!

Well, for me it flew up to Y=200 :)/>/>
jag #12
Posted 29 September 2012 - 08:23 PM
Your code works fine, but it keeps going up, it doesn't go down :(/>/>
It does! It goes up as long as there's a block above it!

Well, for me it flew up to Y=200 :)/>/>
Wow, well that's strange… It should just mine the tree and then come back down again…