Problem: ifs won't trigger and my turtle does not turn as intended.
What's happening: Run code once, works as intended. Run code a second time: suddenly it's like if statements no longer function?
My turtle loads up it's position and facing from a file, and is told to move south. It's told it face south, the function used to face a direction is triggered. I print out the input for debugging, the current face is 1 (north) and the intended direction is 3(south). Though handled in my if statement, it does not turn and desyncs, the program glosses over the if clause and just executes the else clause. This is my problem, the ifs won't trigger and my turtle does not turn.
EDIT: As long as my first turn is 90 degrees I can do 180 degree turns as I please, but if the first turn I try to make is 180 degrees, it gets confused.
Relevant code:
(for reference: 1 = north, 2 = east, 3 = south and 4 = west. the function is called with face(3) when the problem occurs)
function face(newF)
dif = 0
print("I'm facing " ..f)
print("I was told to face: " .. newF)
if f == 1 and newF == 3 or f == 3 and newF == 1 or f == 4 and newF == 2 or f == 2 and newF == 4 then
print("I decided to turn right twice")
turnRight()
turnRight()
else
dif = f - newF
print("dif: " .. dif)
if dif == 3 or dif == -1 then
turnRight()
elseif dif == -3 or dif == 1 then
turnLeft()
elseif not dif == 2 or diff == -2 then
turnRight()
turnRight()
else
print("Did not turn or something went wrong?")
end
end
end
function updateFaceDir(i)
f = f + i
if f == 5 then
f = 1
elseif f == 0 then
f= 4
end
end
function turnRight()
turtle.turnRight()
updateFaceDir(1)
end
function turnLeft()
turtle.turnLeft()
updateFaceDir(-1)
end
The first if statement is supposed to see when two turns are needed to face the direction as opposed to only needing one turn which is handled in the else clause. As the input (newF) is 3, and the current face (f) is 1, it should be caught right away in the first if and turn twice. Instead the else clause is triggered. Later on while debugging I tossed in some redundancy (elseif dif == 2 or dif == -2 then …) to catch this specific case (dif is equal to -2) and it doesn't trigger the clause, instead it moves on the else clause again and prints "did not turn, or something went wrong?" and the function ends.
Sceenshot:
As far as I can see there's no reason that it's just not triggering my two ifs and just going right to the else clauses so I dunno what's going on. Any help would be greatly appreciated and i'm sorry if this post is a bit long winded, i tried to cut it down the best I could :x