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

Gps Not Working

Started by cmckain14, 25 September 2013 - 10:52 PM
cmckain14 #1
Posted 26 September 2013 - 12:52 AM
I have building a mining program but the gps is reporting nil even though the debug reports a value.

a = 0
print("Are there GPS satellites in the sky?")
input = read()
if input == "No" then
  error("There must be a GPS system in place to use this program!")
end
if input == "Yes" then
  local x, y, z = gps.locate(2,true)
  y = by --Backup for y
  print(y) --No value 
  if y == nil then
    error("Nope, still lost :(/>/>")
    end
  for i = 1,tonumber(y) do --It says that y is not a number 
    turtle.digDown()
    turtle.down()
    repeat
      if turtle.detect() then
        turtle.dig()
        end
      turtle.turnLeft()
      a = a + 1
     until a > 4
    end
  while by ~= 1 do
    turtle.up()
    by = by - 1
    end
  turtle.select(2)
  turtle.placeDown()
end
Also, could I have some feedback on the code, please?
immibis #2
Posted 26 September 2013 - 03:01 AM
Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>")
end
Luanub #3
Posted 26 September 2013 - 03:40 AM
One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.

input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.
immibis #4
Posted 26 September 2013 - 04:17 AM
One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.

input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.
More importantly though, what happens if they type "banana"?
Luanub #5
Posted 26 September 2013 - 05:36 AM
One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.

input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.
More importantly though, what happens if they type "banana"?

Or No/Yes instead of no/yes. Definitely could use some extra error checking/prevention.

It could look something like…

local input = string.lower(read()) --make the var local and convert to lower case
if input == "no" then
  --error
elseif input == "yes" then
--run the code
else
   error("Invalid answer")
end

There are also other ways to accomplish this but hopefully that shows the general idea.
cmckain14 #6
Posted 26 September 2013 - 09:54 AM
Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>/>")
end
But why is y nil when the GPS should define it?
MR_nesquick #7
Posted 26 September 2013 - 10:40 AM
by = y == y cord
y = by == nil

your backup is deleting the y cord.

smal derp
immibis #8
Posted 27 September 2013 - 02:36 AM
Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>/>/>/>/>")
end
But why is y nil when the GPS should define it?
Because you made it nil after that.
"by" is nil.
"y = by" sets "y" to whatever "by" is.
Therefore, "y = by" sets "y" to nil (in this program).