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

Goto command for turtle not working

Started by YoureFunny, 04 September 2012 - 02:43 PM
YoureFunny #1
Posted 04 September 2012 - 04:43 PM
so i made this goto command for my turtle and it shows an error "bios:48: bad argument: string expected, got nil" i tried finding the problem, but the line 48 only contains
face = 4
, help please?


local tArgs = {...}
local face = 0
local r = 1
rednet.open("right")
local cx,cy,cz = gps.locate(2,false)
if #tArgs ~= 3 then
  print("Incorrect usage of Function.")
  print("goto <x> <y> <z>")
else
local gx = tArgs[1]
local gy = tArgs[2]
local gz = tArgs[3]
local x,y,z
function PL()
  print("Current Location")
  write(cx) write(" ") write(cy) write(" ") print(cz)
  print("Destination")
  write(gx) write(" ") write(gy) write(" ") print(gz)
  print("GOING...")
  end
function c_face()
if face == 5 then
  face = 1
elseif face == 0 then
  face = 4
end
end
function get_Face()
turtle.forward()
while turtle.forward() == false do
  turtle.up()
   if turtle.up() == false then
	turtle.digUp()
   end
  end

local nx,ny,nz = gps.locate(2,false)
if nx > cx then
  face = 1
end
if nx < cx then
  face = 4
end
if nz > cz then
  face = 3
end
if nz < cz then
  face = 2
end
end
function calc()
x = nx - gx
y = ny - gy
z = nz - gz
end
function rotateL()
turtle.turnLeft()
face = face - 1
c_face()
end
function f()
turtle.forward()
while turtle.forward() == false do
  turtle.up()
   if turtle.up == false then
	turtle.digUp()
   end
  end
end

function go()
calc()
while x > 0 do
  if face ~= 1 then
   rotateL()
  else
  f()
  x = x - 1
  end
end
while x < 0 do
  if face ~= 4 then
   rotateL()
  else
  f()
  x = x + 1
  end
end
while z > 0 do
  if face ~= 3 then
   rotateL()
  else
  f()
  z = z - 1
  end
end
while z < 0 do
  if face ~= 2 then
   rotateL()
  else
  f()
  z = z + 1
  end
end

while y > 0 do
  turtle.up()
  if turtle.up() == false then
   turtle.digUp()
  end
end
while y < 0 do
  turtle.down()
  if turtle.down() == false then
   turtle.digDown()
  end
end
end

PL()
get_Face()
go()
print(face)
write(nx) write(" ") write(ny) write(" ") print(nz)
write(cx) write(" ") write(cy) write(" ") print(cz)

end

EDIT: Added another Wireless PC, that error gone away, now its "goto:59: attempt to perform arithmetic __sub on nil and string"
line 59:

x = nx - gx
Lyqyd #2
Posted 05 September 2012 - 01:21 AM
Arguments are passed in as strings, so you'd need to tonumber() each of the g coordinates.
YoureFunny #3
Posted 06 September 2012 - 07:26 AM
Thanks, that helped, but now I'm having a different problem, my turtle starts going in circles if it needs to rotate more than once…
Code

local tArgs = {...}
local face = 0
local r = 1
rednet.open("right")
local cx,cy,cz = gps.locate(2,false)
if #tArgs ~= 3 then
  print("Incorrect usage of Function.")
  print("goto <x> <y> <z>")
else
local gx = tonumber(tArgs[1])
local gy = tonumber(tArgs[2])
local gz = tonumber(tArgs[3])
local x
local y
local z
function PL()
  print("Current Location")
  write(cx) write(" ") write(cy) write(" ") print(cz)
  print("Destination")
  write(gx) write(" ") write(gy) write(" ") print(gz)
  print("GOING...")
  end
function c_face()
if face == 5 then
  face = 1
elseif face == 0 then
  face = 4
end
end
function get_Face()
turtle.forward()
while turtle.forward() == false do
  turtle.up()
   if turtle.up() == false then
    turtle.digUp()
   end
  end

nx,ny,nz = gps.locate(3,false)
print(nx) print(" ") print(ny) print(" ") print(nz)
if nx > cx then
  face = 1
end
if nx < cx then
  face = 3
end
if nz > cz then
  face = 2
end
if nz < cz then
  face = 4
end
print(face)
end
function calc()
x = nx - gx
y = ny - gy
z = nz - gz
end
function rotateL()
turtle.turnLeft()
face = face - 1
c_face()
print(face)
end
function f()
while not turtle.forward() do
   if not turtle.up()  then
    turtle.digUp()
   end
   y = y + 1
  end
end

function go()
calc()
goloop = 1
while goloop == 1 do
  if x > 0 then
   if face ~= 1 then
    rotateL()
   end
   f()
   x = x - 1
   end
  if x < 0 then
   if face ~= 3 then
    rotateL()
   end
   f()
   x = x + 1
   end
  if z < 0 then
   if face ~= 2 then
    rotateL()
   end
   f()
   z = z - 1
   end
  if z > 0 then
   if face ~= 4 then
    rotateL()
   end
   f()
   z = z + 1
   end
  if y > 0 then
   if not turtle.up() then
    turtle.digUp()
   end
   y = y - 1
   end
  if y < 0 then
   if not turtle.down() then
    turtle.digDown()
   end
   y = y + 1
   end
  if x == 0 and y == 0 and z == 0 then
   goloop = 0
   end
end
 
end
 

tonumber(gx)
tonumber(gy)
tonumber(gz)
PL()
get_Face()
go()
print(face)
write(nx) write(" ") write(ny) write(" ") print(nz)
write(cx) write(" ") write(cy) write(" ") print(cz)

end
KaoS #4
Posted 06 September 2012 - 08:57 AM
I see in your while true loops you say:


turtle.forward()
while turtle.forward() == false do

that will actually go forward twice, you are telling it to move forward, then execute the forward command again and check its output. use


while turtle.forward() == false do
YoureFunny #5
Posted 06 September 2012 - 03:31 PM
Thanks for the help, i fixed the spinning in circles by writing a new go() function, if anybody is interested, here's the code:

local tArgs = {...}
local face = 0
local r = 1
rednet.open("right")
local cx,cy,cz = gps.locate(2,false)
if #tArgs ~= 3 then
  print("Incorrect usage of Function.")
  print("goto <x> <y> <z>")
else
local gx = tonumber(tArgs[1])
local gy = tonumber(tArgs[2])
local gz = tonumber(tArgs[3])
local x
local y
local z
function PL()
  print("Current Location")
  write(cx) write(" ") write(cy) write(" ") print(cz)
  print("Destination")
  write(gx) write(" ") write(gy) write(" ") print(gz)
  print("GOING...")
  end
function c_face()
if face == 5 then
  face = 1
elseif face == 0 then
  face = 4
end
end
function get_Face()
while turtle.forward() == false do
   if turtle.up() == false then
    turtle.digUp()
   end
  end

nx,ny,nz = gps.locate(3,false)
print(nx) print(" ") print(ny) print(" ") print(nz)
if nx > cx then
  face = 1
end
if nx < cx then
  face = 3
end
if nz > cz then
  face = 2
end
if nz < cz then
  face = 4
end
print(face)
end
function calc()
x = nx - gx
y = ny - gy
z = nz - gz
end
function setDir(dir)
while dir ~= face do
  turtle.turnLeft()
  face = face - 1
  c_face()
  end
end
function f()
while not turtle.forward() do
   if not turtle.up()  then
    turtle.digUp()
   end
   y = y + 1
  end
end
function go()
if x < 0 then
  setDir(1)
  while x ~= 0 do
   f()
   x = x + 1
  end
end
if x > 0 then
  setDir(3)
  while x ~= 0 do
   f()
   x = x - 1
  end
end
if z < 0 then
  setDir(2)
  while z ~= 0 do
   f()
   z = z + 1
  end
end
if z > 0 then
  setDir(4)
  while z ~= 0 do
   f()
   z = z - 1
  end
end
while y < 0 do
  if turtle.up() ~= true then
   turtle.digUp()
  end
  y = y + 1
end
while y > 0 do
  if turtle.down() ~= true then
   turtle.digDown()
  end
  y = y - 1
end
end
 


tonumber(gx)
tonumber(gy)
tonumber(gz)
PL()
get_Face()
calc()
go()
print(face)
write(nx) write(" ") write(ny) write(" ") print(nz)
write(cx) write(" ") write(cy) write(" ") print(cz)

end