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

Unexpected symbol... on a line with only expected symbols...

Started by KeithGB, 06 July 2012 - 10:22 PM
KeithGB #1
Posted 07 July 2012 - 12:22 AM
Hey, I'm getting an Unexpected Symbol on line 72 (commented), any help fixing would be greatly appreciated.

Also, how do I condense my TarIn function so that the coords can be entered on one line?

Thanks very much.


function CalLoc()
x, y, z = gps.locate(1)
if x == nil or y == nil or z == nil then
error("Failed to get coordinates for initial location, check GPS Hosts")
else
print ("Current coordinates:")
print ("x: "..x)
print ("y: "..y)
print ("z: "..z)
end

end
function CalFac()
turtle.forward()
curx, cury, curz = gps.locate(1)
if curx == nil or cury == nil or curz == nil then
error("Failed to get coordinates for orientation, check GPS Hosts")
else

if x == curx and y == cury then
error("Failed to move, please check obstacles")
else

if curx > x then
fac = 1
print ("East "..fac)
else

if curx < x then
fac = 3
print ("West "..fac)
else

if cury > y then
fac = 2
print ("South "..fac)
else

if cury < y then
fac = 4
print ("North "..fac)
else

error("Failed to get facing")

end
end
end
end
end
end
end

function TarIn()
print("Enter target coordinates")
write("X: ")
tarx = read()
write("Y: ")
tary = read()
write("Z: ")
tarz = read()
print("Coordinates confirmed as "..tarx..", "..tary..", "..tarz)
end

function flytoZ()
if curz < tarz then
  repeat
   turtle.up()
   curz=curz+1 --Y U SO BUGGY?
  until curz >= tarz
else
if curz > tarz then
  repeat
   turtle.down()
   curz=curz-1
  until curz <= tarz
else
end
end
end

CalLoc()
CalFac()
TarIn()
flytoZ()
print ("Finished flight")

P.s. Very sorry, only just remembered that I should have labelled this [Lua][Error]!
Edited on 06 July 2012 - 10:25 PM
MysticT #2
Posted 07 July 2012 - 12:27 AM
Is that the exact code you use? Cause I don't see any error.

For the function, you could make the user input the coords in a certain format, and then read it. Like:

print("Enter: X Y Z")
local input = read()
local tCoords = {}
for c in string.gmatch(input, "[^ t]) do
  table.insert(tCoords, c)
end
-- now you have the input in the tCoords table, you can access them like tCoords[1], tCoords[2] and tCoords[3]
KeithGB #3
Posted 07 July 2012 - 12:48 AM
Thanks very much! Just double checked and noticed that when I edited the file on the server it always removed the '+' when saving. Now I'm uploading it works a charm. However I do have a new error, should have noticed it before but I'm storing the tarx etc. as string rather than numbers, how do I change that?

Oh and thanks for the code however I'm getting an Unfinished String error on:


function TarIn()
print("Enter target coordinates")
local input = read()
local tCoords = {}
for c in string.gmatch(input, "[^ t]) do -- This line here
table.insert(tCoords, c)
end
-- now you have the input in the tCoords table, you can access them like tCoords[1], tCoords[2] and tCoords[3]
tarx = tCoords[1]
tary = tCoords[2]
tarz = tCoords[3]
end
MysticT #4
Posted 07 July 2012 - 01:03 AM
Yeah, I forgot a " to close the string, this should fix it:

function TarIn()
  print("Enter target coordinates")
  local input = read()
  local tCoords = {}
  for c in string.gmatch(input, "[^ t]") do
    table.insert(tCoords, c)
  end
  if #tCoords ~= 3 then
    print("Wrong input")
  else
    tarx = tonumber(tCoords[1])
    tary = tonumber(tCoords[2])
    tarz = tonumber(tCoords[3])
    if not tarx or not tary or not tarz then
	  print("Wrong input")
    end
  end
end
To convert a string to a number, you can use tonumber, like I did there.
I also added some code to check if the input was correct, you can change the prints to something else.
KeithGB #5
Posted 07 July 2012 - 01:08 AM
You sir, are a god. Thanks of the highest order!

Btw, anyone reading and learning from this, probably a good idea to open your modem first with rednet.open("side").
KeithGB #6
Posted 07 July 2012 - 04:05 AM
*Post moved to new topic*