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

[lua] turtle compare statements not working as expected

Started by sm31415, 10 January 2013 - 10:13 AM
sm31415 #1
Posted 10 January 2013 - 11:13 AM
I am trying to code a program that will plant saplings for 2x2 fir tree from the xl biomes mod (im in the ftb mindcrack pack, latest update)

i have saplings for it in slot 1,

this is my code at the moment.


while true do
turtle.select(1)
turtle.down()
if turtle.compare() ~= "result" and turtle.detect ~= "result" then
turtle.up()
turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.down()
end
turtle.up()
if turtle.detect() == "result" then
shell.run("firtree")
turtle.turnRight()
for i = 2,16 do
turtle.select(i)
turtle.drop()
end
turtle.turnLeft()
end
if redstone.getInput("back") == "true" then
break
end
sleep(10)
end

if turtle.compare() ~= "result" and turtle.detect ~= "result" then

this line, which compares the block in front, to the saplings, and also checks to see if the space is empty,

only if it not saplings, and empty, should it do the bit after, which involves planting the saplings, but this is not the case

it plants saplings regardless, or rather tries to, even when the tree has grown, which breaks its positioning.

also i seem to be unable to compare the fir wood i get,

the shell.run("firtree") is an established program i wrote, that will completely harvest the wood of a 2x2 tree.

thank you for reading, extra thanks for any possible help you may render
crazyguymgd #2
Posted 10 January 2013 - 11:20 AM
it's turtle.detect() not turtle.detect
hopefully that's your only problem
ChunLing #3
Posted 10 January 2013 - 09:40 PM
turtle.compare() returns a boolean, it will never not be unequal to the string "result", turtle.detect is a function, it will never not be unequal to anything other than itself (meaning it is also always unequal to "result").

The return of turtle.detect is also a boolean, which will never not be unequal to the string "result". So what you want is probably something more like:
if not (turtle.compare() or turtle.detect()) then
sm31415 #4
Posted 11 January 2013 - 03:46 AM
it's turtle.detect() not turtle.detect
hopefully that's your only problem

Ah thank you i will change that.

turtle.compare() returns a boolean, it will never not be unequal to the string "result", turtle.detect is a function, it will never not be unequal to anything other than itself (meaning it is also always unequal to "result").

The return of turtle.detect is also a boolean, which will never not be unequal to the string "result". So what you want is probably something more like:
if not (turtle.compare() or turtle.detect()) then

but i thought turtle compare was meant to look at the block in the selected slot, and see if it was the same as the one in front of it,

if it always returned result surely the command would be pointless, or do i need to have if it is returning true or false?
Orwell #5
Posted 11 January 2013 - 03:48 AM
it's turtle.detect() not turtle.detect
hopefully that's your only problem

Ah thank you i will change that.

turtle.compare() returns a boolean, it will never not be unequal to the string "result", turtle.detect is a function, it will never not be unequal to anything other than itself (meaning it is also always unequal to "result").

The return of turtle.detect is also a boolean, which will never not be unequal to the string "result". So what you want is probably something more like:
if not (turtle.compare() or turtle.detect()) then

but i thought turtle compare was meant to look at the block in the selected slot, and see if it was the same as the one in front of it,

if it always returned result surely the command would be pointless, or do i need to have if it is returning true or false?
Indeed, it never returns "result" (I don't know where you'd get that from :P/>), it returns true when the block in front is the same as in the selected slot and false when it isn't.
theoriginalbit #6
Posted 11 January 2013 - 03:49 AM
turtle.compare() returns true/false if it matches or not. there is no need to use == true or == false in an if statement…
sm31415 #7
Posted 11 January 2013 - 04:39 AM
it's turtle.detect() not turtle.detect
hopefully that's your only problem

Ah thank you i will change that.

turtle.compare() returns a boolean, it will never not be unequal to the string "result", turtle.detect is a function, it will never not be unequal to anything other than itself (meaning it is also always unequal to "result").

The return of turtle.detect is also a boolean, which will never not be unequal to the string "result". So what you want is probably something more like:
if not (turtle.compare() or turtle.detect()) then

but i thought turtle compare was meant to look at the block in the selected slot, and see if it was the same as the one in front of it,

if it always returned result surely the command would be pointless, or do i need to have if it is returning true or false?
Indeed, it never returns "result" (I don't know where you'd get that from :P/>), it returns true when the block in front is the same as in the selected slot and false when it isn't.

The turtle api page on the wiki

probably me misunderstanding it lol

turtle.compare() returns true/false if it matches or not. there is no need to use == true or == false in an if statement…

having updated it with this in mind, it now works perfectly,

it plants the saplings, then checks every 10 seconds to see if the tree has grown, by going up a square and turtle detecting

if theres a tree there it cuts it down, then plants more saplings,

water and an obisidian pipe transport it to a chest

Thank you for your help all :)/>