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

Uncalled function running randomly

Started by Noiro, 19 June 2013 - 08:44 PM
Noiro #1
Posted 19 June 2013 - 10:44 PM
Alright guys, I'm trying to get a clearArea program running, but much to my demise, the very end of the program has some random call to my continue() function though the condition to run it should be false (actually in the command stream, it should not even be checking it again at all). It runs as usual, properly performs the X while statement in init() by executing continue at proper times, but at the end of the while statement, continue gets called again….somehow. Ideas?

http://pastebin.com/vHJbJ8qF


If you put a 2x2 blockset in front of the turtle with him in the far left corner and use clearArea, he will properly mine them (and any dimensions if you change width and length), the issue occurs when
he gets to the end of the loop, instead of sitting there and waiting, he calls continue() which essentially makes him appear to flip out at the end of the loop. Why is it getting called?
Lyqyd #2
Posted 19 June 2013 - 11:21 PM
Try initializing x as 1 instead of 0, same with y.
Bomb Bloke #3
Posted 19 June 2013 - 11:27 PM
To your "demise"? That's quite a bug you've got there… ;)/>

To my eye, starting "x" at 1 will result in one too few lines being dug. But, you have got it rigged so that for every "line" call there's one "continue" call, and I'd say that's your problem: you want one less "continue", much the same as you've rigged one less "digUp".

Also, consider trying "for" loops. This:

    x = 0
    while x ~= width do
      -- etc
      x=x+1
    end

… can be re-written as this:

    for x=0,width-1 do
      -- etc
    end

The result is much the same: "x" starts at 0 and increments by 1 with each loop. The loop stops when "x" would exceed "width-1".
Noiro #4
Posted 19 June 2013 - 11:32 PM
Try initializing x as 1 instead of 0, same with y.
When I do that, it does one less colum than it is supposed to, but it still continues to run continue (continue runs as if it will complete last column, but the line() function never gets called to finish it). It is almost as if the continue() is called THEN the condition to check if it should run line().


To your "demise"? That's quite a bug you've got there… ;)/>

To my eye, starting "x" at 1 will result in one too few lines being dug. But, you have got it rigged so that for every "line" call there's one "continue" call, and I'd say that's your problem. You want one less "continue", much the same as you've rigged one less "digUp".

Also, consider trying "for" loops. This:

	x = 0
	while x ~= width do
	  -- etc
	  x=x+1
	end

… can be re-written as this:

	for x=0,width-1 do
	  -- etc
	end

The result is much the same: "x" starts at 0 and increments by 1 with each loop. The loop stops when "x" would exceed "width-1".

That worked like a charm (adding an 'if' in there). I can't believe I missed that. And the issue with for loops is that a for loop will always run at least once regardless of if the condition matches or not whereas a while loop will not run unless the condition is met (Well…that's what I remember anyway, could be wrong).
Bomb Bloke #5
Posted 19 June 2013 - 11:58 PM
No, "for" loops don't have that problem. Try this out to see:

for x=1,0 do print("Blah") end

Because "x" starts out exceeding 0, the loop never executes and the print statement is skipped entirely.