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

Compare Entering Loop With Tree Mining Program

Started by leskat, 17 November 2013 - 07:08 AM
leskat #1
Posted 17 November 2013 - 08:08 AM
Edit: Sorry I screwed up the title, I should double check that next time. It should be: Compare entering loop with Tree Mining Program

Hi,

I made a program that mines 4-Block trees. in the 15 slot the turtle carries the saplings, in the 16th he carries a stack of wood for comparing. First the turtle checks if the tree grew, and if yes he starts mining it in layers. Every layer he checks if the block above him is equal to the stack of wood in slot 16. This is where it is malfunctioning.


if (turtle.compareUp() == false) then
  while (hight ~= 0) do
	turtle.down()
	hight = hight - 1
  end
  turtle.back()
  i = 1
end

Here is the full script: http://pastebin.com/Q3KfggcQ

He selects the correct slot beforehand, but when he compares with the block above him he enters the while loop and descends. I tried it out in lua mode and it returned true when I compare them, so I can't see why it enters the loop.

Thanks a lot for your help.
Edited on 17 November 2013 - 02:42 PM
Cozzimoto #2
Posted 17 November 2013 - 09:45 AM
on the bottom right corner of your OP click edit then right next to the Save Modifed Post button there should be a button named Use Full Editor

There you should be able to change you title in this topic

as for your error, just skimming over your program you have a local variable height declared within a function which means that variable can only be accessed when that function is called.
you should delcare the local variable height at the top of your program so every function within your program can access that variable instead of error of a nil value because it doesnt exist
Edited on 17 November 2013 - 08:46 AM
leskat #3
Posted 17 November 2013 - 03:44 PM
But shouldn't it work if I have it declared in the function? It doesn't give an error, it goes in to the loop even though it shouldn't.
Bomb Bloke #4
Posted 17 November 2013 - 05:50 PM
Indeed, since "hight" is only referred to within the function that declares it, that's not a problem at all.

I believe the issue has to do with the tree you're chopping up. Being a 2x2 trunk, you've got four different types of blocks there - but only one type of block in slot 16, which seemingly isn't matching whatever segment of the trunk you're putting your turtle under. Odds are if you experiment with the four different corners you'll find that only one works.
Parmacoy #5
Posted 17 November 2013 - 07:03 PM
I have had this same issue before. First of all, comparing is a really buggy process, When my 2x2 tree chopper was comparing blocks, it would sometimes screw up for no apparent reason. I went with the solution of this.

Turtle at base of tree:

grown = false
while not grown do
	turtle.moveUp()
	if turtle.detect() then
		grown = true
	end
	turtle.moveDown()
	if grown then
		turtle.dig()
		turtle.forward()
	else
		sleep(5)
	end
end
y = 1
while turtle.detectUp() do
	turtle.dig()
	turtle.digUp()
	turtle.moveUp()
	y = y + 1
end
-- No longer detecting up
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnLeft()
-- Downwards
while y > 1 do
	turtle.dig()
	turtle.digDown()
	turtle.moveDown()
	y = y - 1
end
-- Reached base
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
-- Replant saps if its renewable, unsure as to use
turtle.back()
Edited on 17 November 2013 - 06:04 PM