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

Turtle Home position.

Started by dansan1987, 13 February 2013 - 05:41 AM
dansan1987 #1
Posted 13 February 2013 - 06:41 AM
Title: Turtle Home position.

Hi, i am still relatively new to Lua and i am having a bit of a problem.
I am attempting to just learn how to make the robot come back to its starting position.
As of now I only want him to dig till there are no blocks ahead then come back to his starting position.
Sorry if this has been asked i have checked the forums but generally they are in long strings of code that are
still hard for me to grasp.


print("test")
while turtle.detect() do
turtle.dig()
turtle.forward()
end
while not turtle.detect() do
return false
end
LBPHacker #2
Posted 13 February 2013 - 06:47 AM
First of all, you have to know how far you are from the home-block. You can use this code:


local distance = 0


function attemptForward()
	if turtle.forward() then distance = distance + 1 end
end


function attemptBack()
	if turtle.back() then distance = distance - 1 end
end

So use attemptForward() instead of turtle.forward(), and when you're done, "distance" represents the distance from the home-block.
dansan1987 #3
Posted 13 February 2013 - 06:51 AM
Ah ok, thank you and sorry for bothering with something this small
dansan1987 #4
Posted 13 February 2013 - 07:27 AM
Well the noob is back lol. Again sorry about this i am just tryin to understand one more thing.
the line of code you gave me. I understand it all up to the point on how to actually make him follow
the distance Back its all working such as the counting and such but i just cant get him to return.


local distance = 0
function attemptForward()
if turtle.forward() then distance = distance + 1 end
end


function attemptBack()
if turtle.back() then distance = distance - 1 end
end
print("test")
while turtle.detect() do
turtle.dig()
attemptForward()
print(distance)
end
if not turtle.detect() then
turtle.back = distance - 1
end
I_IblackI_I #5
Posted 13 February 2013 - 08:33 AM
replace turtle.back = distance - 1 (last -1 line) with attemptBack() that should work
ChunLing #6
Posted 13 February 2013 - 12:57 PM
You should probably be more explicit in framing your intention for how the turtle is to operate on the movement forward.

But as for going back, it's:
while distance > 0 do
    attemptBack()
end
dansan1987 #7
Posted 14 February 2013 - 12:47 PM
You should probably be more explicit in framing your intention for how the turtle is to operate on the movement forward.

But as for going back, it's:
while distance > 0 do
	attemptBack()
end



This was Exactly what i was looking for and TYVM it works perfectly for my new programs.
SuicidalSTDz #8
Posted 14 February 2013 - 12:52 PM
Just out of curiosity, what are you implementing this code into? Quarry, Earth-Remover, etc?
dansan1987 #9
Posted 15 February 2013 - 04:35 PM
Atm i was using it more for a line remover for doing walls and such but was tired of climbing back up and down to get my turtle.
I am gonna be using it for various things in the future but still working on this and that.