4 posts
Posted 05 January 2013 - 01:57 PM
Ok i've been staring at this program for hours now and just cannot figure out why it will not exit the "while" loop when the program is run?? I've only been learning computercraft/lua for a day so i'm hoping its just me missing something obvious.
I know its getting stuck in the first while loop in digMoveR() because I have tested (using "print("curL: "..curL)") after each move and that should stop at 5 but it keeps incrementing.
I have also tried enclosing the "curL ~= length" in brackets but that also made no difference.
I am running computercraft in the latest build of Tekkit (not FTB) so I dont know if this is a bug in the older version or something.
Any help will be hugely appreciated :D/>
length = 5
width = 5
curL = 0
curR = 0
function dig()
if turtle.detect() then
turtle.dig()
turtle.forward()
else
turtle.forward()
end
end
function digMove()
dig()
curL = curL + 1
end
function digMoveR()
while curL ~= length do
digmove()
end
if curW ~= width then
turtle.turnRight()
dig()
turtle.turnRight()
curW = curW + 1
curL = 0
end
end
digMoveR()
127 posts
Posted 05 January 2013 - 04:05 PM
try replaceing the while loop with this
while true do
if curL == length then break end
digmove()
end
This will end the while statement when curl is at 5
2005 posts
Posted 05 January 2013 - 05:46 PM
Yeah…but the code should already do that.
Try with "curL < length", == can be a bit fickle.
2088 posts
Location
South Africa
Posted 05 January 2013 - 11:27 PM
why not just use a simple for loop?
It's best for codes like this, in my opinion.
for i = 1, length do
digmove()
end
818 posts
Posted 05 January 2013 - 11:32 PM
Really, using while loops for increment looping, is like using the heap for random variable storing in c++, it's unnessesary.
2005 posts
Posted 06 January 2013 - 03:17 AM
True, and the for loop automatically implements the < test rather than equality. Still, I want to get an idea of exactly why this code isn't working.
127 posts
Posted 06 January 2013 - 03:58 AM
I just tested this out in a test world all worked fine besides 3 things, you were trying to run digmove() when you defined the function as digMove(), gotta watch them caps :)/> when u were declaring curL and curR at top you didnt define curW so i assumed the curR was a mistake and changed it to a W, and thirdly you dont want to use while ~= because everything below 5 would count and everything after it also , this last is not that big of a deal but is something to watch for.
length = 5
width = 5
curL = 0
curW = 0
function dig()
if turtle.detect() then
turtle.dig()
end
turtle.forward()
end
function digMove()
dig()
curL = curL + 1
end
function digMoveR()
for i = 1, length do
digMove()
end
if curW ~= width then
turtle.turnRight()
dig()
turtle.turnRight()
curW = curW + 1
curL = 0
end
end
digMoveR()
2005 posts
Posted 06 January 2013 - 04:14 AM
Wait, how was that digmove() not throwing an attempt to call nil error?
127 posts
Posted 06 January 2013 - 04:23 AM
it threw me a nil error thats how i saw it, maybe he copyed the code out? like by hand?
4 posts
Posted 06 January 2013 - 05:34 AM
Thanks for all your help guys, I've moved away from using the "while" statements and moved to "for", as suggested, and came up with something very similar to ChiknNuggets that is working for me :)/>.
Yes because this was written on a turtle on a server I play on, i didn't have access to copy and paste it, so I just wrote out the relevant bits of code for this topic.
I've still not finished the program, but if I run into anymore issues i'll be sure to post them here :)/>.
ChiknNuggets: could you explain why the following is true?
" you dont want to use while ~= because everything below 5 would count and everything after it also"
Surely once curL became 5 it should exit the while loop??
2005 posts
Posted 06 January 2013 - 05:52 AM
Yes, which is why the problem is so puzzling. But let's say that somehow curL got a 0.1 added at some point. Well then, it would never be equal to 5, would it? But it would eventually become greater than 5.
The for loop uses > for this very reason.
127 posts
Posted 06 January 2013 - 06:03 AM
also just a peice of advice use the pastebin program to upload the code from the server, pastebin put <file>
797 posts
Posted 06 January 2013 - 06:06 AM
you put digmove() instead of digMove() therefore it might not run the digMove() function and never add anything to curL
818 posts
Posted 06 January 2013 - 06:12 AM
awsumben13, are you just trying to brag about that you spotted the problem because the guys here already found it, and you just said the exact same thing as the other guys here. So, why did you post? I'm curious.