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

I need help with my turtle programm

Started by darkwarecookie, 22 October 2018 - 04:33 PM
darkwarecookie #1
Posted 22 October 2018 - 06:33 PM
I made the turtle is just for experimental reasons. The turtle should go through a small labyrinth. In order to do that, it should check: if there is not block in front it should go forward, else it should go left, but after I tried it this message popped up: l.lua:30: attempt to comparte __lt on nil and nil.
Code:
local x = 0
local y = 0
local z = 0
turtle.down()
if turtle.detect() then
local x = 1
else x = 0
end
if turtle.turnLeft() then
local y = 1
else y = 0
end
turtle.turnRight()
turtle.turnRight()
if turtle.detect() then
local z = 1
else z = 0
end
if x == y then
local a = 1 else
local a = 0
end
if x == z then
local b = 1 else
local b = 0
end
if a == b then
turtle.forward()
end
if a > b then
turtle.turnLeft()
turtle.forward()
end
if b > a then
turtle.turnRight()
turtle.forward()
end

PS. I don't have much knowledge about progamming, I just read the ComputerCraft Introductions
Bomb Bloke #2
Posted 23 October 2018 - 12:34 AM
The error is telling you that you're attempting to compare two incompatible values on the 30th line. "a" and "b" will be nil when the script reaches that point.

The reason for this is that you're localising them to the "if" blocks higher up, which means their values are discarded at the end of those blocks. One way around this would be to define them as upvalues - see this scope tutorial for more info on this.

You might also find this control structure tutorial handy, as at some stage you'll want to add some loops to the code.