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

Attempt to compare __lt on nil and number

Started by BWatson236, 17 September 2014 - 07:37 AM
BWatson236 #1
Posted 17 September 2014 - 09:37 AM
So either I'm stumped or the fact that its 4:30 in the morning is making me oblivious, but I can't seem to figure out the issue. Error is: program:11: attempt to compare __lt on nil and number

here is the snippet having issues


function face()
  local facing = Unknown
  turtle.forward()
  local x, y, z = gps.locate()
  if x ~= myx then
	if x > myx then --this is line 11
	  facing = East
	end
	if x < myx then
	  facing = West
	end
  end
  if z ~= myz then
	if z > myz then
	  facing = South
	end
	if z < myz then
	  facing = North
	end
  end
end

This is where the myx myy myz is called

  local myx, myy, myz = gps.locate()


I have put the snippet into its own program to further debug. I don't get any errors, but it also doesn't print anything
basically the turtle gets its coords from the GPS then it moves once and it gets the Coords again. it then figures out if it was less than or greater than the first set to determine what direction the turtle is facing

any and all help that could be provided would be much appreciated!
Bomb Bloke #2
Posted 17 September 2014 - 10:48 AM
My guess is that you're either defining myx sometime after you define this function, or you're defining it within a scope where it can't be accessed by this function.

local function moo()
  print(myx)
end

local myx = "asdf"

moo()

The above code will print nothing, as when the Lua interpreter goes to convert the moo() function to bytecode, no local version of myx has been defined yet - so it assumes it should use a global version. That doesn't change when a local myx is defined.

local myx

local function moo()
  print(myx)
end

myx = "asdf"

moo()

This version uses a forward-declaration to establish a local myx before the function definition is reached. It'll print "asdf".

local function moo()
  local myx = "asdf"
end

local function moo2()
  print(myx)
end

moo()
moo2()

This code also prints nothing. Although a local myx variable is declared before moo2(), it's local to the moo() function, so moo2() can't see it.
BWatson236 #3
Posted 17 September 2014 - 07:10 PM
edit:

Never mind. My final issue resided in putting quotes around the string declaring the direction the turtle was facing.

Thanks!
Edited on 17 September 2014 - 05:35 PM