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

Turtle strays from script.

Started by inuit7, 13 May 2014 - 01:29 AM
inuit7 #1
Posted 13 May 2014 - 03:29 AM
Basically I'm trying to make a "house building" turtle and for some reason on about the third or fourth level (It changes) the turtle will make a premature turn and ruin the whole structure.

here is the code:

function line()
for i = 1,5 do
reload()
turtle.placeDown()
turtle.forward()
end
end

function base()
for o = 1,4 do
line()
turtle.turnRight()
end
end

function walls()
for p = 1,4 do
base()
turtle.up()
end
end

function reload()
turtle.getItemCount(1)
if turtle.getItemCount(1) == 0 then
turtle.select(2)
else
turtle.select(1)
end
end

turtle.select(1)
turtle.up()
walls()
Lyqyd #2
Posted 13 May 2014 - 04:43 AM
Moved to Ask a Pro.
gezepi #3
Posted 14 May 2014 - 04:33 PM
You'll have a better chance of people replying to your post if you put your code in code tags (the icon with <>) so that it is easier to read. Also, make sure you've read through this, especially the last two sections.


function line()
  for i = 1,5 do
	reload()
	turtle.placeDown()
	turtle.forward()
  end
end
function base()
  for o = 1,4 do
	line()
	turtle.turnRight()
  end
end
function walls()
  for p = 1,4 do
	base()
	turtle.up()
  end
end
function reload()
  turtle.getItemCount(1)
  if turtle.getItemCount(1) == 0 then
	turtle.select(2)
  else
	turtle.select(1)
  end
end
turtle.select(1)
turtle.up()
walls()

Your reload() function should be moved to the top since line() will be calling it. Although since you say it works for a couple layers that doesn't seem to be an issue.
Also in reload(), you can remove the first turtle.getItemCount(1) since the value it returns is never used.
As to why it fails to make your structure properly I don't see anything that would cause that, although how it fails would narrow down what to look for. Perhaps something like a mob or torch gets in the way so it can't move forward? Does it have enough fuel? Is there enough resources in slots 1 and 2 to complete the job?
Edited on 14 May 2014 - 02:34 PM
CometWolf #4
Posted 14 May 2014 - 04:45 PM
Your reload() function should be moved to the top since line() will be calling it. Although since you say it works for a couple layers that doesn't seem to be an issue.
This is not relevant, as his functions are not defined locally. Thus he may put them wherever he wishes in his script, since all variables will point to the global environment anyways.

Yor second point however, is probably the correct answer. Since the script does not employ any form of checks to ensure that the turtle was in fact able to move, pretty much anything getting in the way will throw it off.
gezepi #5
Posted 14 May 2014 - 07:01 PM
Ahh, so that's what it is. I never did figure out why the order didn't matter sometimes, thanks CometWolf!
Ion Silverbolt #6
Posted 14 May 2014 - 10:46 PM
The built-in movement functions return boolean values, so you can use them in conditional statements to deal with obstacles. Using just turtle.forward() will always cause you trouble if there are potential obstacles in the way(blocks, gravel, mobs), but since they return true or false, It's easy to use them to make your own movement functions. I use this when I want my turtle to move forward:


function pushForward()
	  
	    while not turtle.forward() do
			    turtle.dig()
			    turtle.attack()
	    end
end

Only when turtle.forward() returns true, will the loop break. The function call also makes the turtle move when successful.

This works for all the other directions too. turtle.back() is a bit trickier since you can't dig backwards. I wrote backwards like this:


function pushBackward()

	    if not turtle.back() then
			    turtle.turnRight()
			    turtle.turnRight()
			    pushForward()
			    turtle.turnRight()
			    turtle.turnRight()
	    end   
end

Moving backwards is kinda odd, but some parts of my programs finish faster if the turtle doesn't have to turn a lot.