attempt to compare nil with number

Can someone kindly explain to me why this code:

local coordinate = {}  
local function gridPositions(number, offsetDown)   
 if number \<= 4 and offsetDown \<= 2 then  
 local pointx = (85 \* number) + 31  
 local pointy = (85 \* offsetDown) + 53  
 coordinate = {pointx, pointy}  
 else  
 print "Off of grid"  
 end  
end  

produces a Runtime Error “attempt to compare nil with number”

I think it has something to do with my offsetDown value, but I don’t know why. When I try to set up my function as:

local function gridPositions(number = 0, offsetDown = 0)  

I get a Runtime error “error loading module” and the app crashes. What’s going on? [import]uid: 47235 topic_id: 20757 reply_id: 320757[/import]

at top of function add
print( number, offsetDown )
this way you will know if numbers are being passed to function [import]uid: 7911 topic_id: 20757 reply_id: 81590[/import]

If you’re getting the error it’s because one of the parameters is nil entering the function. If you want to validate values in your code, you can use the following to abort the program and print an error message in the console:

assert( number and offsetDown, "gridPositions: either number or offsetDown is nil" )  

The expression in the assert statement must evaluate to true or it aborts and sends the message to the console.

Tom [import]uid: 6119 topic_id: 20757 reply_id: 81596[/import]

Lua doesn’t allow default function parameter values in the following format:

local function gridPositions(number = 0, offsetDown = 0)

You’ll have to do something like this, which is the standard Lua convention:

local function gridPositions(number, offsetDown) number = number or 0 offsetDown = offsetDown or 0
[import]uid: 71767 topic_id: 20757 reply_id: 81606[/import]

I appreciate your answers guys. Here was my problem. Me being new to corona, I was calling this function through a listener with a prototype of:

local function listener(e, number, offsetDown)  
 gridPositions(number, offsetDown)  

This was in effect being called by a timer:

timer.performWithDelay( 1000, listener( 3, 1) )  

If you notice I’m only feeding 2 values to the listener, which is also doing some more unimportant stuff not relevant to this discussion. I assumed that the event was assumed by the listener function and therefor I didn’t need to address it. Once I took out the e:

local function listener(number, offsetDown)  

everything worked perfectly. I could have also added the e as a first parameter on the timer:

timer.performWithDelay( 1000, listener( e, 3, 1) )  

I appreciate you guys help. Thanks [import]uid: 47235 topic_id: 20757 reply_id: 81647[/import]

The code you have is probably not working the way you except because listeners in Lua don’t accept arguments. So when you do this:

timer.performWithDelay( 1000, listener( e, 3, 1) )

the listener is called before the delay even starts. The API expects the handle of the listener instead of the results of the listener. In your case it didn’t throw an error because your listener method doesn’t return anything, so it’s nil.

The proper way to do what you are trying to do, is by using upvalues. If you study the listener method below you will see that it accepts the same arguments but returns a function. This returned function IS the listener that is called by the delay routine after the delay period. The listener will call gridPositions with the arguments that were passed when the function was created (this is called non-local variables). This is a powerful feature of Lua and you should do a Google search for upvalues and closures to learn more.

[code]
local function listener(number, offsetDown)
return function(event) gridPositions(number, offsetDown) end
end

timer.performWithDelay( 1000, listener( 3, 1) )
[/code] [import]uid: 6119 topic_id: 20757 reply_id: 81697[/import]

myfogview nailed this problem with a great analysis. Also note the following ways to accomplish the same results, with slightly different code structures:

local function listener(e, number, offsetDown)  
 gridPositions(number, offsetDown)  
end  
  
timer.performWithDelay( 1000, function( e ) listener( e, 3, 1 ) end )  

Or, if you don’t care about the event:

local function listener(number, offsetDown)  
 gridPositions(number, offsetDown)  
end  
  
timer.performWithDelay( 1000, function() listener( 3, 1 ) end )  

You should probably keep the listener function for code clarity, but note that it’s not strictly necessary:

timer.performWithDelay( 1000, function() gridPositions( 3, 1 ) end ) [import]uid: 71767 topic_id: 20757 reply_id: 81782[/import]