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

Turtle stuck in a loop

Started by leonardi, 10 May 2013 - 05:13 PM
leonardi #1
Posted 10 May 2013 - 07:13 PM
Hi there I am quite new to LUA and coding in general and this is the first time I am trying to write my own program from scratch. It's quite a simple program and it is working fine upto the point where I basically want it to stop executing but it seems to be stuck in a while loop…

I have already stopped by the IRC chat for some help but that didn't get me anywhere.

http://pastebin.com/y0T8muLj

That's the code…
What it's supposed to do is mine down 10 layers in a 5 by 5 radius which it does but atm it doesn't stop after the 10th layer.

Hope you guys can help me out here.

Leonardi
Lyqyd #2
Posted 10 May 2013 - 08:24 PM
Split into new topic.
The_Awe35 #3
Posted 11 May 2013 - 01:00 AM
The reason it is messing up is because direction() calls initial() which calls platform() which calls direction() which calls initial() etc.

There seem to be alot of conditions that aren't needed. for example on line 65:

i=1
  while i < 5 and row < 10 do
  forward()
  i = i+1
	if i == 5 and row <= 10 and rijtje == 5 then
	line = 1
	goDown() -- It is custom to make the first word lowercase, and every word after start with a capital
	end
  end
the forward function does nothing to the variables, and only i gets changed. Since row and rijtje never reach their variable amounts, it never gets called, unless it reaches it before that point, where it is pointless.
You make rijtje increase in both platform() and initiate()
Just a note, change your forward function to :


local function forward()
  while not turtle.forward() do
	if turltle.detect() then
	turtle.dig()
	else
	turtle.attack() --you may want to use sleep() instead so that if you get in its way, it doesn't kill you :)/>/>/>
	end
  end
end

With this it will keep digging until it can move forward, stopping gravel or sand from messing it up.

Another suggestion, instead of having the set variables of 5 and 10, change them to variable, so if you want to change it later, you can change it in just one spot, instead of the whole code.


A question: Why are there some comments that are in english, and others in a language that I am not sure of?
leonardi #4
Posted 11 May 2013 - 09:16 AM
Yeah the reason why my code is this sloppy is because I just started with the whole coding thing. And as I progressed into the code, and was testing it out I was adding stuff as I went along. and in the end it became rather clusterfucky.

The reason comments are in english and in dutch is because I started of doing it in english and as I went on and got more desperate I switched over to dutch.

And yeah one of the people on the IRC chat already pointed out to me that these functions keep calling eachother but is there a was to break that cycle or maybe a way to make a function that calls the appropriate fuctions when needed?
I am kind of stuck here.
The_Awe35 #5
Posted 11 May 2013 - 02:26 PM
I can help find problems, but I am not really good at "fixing" code. Instead I made my own version of how I would go about doing it. It is missing some things, like a refuel function, but it should (I haven't tested it yet) be able to mine in a rectangle of any size.

http://pastebin.com/ma2eQV65

Feel free to use/expand upon it as much as you want.
Engineer #6
Posted 11 May 2013 - 02:59 PM
It is very easy to fix your code. Here is the fixed one!

http://pastebin.com/BGGdLt4t

You were recalling the function, but that was not necessary since you are calling it in a loop :)/>
Also, a dutch PM with explanation why you should use locals is on its way!
leonardi #7
Posted 11 May 2013 - 10:53 PM
Hey thanks for replying and taking the time to look at my code.

I looked at the link you gave me in PM and as I said I just started coding so any help is greatly appreciated so thanks again for that.
But the code you gave me back doesn't seem to work either it works fine the first platform that it does but after it goes down for the first time it stops and says: Attempt to call Nil on line 21.
Engineer #8
Posted 12 May 2013 - 06:14 AM
Oops I didnt notice that. You should first make the function and after that you call it:


function x()
    print('lol')
end 
x()
leonardi #9
Posted 12 May 2013 - 02:28 PM
Hey guys I appreciate all of your help but after spending several hours trying to write this little program (Yes hours, I am not proud of this =P)
My OCD was kind of pushing me to do it on my own so today I just started from scratch and I was determined to write this code myself…

So after another few hours (=P) and extensive testing I finally got it to work the way I wanted it to.

If any of you is interested http://pastebin.com/z0rQMqEP this is the working code.
I tried to also pay more attention to the indentation but pasting it into pastebin kinda makes it look weird again…. Oh well.

Regards,

Leonardi