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

Tricky bug with while loop

Started by TechnicalParadox, 03 August 2016 - 02:52 AM
TechnicalParadox #1
Posted 03 August 2016 - 04:52 AM
I have made several programs all meant to work together, I have encountered a bug with my while loop, though the other parts of the program all function correctly. Only the GOTO command does not work. Excuse the sloppiness, I worked through many bugs along the way but I am hoping this is one of the last to have a fully working background control program to control turtles.

The sleeps and prints were so I could locate where it happened, prints during/after the while loop never displayed.

Here is the pastebin:
http://pastebin.com/bP28Hsgb

The crash happens at the while loop at 106, no error it just closes the process on my turtle.
Lupus590 #2
Posted 03 August 2016 - 04:01 PM
I'm not sure but for some reason I think that the Lua version that CC uses doesn't have goto or has it disabled.

You could put your loop into a function, then you can exit the loop with return. You may also want to check if you can use break which will skip the need for a function.
Edited on 03 August 2016 - 02:04 PM
Exerro #3
Posted 03 August 2016 - 05:22 PM
He's not trying to use a 'goto' statement, although if he were then you'd be right in saying it isn't in Lua 5.1 (what CC uses).

I think the issue might be this line:

while not id == slaveControllerID do

Lua interprets that as this:

while (not id) == (slaveControllerID) do

So, in other words…

while false == slaveControllerID do

To fix this, you can use either of the following:

while not (id == slaveControllerID) do
-- or
while id ~= slaveControllerID do

There's a few more cases of this further down in the code, so make sure to change all of them.
Edited on 03 August 2016 - 03:22 PM
TechnicalParadox #4
Posted 03 August 2016 - 08:27 PM
He's not trying to use a 'goto' statement, although if he were then you'd be right in saying it isn't in Lua 5.1 (what CC uses).

I think the issue might be this line:

while not id == slaveControllerID do

Lua interprets that as this:

while (not id) == (slaveControllerID) do

So, in other words…

while false == slaveControllerID do

To fix this, you can use either of the following:

while not (id == slaveControllerID) do
-- or
while id ~= slaveControllerID do

There's a few more cases of this further down in the code, so make sure to change all of them.

Everything works until line 106 but then the program crashes with no error just a black screen
Marcell D'avis #5
Posted 03 August 2016 - 08:30 PM
He's not trying to use a 'goto' statement, although if he were then you'd be right in saying it isn't in Lua 5.1 (what CC uses).

I think the issue might be this line:

while not id == slaveControllerID do

Lua interprets that as this:

while (not id) == (slaveControllerID) do

So, in other words…

while false == slaveControllerID do

To fix this, you can use either of the following:

while not (id == slaveControllerID) do
-- or
while id ~= slaveControllerID do

There's a few more cases of this further down in the code, so make sure to change all of them.

No, I'm pretty sure Lua interprets


while not id == slaveControllerID do
as

while not (id == slaveControllerID) do
Exerro #6
Posted 04 August 2016 - 02:53 AM
-snip-

No, I'm pretty sure Lua interprets


while not id == slaveControllerID do
as

while not (id == slaveControllerID) do

Nope, tested in a Lua 5.1 console:


> =not "hi" == "hi"
false
> =not "abc" == "hi"
false

If you were right, it'd parse the second as `not ("abc" == "hi")`, so `not false` so `true`.

Everything works until line 106 but then the program crashes with no error just a black screen

When you say it crashes, do you mean the turtle shuts down? Does it go back to the console, or does the entire screen go black? I mean there's no reason for it to crash because of that while loop condition, it should sleep and print test afterwards. Does it print "test" at all?
TechnicalParadox #7
Posted 04 August 2016 - 07:02 AM
-snip-

No, I'm pretty sure Lua interprets


while not id == slaveControllerID do
as

while not (id == slaveControllerID) do

Nope, tested in a Lua 5.1 console:


> =not "hi" == "hi"
false
> =not "abc" == "hi"
false

If you were right, it'd parse the second as `not ("abc" == "hi")`, so `not false` so `true`.

Everything works until line 106 but then the program crashes with no error just a black screen

When you say it crashes, do you mean the turtle shuts down? Does it go back to the console, or does the entire screen go black? I mean there's no reason for it to crash because of that while loop condition, it should sleep and print test afterwards. Does it print "test" at all?

I run the program on the background of the Turtle, it does everything correctly and prints out everything before the while loop as it should, but once it reaches that line it closes the background process, normally if something goes wrong it stays open and just says the error etc but it completely closes with no output.
Bomb Bloke #8
Posted 04 August 2016 - 08:31 AM
What happens if you run it in the current tab, on its own, with no other tabs open?
TechnicalParadox #9
Posted 04 August 2016 - 07:59 PM
What happens if you run it in the current tab, on its own, with no other tabs open?

Ah, ok so it must have been going into the while loop but since I had the sleep before it printed test, I didnt get to see that it prints test and then crashes.

Error is line 111
"attempt to compare string with number expected, got string"
Bomb Bloke #10
Posted 05 August 2016 - 02:52 AM
In that case, double check that your paste matches what's on the turtle. I'm not seeing any way in which either of the two variables referenced on that line could become a string - at least, not within the code provided.

Unless you got the line number wrong? gotoX / gotoY / gotoZ could be a string, in which case the code on your other computer would be at fault…
TechnicalParadox #11
Posted 05 August 2016 - 08:17 AM
In that case, double check that your paste matches what's on the turtle. I'm not seeing any way in which either of the two variables referenced on that line could become a string - at least, not within the code provided.

Unless you got the line number wrong? gotoX / gotoY / gotoZ could be a string, in which case the code on your other computer would be at fault…

Yes the code is the same. The code on the other computer is very simple for example, for the GOTO command, the user would enter "CONTROL [turtle rednet id] GOTO [x] [y] [z]" where control is the name of the program. The program then sends the last 4 arguments to the turtle one at a time, waiting for a response in between. I checked and the coordinates were being received correctly. In all honesty I am not too worried about the error as I planned on remaking all my programs to be more optimized etc. The whole thing was kind of a learning experience (I didn't know lua syntax or how to do much with it).

EDIT: I do see what you mean with the gotoX/Y/Z being a string. It probably is since its just the message from rednet. Ill try using tonumber() to fix it and get back

EDIT 2: YES! That was the issue, gotoX/gotoY/gotoZ were strings and it was throwing everything off. Thank you so much!
Edited on 05 August 2016 - 06:50 AM