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

[lua]If statement troubles?

Started by Leaddier, 10 January 2013 - 10:40 AM
Leaddier #1
Posted 10 January 2013 - 11:40 AM
Likely important note: I'm using whatever verison of computercraft that is bundled with Tekkit Lite but I think this is a lua issue and not a computercraft issue anyway

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
crazyguymgd #2
Posted 10 January 2013 - 12:15 PM
what is f initialized to? And where is it initialized?
Leaddier #3
Posted 10 January 2013 - 12:29 PM
f is initialized to 0 and at the beginning of the file where I assume is just the global scope? It's later set by the function which reads the last know facing of the turtle from a file

Edit: wow sorry I didn't notice it took me so long to respond, i was debugging the mining portion of the scrip (completely unrelated in every way to this issue) and lost track of time
remiX #4
Posted 10 January 2013 - 12:35 PM


  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

Read it carefully :P/> then open spoiler…

Spoiler
Spoiler
Spoilerelseif not dif == 2 or diff == -2 then – change 'diff' to 'dif'
Leaddier #5
Posted 10 January 2013 - 12:40 PM
Hah okay, been staring at a screen too long I guess, I added an f there by mistake, whoops!

But why doesn't the first if trigger? I don't see an issue in that


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

this part
remiX #6
Posted 10 January 2013 - 12:44 PM
Add this before the if statement and tell us what is the outcome:


print(type(f))
print(type(newF))
Leaddier #7
Posted 10 January 2013 - 12:58 PM
f is a string… I'll see myself out now

I didn't even to think to check if they were both numbers and I've been programming for 4 - 5 years now :x

Thank you,
remiX #8
Posted 10 January 2013 - 01:00 PM
f is a string… I'll see myself out now

I didn't even to think to check if they were both numbers and I've been programming for 4 - 5 years now :x

Thank you,

Yeah debugging is a main part in coding.

How did f become a string? Did you initialize it like this?

f = ""
Leaddier #9
Posted 10 January 2013 - 01:06 PM
I initialized it as


f = 0

but later on I did

f = string.sub(tempString, index3+1, index3+1)
to parse it out of the text I pulled from a file. (yeah.. the whole index3+1 thing is sloppy coding.. shush)

I added "+0" to the end of that and it works just fine now.. That's probably a sloppy fix too, come to think of it, but oh well
ChunLing #10
Posted 10 January 2013 - 09:25 PM
Yeah, the tonumber() is cheaper than a +0 in this case, since you are only using the addition to force a tonumber conversion on the string. But not a lot cheaper, and I got's to admit that +0 looks more compact.