r/ComputerCraft • • Apr 05 '21

Trouble with turtle.detect() command

Hello all. I'm having some trouble with the turtle.detect() command in the modpack Project Ozone 2 (Minecraft 1.7). Here is the code:

if turtle.detect() then

print("yes")

else

print("no")

end

Running the code does nothing at all, though it should output to the console one of either 'yes' or 'no', correct? I've tried making a completely new advanced turtle with just that program in it, still doesn't work. What am I doing wrong? All the other commands in my larger script are working, it's just turtle.detect() and turtle.inspect() that are behaving oddly.

3 Upvotes

9 comments sorted by

2

u/SadieWopen Apr 05 '21

The code looks good, what happens when you use turtle.detect() in the Lua console?

Another thing to try is something like this:
while not turtle.detect() do print("nothing") end
print("something")

just to see what is going on.

1

u/hengwozzle Apr 06 '21

Thank you for your answers.

I couldn't try turtle.detect() in the Lua console, because despite my research I couldn't find out how to bring up the console!

I tried the code snippet you suggested above and if there is a block in front of the turtle, the program has no effect and no text is printed at all. If there is not a block there, the program repeatedly prints "nothing" until I put a block in front, and then it does nothing further. In both cases it does not print "something".

I tried this code:

if turtle.detect() then print("detected") end

turtle.turnLeft()

print("finished")

And the turtle does turn left but does not print "finished", regardless of whether there's a block in front.

1

u/SadieWopen Apr 06 '21 edited Apr 06 '21

To get to the Lua console, just type lua and press enter (like you would any other program.

It sounds like the print() function is what is the problem here. I wonder if it just doesn't print the first time you use it. Try this code to see if this is the case

for i=1,4,1 do print(i) end

While we're at it, lets modify my little snippet from before so we can get better feedback

while not turtle.detect() do print("nothing") end
turtle.turnLeft()

1

u/hengwozzle Apr 07 '21

while not turtle.detect() do print("nothing") end

Hello Sadie,

When in the lua console and I run this line:

if turtle.detect() then print("detected") end print("finished")

... it works as expected: if there's a block in front, prints "detected" and "finished"; if there's no block, just "finished". The same line pasted into a new program yields no result at all, regardless of whether there's a block in front. My impression of ComputerCraft is that if there's any output from a call to the print function, then it shows the console output with a "press any key" message. This temporary console output screen isn't appearing. But perhaps output has been generated, but the console screen isn't appearing?

I tried your snippet above, and it did indeed print "nothing" to fill the screen. Then when I exited the turtle and placed a block in front of it, the turtle turned.

Note that with this program, there's no print output either:

print("starting")

if turtle.detect() then x=3 end

But getting rid of the 2nd line means that the word "starting" does appear as expected. There's a glitch I suppose in my installation of ComputerCraft. Unless as I said above the output screen isn't appearing when it should.

1

u/SadieWopen Apr 07 '21

Okay, let's try replacing print() with term.write() and see if that works.

1

u/hengwozzle Apr 07 '21

Same as before! Any previous call to turtle.detect() prevents term.write() from working.

1

u/SadieWopen Apr 07 '21

Could you share a screenshot of your setup, I want to try replicate your issue on my side.

1

u/hengwozzle Apr 09 '21

Here's the setup: just an advanced turtle next to a block. Also have screenshot of the program.

https://imgur.com/a/hqpkCmJ

3

u/Immow Apr 06 '21 edited Apr 06 '21

On the wiki is this code at it does almost the same you posted

local result = turtle.detect() 
if result then 
    print("There is a block in front of the turtle!") 
else 
    print("There is no block in front of the turtle, only air or a liquid.") 
end