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

2 variables are equal, but Lua doesn't think so

Started by capnstoobie, 18 June 2016 - 05:42 AM
capnstoobie #1
Posted 18 June 2016 - 07:42 AM
Hope someone can help me with this maddening problem.

I am trying to write a program that will make a turtle construct a platform, based on the length and width entered by the user. The turtle is supposed to "snake" back and forth, placing blocks, until the platform is complete.

In the program, an IF statement tests the platform_length variable against a counter, and when they equal it should execute the "first turn". My problem is that Lua never believes those 2 variables to be equal, even though a print statement I put in there for debugging shows that they are!

The program does execute the "second turn", which is a similar expression, so I can't work out why my "first turn" is not working. The code is simple, so I can only think I'm missing a Lua fundamental?

The program is here: http://pastebin.com/D8EtHNpr

thanks in advance if you can help!
Bomb Bloke #2
Posted 18 June 2016 - 08:52 AM
print(type(turncounter),type(platform_length))

write("Enter width: ")
local platform_width = tonumber(read())
write("Enter length: ")
local platform_length = tonumber(read())
capnstoobie #3
Posted 18 June 2016 - 09:49 AM
Fantastic - thanks for your help! What confused me was that "second turn" worked and "first turn" didn't. It's all working now with that simple change - thanks :)/>
theoriginalbit #4
Posted 18 June 2016 - 02:01 PM
Just to add a little explanation behind what Bomb Bloke showed there.

When you get input from the user it will always be a string, it is up to you to convert it to a number. The problem you had was you were comparing a string to a number, and "2" is never equal to 2 unless they're the same data types.
capnstoobie #5
Posted 19 June 2016 - 05:03 AM
Thanks!