2 posts
Posted 19 December 2013 - 03:05 PM
I am having issues with programming my turtle. This is what i want the turtle to do: move down, if you cannot, then move up. Now my code is set up like this:
turtle.down()
if turtle.down() = false then turtle.up()
It is very simple but does not work, i get an error saying that the 'then' is expected on line 2. I have a then there so what is going wrong? I am some-what experimenting with using the Boolean values of the turtle.moves and if I can get it to work that would be fantastic!
7508 posts
Location
Australia
Posted 19 December 2013 - 09:49 PM
This is because in most programming languages a single = means that you wish to set the variable on the left to the value on the right. Using two = is used for comparison. Example:
local loopCount = 0 --# this sets a variable `loopCount` to 0
if loopCount == 1 then --# this is asking "is `loopCount` equal to 1"
print("Loop count was 1")
elseif loopCount == 2 then
print("Loop count was 2")
else
print("Loop count was neither 1, nor 2")
end
However do also note that when checking for false you can use the keyword
not to invert the result, so true becomes false and false becomes true.
if not turtle.down() then
print("could not move down")
end
7083 posts
Location
Tasmania (AU)
Posted 19 December 2013 - 10:27 PM
It's also worth noting that in order to check the value of turtle.down(), that function must be executed:
turtle.down() -- Tries to move down. Ignores the result.
if turtle.down() == false then turtle.up() end -- Tries to move down AGAIN, then uses the result to determine whether to move up.
In short, that first line is redundant.
19 posts
Posted 19 December 2013 - 11:17 PM
When you have an if statement you have to use two equal signs
if true == true then
function()
end
also you move down twice. When you do turtle.down() that moves it and when you do if turtle.down() it moves it again.
You also don't need to do
if turtle.down() == false then
You can do
if not turtle.down() then
but you don't have to. So here is the code you are trying to make
if not turtle.down() then
turtle.up()
end
:)/>
EDIT:
If you want it to keep moving down until it can anymore you can do this too
while true do
if not turtle.down() then break end
end
Edited on 19 December 2013 - 10:20 PM
7508 posts
Location
Australia
Posted 19 December 2013 - 11:39 PM
EDIT:
If you want it to keep moving down until it can anymore you can do this too
while true do
if not turtle.down() then break end
end
or just this
while turtle.down() do end