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

Ending program from within an if statement within a while loop.

Started by Svalm, 08 January 2013 - 02:03 PM
Svalm #1
Posted 08 January 2013 - 03:03 PM
Hey, I know the kode is messy, haven't cleaned it up yet, and it actually got a lot messier while I was trying to solve this. Hope you can read it.
function digging() –Digs in front of self and tries to move forward
emptying()
turtle.dig()
refueling()
while turtle.forward() == false and done == false do
if done == false then
if turtle.detect() then
if turtle.dig()then
emptying()
else
print("I could not dig that block. I must be done.")
emptyingNoCheck()
done = true
leaveProg()
end
else
turtle.attack()
end
else
leaveProg()
end
end
end

and the leaveProg function:
function leaveProg()
if done == true then
shell.exit()
end
end

For some reason the program always finishes the while loop before exiting the program.

What I'm trying to do is getting the turtle to realize when it's trying to mine bedrock and ending the program there. Will be adding a move to original point but haven't gotten to it as I got stuck here.

Hope you understand my code, and what I'm trying to achive.
In hopes of helpful replys
Stefan
Heracles421 #2
Posted 08 January 2013 - 03:08 PM

function digging() --Digs in front of self and tries to move forward
  emptying()
  turtle.dig()
  refueling()
  while turtle.forward() == false and done == false do
    if done == false then
      if turtle.detect() then
        if turtle.dig()then
          emptying()
        else
	  print("I could not dig that block. I must be done.")
 	  emptyingNoCheck()
      	  leaveProg()
        end
      else
        turtle.attack()
      end
    else
      leaveProg()
    end
  end
end


function leaveProg()
  error()
end

I'm not sure why were you checking if done == true inside the leaveProg function, but you actually don't need it since you already checked if you wanted to terminate the program before
theoriginalbit #3
Posted 08 January 2013 - 03:14 PM
- snip -

You are setting the variable done to true before calling the end function, so the loop stops there

- snip -

This isn't a problem. a while loops condition is only checked when all instructions have been completed inside of it.

EDIT: Now as for the OP. why are you setting done if your then going to exit the entire program? also Id suggest using error() instead of shell.exit() as shell.exit actually exits the current shell and inherently your program, not just your program. would something like this work a little better for you?



function digging()
  emptying()
  turtle.dig()
  refueling()
  while not turtle.forward() do
	if turtle.detect() then
	  if turtle.dig()then
		emptying()
	  else
		print("I could not dig that block. I must be done.")
		emptyingNoCheck()
		error()
	  end
   else
	 turtle.attack()
   end
  end
end
Edited on 08 January 2013 - 02:21 PM
Svalm #4
Posted 08 January 2013 - 03:27 PM
I didn't add the done variable untill I ran into trouble getting it to stop. It was just one of my many half assed ideas of solving a problem which I had no clue as how to solve, nor even what was causing it for that matter.
Ideally I'd want the turtle to empty itself, move up to the spot where it was placed down and shut of the program. I was planing to do this all in the spot where I had temporarily put the line "print("I could not dig that block. I must be done.") but since I could not even get it to shut of the program before finishing the while loop I hadn't gotten to it yet.

Edit: Before I ran into the problem I had set the code up like so:
function digging() –Digs in front of self and tries to move forward
emptying()
turtle.dig()
refueling()
while turtle.forward() == false do
if turtle.detect() then
if turtle.dig()then
emptying()
else
print("I could not dig that block. I must be done.")
emptyingNoCheck()
shell.exit()
end
else
turtle.attack()
end
end
end

God damn it. How do you get the code to show up without going to the begining of the line like that?
theoriginalbit #5
Posted 08 January 2013 - 03:32 PM
Ok so still replace shell.exit with error thats a must. but next question, may be a stupid one, but have to check… do you, when testing, actually have a piece of bedrock in front of it?
Heracles421 #6
Posted 08 January 2013 - 03:38 PM
- snip -

God damn it. How do you get the code to show up without going to the begining of the line like that?

- snip -
Try turning HTTP preview off (The little "light" switch at the top left corner of the edit window)

Usually that works for me when using Mac
Svalm #7
Posted 08 January 2013 - 03:39 PM
Yes of course :D/> Went down to bedrock level and set it to mine there and gaged it's reaction once it hit bedrock. It printed the line but then turned around and continued the program one circle before actually exiting. I am testing. Going to see if your suggestion helps.
Svalm #8
Posted 08 January 2013 - 03:43 PM
Hey, wouldn't you know it. Problem solved. Your code fixed it.
Thanks a million, had been wrestling with this for hours now.
I guess that's why they call it Ask a Pro :D/>

Thanks again.
theoriginalbit #9
Posted 08 January 2013 - 03:50 PM
Your welcome. Any time.