7 posts
Posted 06 January 2016 - 09:40 PM
I have been looking for a while for a working function to get the direction my turtle is facing with no luck. So now I gave up on looking for it and decided to ask it here! Is there any easy code for getting the direction as a number (like in the F3 menu)? I'm still fairly new to this so I would like a bit of an explanation/example on how I would use it combined with my own code. Thank you in advance
3057 posts
Location
United States of America
Posted 06 January 2016 - 09:44 PM
There is no function in computercraft that tells a turtle which way it is facing. You can find which way you are facing by setting up a GPS cluster and using gps.locate(), then moving forward and using gps.locate() again. Depending on the variable which changed, and if it incremented or decremented, you can find if you are moving north / south / east / west.
GPS APIGPS TutorialExample of figuring out which way the turtle is facing
local facing
local x, y, z = gps.locate( 1 )
if not x then
error( "No GPS available", 0 )
end
if turtle.forward() then
local nx, ny, nz = gps.locate( 1 )
if x - nx == 1 then
--#this is a direction
facing = 1
elseif x - nx == -1 then
--#this is another direction
facing = 3
elseif y - ny == 1 then
facing = 2
else
facing = 0
end
end
Note: I'm unsure of Minecraft's coordidinate system and north/south/east/west at the moment, so I won't be able to tell you which way is which. You can figure out which way is which by watching the debug values in the F3 screen.
Edited on 06 January 2016 - 08:50 PM
7 posts
Posted 06 January 2016 - 09:46 PM
There is no function in computercraft that tells a turtle which way it is facing. You can find which way you are facing by setting up a GPS cluster and using gps.locate(), then moving forward and using gps.locate() again. Depending on the variable which changed, and if it incremented or decremented, you can find if you are moving north / south / east / west.
I know that and that is why I am asking if someone has some code for it. The only code which seemed to look full and would fit my program didn't have any explanation meaning I had no idea how to use it and it kept returning 0
3057 posts
Location
United States of America
Posted 06 January 2016 - 09:51 PM
I've edited my above post to add a bit more information.
7 posts
Posted 06 January 2016 - 11:05 PM
I've edited my above post to add a bit more information.
Thank you for your reply! It's currently late for me so I will try this tomorrow but if I can't get it to work then I'll reply back in this thread again.
7083 posts
Location
Tasmania (AU)
Posted 06 January 2016 - 11:25 PM
Alternatively, Lion4ever came up with a clever solution
here.
7 posts
Posted 07 January 2016 - 03:33 PM
Alternatively, Lion4ever came up with a clever solution
here.
I don't want to use a method where I need to have things in my mining turtles inventory because It's a harvester and it could fill up a lot of it's inventory. But thanks anyways!
2427 posts
Location
UK
Posted 07 January 2016 - 05:20 PM
Will
LAMA do what you need? You will need to tell your turtle it's direction once, but it will be independent from then onwards (even across server restarts).