767 posts
Posted 06 February 2013 - 10:27 AM
Hi.
i've made a Goto program which uses my GPS system..
first i placed my turtle. then i ran the "gps locate" to see if the GPS works… and it was correct… the chordinates worked fine…
but. when i then ran my program, and told it to go to the specific chordinates, it is going some blocks to far.. can anyone show me why?
code:Pastebintwo links ( they are same )
Thanks in advance!!
21 posts
Posted 06 February 2013 - 12:42 PM
in your while runUp > 0 and while runDown > 0 loops you don't decrement runUp or runDown
also if your turtle couldn't move forward into that space why should it be able from the top or the bottom?
if your turtle needs to go two blocks down it will get stuck going down and up again
if your turtle needs to go up or down and can't while digIsPossible == false it will be stuck in a loop without yielding which can cause the program to abort
say x is 1 and gox is 2 then your turtle would go in a for i=1,2 loop which means it's executed twice and your turtle would have moved 1 too far
767 posts
Posted 06 February 2013 - 07:52 PM
Will test it out…
767 posts
Posted 06 February 2013 - 08:22 PM
ehm, so i actually have to add in a "-1" after the for i = 1,2 -1 ???
im not sure, but when i tested it yesterday it does everything too short.
818 posts
Posted 06 February 2013 - 09:15 PM
for i = 1,2,-1 do wont even run because it knows it will never end
7508 posts
Location
Australia
Posted 06 February 2013 - 09:39 PM
for i = 1,2 do wont even run because it knows it will never end
ummmm………..
for i = 1, 2 do
print(i)
end
will run exactly twice. printing 1 and 2.
this is what would not run.
for i = 1, 2, -1 do
print(i)
end
767 posts
Posted 06 February 2013 - 09:50 PM
but… i actually found it out.. when i used the for i = 1,2 -1 it actually worked.
(cur code)
818 posts
Posted 07 February 2013 - 01:01 AM
for i = 1,2 do wont even run because it knows it will never end
ummmm………..
for i = 1, 2 do
print(i)
end
will run exactly twice. printing 1 and 2.
this is what would not run.
for i = 1, 2, -1 do
print(i)
end
Yeah I failed as always….
2005 posts
Posted 08 February 2013 - 10:42 AM
but… i actually found it out.. when i used the for i = 1,2 -1 it actually worked.
(cur code)
What do you mean by "actually worked"? Do you mean that it skipped the loop, or that it ran the loop forever? Or something else? The syntax is valid, but the expected behavior would be for the loop to resolve on the first test because i is already less than 2 (and the loop is set to decrement).
767 posts
Posted 08 February 2013 - 10:55 AM
I mean, now it did what it was supposed to do ;)/>
2005 posts
Posted 08 February 2013 - 11:38 AM
Well, then what it was supposed to do must have involved skipping that loop.
767 posts
Posted 08 February 2013 - 11:52 AM
I didnt skip teh loop, i just added the -1 xD.
ehm could you help me in my other post: "t"api goto("chargecheck") not working.
i really want the işsue solved :)/>
thanks
2005 posts
Posted 08 February 2013 - 12:00 PM
Tell you what, put a line like:
print("Hah hah hah, ChunLing is a poopy-head!")
inside of that loop, and see if your program ever does that.
A for loop of the form:
for i = 1,2 -1 do ... end
should be functionally equivalent to
do local i = 1
while i >= 2 do ...
i = i-1
end
end
Which is to say, it assigns the local i = 1, checks if it is less than 2, and hey, it is, so we skip to the end and keep going.
Edited on 08 February 2013 - 11:02 AM
278 posts
Posted 08 February 2013 - 02:13 PM
Tell you what, put a line like:
print("Hah hah hah, ChunLing is a poopy-head!")
inside of that loop, and see if your program ever does that.
A for loop of the form:
for i = 1,2 -1 do ... end
should be functionally equivalent to
do local i = 1
while i >= 2 do ...
i = i-1
end
end
Which is to say, it assigns the local i = 1, checks if it is less than 2, and hey, it is, so we skip to the end and keep going.
Yeah, they're functionally equivalent. That is to say, they don't run at all.
21 posts
Posted 09 February 2013 - 11:07 AM
for i = 1, 2 - 1 do ... end <=> for i = 1,1 do ... end
(runs once)
while
for i = 1, 42 - 1 do ... end <=> for i = 1, 41 do ... end
(runs 41 times)
EDIT:
for i = x,y do ... end
<=>
do local i = x
while i <= y do
...
i = i + 1
end
end
in the first example x = 1 and y = (2 - 1)
2005 posts
Posted 09 February 2013 - 08:46 PM
How did I not spot that?
Yes, without a comma between 2 and -1, it will be evaluated as a single expression, rather than two separate parameters.
"Hah hah hah, ChunLing is a poopy-head!"