I was wondering how you could assign a variable from lots of numbers, for example, "gps locate", how would you assign x y z variables to the three numbers returned from the gps locate. Thanks!
H
x, y, z = gps.locate
I belive that works idk haven't used gps locate before.
local x, y, z = gps.locate()
print("You are at ", x, ", ", y, ", ", z)
gps:69: attempt to compare nil with number
local x, y, z = gps.locate(1) -- 1 second timeout
local x, y, z = gps.locate(1) -- it needs a timeout in seconds, you can change it to whatever you want
if x and y and z then -- check if it could get the position (when it times out the position is nil)
print(x, ", ", y, ", ", z)
else
print("Couldn't get position")
end
gps locate
it works just fine and gives me my position. I'm very confused.
--To goto coordinates, use: "goto <x> <y> <z>" or "goto <x> <z>"
--To goto a saved location, use: "goto <location>"
--To add a location to the saved list, use: "goto add <location> <x> <y> <z>"
--To change the locations directory name, change all instances of "locations" to whatever you want to call it (find/replace). Just make sure the directory is in the same place as the program. I may make this easier soon.
args = {...}
if not fs.isDir("locations") then
fs.makeDir("locations")
end
local function getDirection()
local f = 0
local x, y, z = gps.locate(5,false)
y = nil
if args[1] == "special" then
if turtle.detect() then
if not turtle.dig() then
error("Tried to dig bedrock.")
end
end
while not turtle.forward() do
turtle.attack()
end
else
if not turtle.forward() then
error("I am against a wall.")
end
end
local newx, newy, newz = gps.locate(5,false)
newy = nil
turtle.back()
if newz > z then
f = 0
elseif newx < x then
f = 1
elseif newz < z then
f = 2
elseif newx > x then
f = 3
end
return f
end
local function gotox(x,newx,f)
if f == 0 then
turtle.turnRight()
elseif f == 2 then
turtle.turnLeft()
elseif f == 3 then
turtle.turnRight()
turtle.turnRight()
end
if newx < x then
local j = x - newx
for i=1,j do
if args[1] == "special" then
if turtle.detect() then
turtle.dig()
end
while not turtle.forward() do
turtle.attack()
end
else
if not turtle.forward() then
error("Path blocked.")
end
end
end
elseif newx > x then
local j = newx - x
turtle.turnRight()
turtle.turnRight()
for i=1,j do
if args[1] == "special" then
if turtle.detect() then
turtle.dig()
end
while not turtle.forward() do
turtle.attack()
end
else
if not turtle.forward() then
error("Path blocked.")
end
end
end
turtle.turnLeft()
turtle.turnLeft()
end
if f == 0 then
turtle.turnLeft()
elseif f == 2 then
turtle.turnRight()
elseif f == 3 then
turtle.turnLeft()
turtle.turnLeft()
end
end
local function gotoy(y,newy)
if newy > y then
local j = newy - y
for i=1,j do
if args[1] == "special" then
if turtle.detectUp() then
turtle.digUp()
end
while not turtle.up() do
turtle.attackUp()
end
else
if not turtle.up() then
error("Path blocked.")
end
end
end
elseif newy < y then
local j = y - newy
for i=1,j do
if args[1] == "special" then
if turtle.detectDown() then
turtle.digDown()
end
while not turtle.down() do
turtle.attackDown()
end
else
if not turtle.down() then
error("Path blocked.")
end
end
end
end
end
local function gotoz(z,newz,f)
if f == 1 then
turtle.turnLeft()
elseif f == 2 then
turtle.turnLeft()
turtle.turnLeft()
elseif f == 3 then
turtle.turnRight()
end
if newz > z then
local j = newz - z
for i=1,j do
if args[1] == "special" then
if turtle.detect() then
turtle.dig()
end
while not turtle.forward() do
turtle.attack()
end
else
if not turtle.forward() then
error("Path blocked.")
end
end
end
elseif newz < z then
turtle.turnRight()
turtle.turnRight()
local j = z - newz
for i=1,j do
if args[1] == "special" then
if turtle.detect() then
turtle.dig()
end
while not turtle.forward() do
turtle.attack()
end
else
if not turtle.forward() then
error("Path blocked.")
end
end
end
turtle.turnLeft()
turtle.turnLeft()
end
if f == 1 then
turtle.turnRight()
elseif f == 2 then
turtle.turnRight()
turtle.turnRight()
elseif f == 3 then
turtle.turnLeft()
end
end
local function findDistance(x,y,z,newx,newy,newz)
local distance = 0
local xDist = 0
local yDist = 0
local zDist = 0
if x > newx then
xDist = x - newx
elseif x < newx then
xDist = newx - x
end
if y > newy then
yDist = y - newy
elseif x < newx then
yDist = newy - y
end
if z > newz then
zDist = z - newz
elseif z < newz then
zDist = newz - z
end
distance = xDist + yDist + zDist
return distance
end
if args[1] ~= "special" then
rednet.open("right")
end
x, y, z, f, newx, newy, newz, newf = 0, 0, 0, 0, 0, 0, 0, 0
if #args == 1 then
local location = args[1]
if fs.exists("locations/"..location) then
local fLocation = fs.open("locations/"..location,"r")
newx = tonumber(fLocation.readLine())
newy = tonumber(fLocation.readLine())
newz = tonumber(fLocation.readLine())
fLocation.close()
print("Going to "..location.."...")
else
error("Unknown location.")
end
elseif #args == 2 and tonumber(args[1]) and tonumber(args[2]) then
newx, newz = tonumber(args[1]), tonumber(args[2])
print("Going to x: "..newx..", z: "..newz.."...")
elseif #args == 3 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3])then
newx, newy, newz = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
print("Going to x: "..newx..", y: "..newy..", z: "..newz.."...")
elseif #args == 4 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) then
newx, newy, newz, newf = tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
print("Going to x: "..newx..", y: "..newy..", z: "..newz..", f: "..newf.."...")
elseif #args == 9 and args[1] == "special" and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) and tonumber(args[6]) and tonumber(args[6]) and tonumber(args[7]) and tonumber(args[8]) and tonumber(args[9]) then
newx, newy, newz, newf = tonumber(args[2]), tonumber(args[3]), tonumber(args[4]), tonumber(args[5])
x, y, z, f = tonumber(args[6]), tonumber(args[7]), tonumber(args[8]), tonumber(args[9])
elseif #args == 5 and args[1] == "add" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
local location, xname, yname, zname = args[2], args[3], args[4], args[5]
if not fs.exists("locations/"..location) then
local fLocation = fs.open("locations/"..location,"w")
fLocation.writeLine(xname)
fLocation.writeLine(yname)
fLocation.writeLine(zname)
fLocation.close()
print("Location \""..location.."\" added.")
else
print("This location already exists. Would you like to replace it?\n\(y/n\)")
while true do
event, character = os.pullEvent()
if event == "char" and character == "y" then
local fLocation = fs.open("locations/"..location,"w")
fLocation.writeLine(xname)
fLocation.writeLine(yname)
fLocation.writeLine(zname)
fLocation.close()
print("Location changed.")
break
elseif event == "char" and character == "n" then
print("Location not changed.")
break
end
end
end
error()
else
print("To goto coords, use: \"goto <x> <z>\" or \"goto <x> <y> <z>\"")
print("To goto a set location, use: \"goto <name>\"")
print("To set a new location, use: \"goto add <name> <x> <y> <z>\"")
error()
end
if args[1] ~= "special" then
x, y, z = gps.locate(5,false)
end
if not x or not y or not z then
error("Out of GPS range")
end
local distance = findDistance(x,y,z,newx,newy,newz)
local fuelLevel = turtle.getFuelLevel()
if type(fuelLevel) == "string" then
fuelLevel = 9001e9001
end
if distance > fuelLevel then
error("Not enough fuel to travel so far!")
end
if args[1] ~= "special" then
turtle.up()
turtle.up()
turtle.up()
f = getDirection()
end
if newy > y then
gotoy(y,newy)
end
if newx ~= x then
gotox(x,newx,f)
end
if newz ~= z then
gotoz(z,newz,f)
end
if newy < y then
gotoy(y,newy)
end
if newf ~= f then
if f == 1 then
turtle.turnLeft()
elseif f == 2 then
turtle.turnLeft()
turtle.turnLeft()
elseif f == 3 then
turtle.turnRight()
end
if newf == 1 then
turtle.turnRight()
elseif newf == 2 then
turtle.turnLeft()
turtle.turnLeft()
elseif newf == 3 then
turtle.turnLeft()
end
end
if args[1] ~= "special" then
turtle.down()
turtle.down()
turtle.down()
print("Done traveling!")
end
if args[1] ~= "special" then
rednet.close("right")
end
args, x, y, z, f, newx, newy, newz, newf = nil, nil, nil, nil, nil, nil, nil, nil, nil
This pretty much answers my question. I should read first and post later. <_</>The odd thing is if I use:it works just fine and gives me my position. I'm very confused.gps locate
Try calling it as local x, y, z = gps.locate(1,true) to get debug info displayed.
--Before
print("Man I am so lost right now!")
local x, y, z = gps.locate(5,true)
if x == nil then
print("Nope, still lost :(/>")
else
print("I am at ",x," ",y," ",z)
end
--After
print("Man I am so lost right now!")
local x, y, z = gps.locate(5,true)
if x == nil then
print("Nope, still lost :(/>")
else
print("I am at ",x," ",y," ",z)
end