Runtime - assertion error

Hi

I’m using a function picked upp från an excellent tutorial called Zombie Break.

The function adds and removes listners.

I’m trying to pass the parameter “add” to this function and get an error seen further down.

First thought I put the call in the wrong place, but now I have tried to add it before and after this function and always get the same error.

PLEASE help me, before I look for a high bridge  :wink:

The function:

[lua]function gameListeners(event)
if event == “add” then
Runtime:addEventListener( “enterFrame”, testWinLoose )
ball:addEventListener( “touch”, cueShot )
ball:addEventListener(“collision”, lineDestroyed)

elseif event == “remove” then
Runtime:removeEventListener( “enterFrame”, testWinLoose )
ball:removeEventListener( “touch”, cueShot )
ball:removeEventListener(“collision”, lineDestroyed)
end

end[/lua]

The call:

[lua]gameListeners(“add”)[/lua]

You find the error attached

If you look at the error it’s telling you it can’t find ball object:

“attempt to index global ball (a nil value)”

Are you creating the ball object as a local object in some other function?  If you show the code where you are creating ball you will probably get a more precise answer but two general approaches that come to mind are either

  1. declare the ball object at the top of your code file, outside any functions.

  2. pass the ball object into your function.

#2 would typically be the preferred method, imo, but you could still run into the same issue if the ball object isn’t available when you try to run the function.

TNX soooo much for your reply

As you can se I’m a newbie and need any help I can get.

You were correct, I did not declare the ball in the beginning, just in the function were I create the ball.

So now I added “local ball” in the beginning, and another one actually.

I saw this nil value, but since I called the function within the “Createball”-function, I did not understand.

Anyway, don’t think I understand what is local and what is global, can you give me a tip where to find this in the docs? Is local just within a function and not within the main module?

And I did not understand how to do like you say with nr 2

Attached the new error, and further down you can see my “create ball” where I remover the “local” because I’ve added “local ball” in the beginning.

[lua]function createball()

ball = display.newCircle( 20, 20, 18 )
ball.strokeWidth = 3
ball:setStrokeColor(0,155,255)
ball:setFillColor(255,155,0)
ball.x = _W/2
ball.y = _H/2

physics.addBody ( ball, “dynamic”, { density = 0.1, friction = 0.1, bounce = 0.5, radius = 20 } )
ball.linearDamping = 1.0
ball.isBullet = true
ball.isFixedRotation = true

target = display.newImage( “target.png” )
target.x = ball.x
target.y = ball.y
target.alpha = 0

gameListeners(“add”)

end[/lua]

You probably won’t find much on how local variables work within the Corona documents(though they did make some blog tutorials on it in the past) because it’s more of a Lua language thing.  

Basically you got it right.  A local variable will not be accessible outside of the function it was declared, unless it is passed via a function.  If you declare a variable as local at the top of your Lua file then that variable will not be accessible outside of that Lua file.  If you google local variables in corona a few things come up, including blog posts by Corona Labs.  Those will go into more depth.

The error message you provided is a kind of hard for me to tell you exactly what is because I can’t tell what the line numbers actually are.  It looks like it’s one of your addEventListeners calls is causing the issue.  If you read the error it’s telling you there is a problem in your gameListener’s function and probably at line 249.  Check out what’s going on there.  Maybe it can’t find the functions you are assigning to the listener?

Alright, lastly by passing the ball in i mean your gameListener call would look like:

[lua]

 gameListener(“add”, ball)

[/lua]

and your gameListener function would look more like

[lua]

function gameListeners(event, myBall)

if event == “add” then

Runtime:addEventListener( “enterFrame”, testWinLoose )

myBall:addEventListener( “touch”, cueShot )

myBall:addEventListener(“collision”, lineDestroyed)

elseif event == “remove” then

Runtime:removeEventListener( “enterFrame”, testWinLoose )

myBall:removeEventListener( “touch”, cueShot )

myBall:removeEventListener(“collision”, lineDestroyed)

end

end

[/lua]

So instead of just passing “add” as event we would be passing the ball object which we then call myBall inside the function.   I would probably hold off on changing your code to that method(if you even bother) until you get a grip on what’s going on with your error messages.

THX a lot

Realized the other problem because of your answer.

One of the functions came after the listner function.

The error message pointed towards a line in the beginning of the function, not exactly ther row that makes it break, that makes everything som much harder :slight_smile:

Then the error messages always contain several row numbers, I never know which one to look at.

Anyway, TNX friend. Helped me a lot.

Guess declaring all variables in the beginning as local will be a rule in my house from now on  :stuck_out_tongue:

Regards

bergapappa

If you look at the error it’s telling you it can’t find ball object:

“attempt to index global ball (a nil value)”

Are you creating the ball object as a local object in some other function?  If you show the code where you are creating ball you will probably get a more precise answer but two general approaches that come to mind are either

  1. declare the ball object at the top of your code file, outside any functions.

  2. pass the ball object into your function.

#2 would typically be the preferred method, imo, but you could still run into the same issue if the ball object isn’t available when you try to run the function.

TNX soooo much for your reply

As you can se I’m a newbie and need any help I can get.

You were correct, I did not declare the ball in the beginning, just in the function were I create the ball.

So now I added “local ball” in the beginning, and another one actually.

I saw this nil value, but since I called the function within the “Createball”-function, I did not understand.

Anyway, don’t think I understand what is local and what is global, can you give me a tip where to find this in the docs? Is local just within a function and not within the main module?

And I did not understand how to do like you say with nr 2

Attached the new error, and further down you can see my “create ball” where I remover the “local” because I’ve added “local ball” in the beginning.

[lua]function createball()

ball = display.newCircle( 20, 20, 18 )
ball.strokeWidth = 3
ball:setStrokeColor(0,155,255)
ball:setFillColor(255,155,0)
ball.x = _W/2
ball.y = _H/2

physics.addBody ( ball, “dynamic”, { density = 0.1, friction = 0.1, bounce = 0.5, radius = 20 } )
ball.linearDamping = 1.0
ball.isBullet = true
ball.isFixedRotation = true

target = display.newImage( “target.png” )
target.x = ball.x
target.y = ball.y
target.alpha = 0

gameListeners(“add”)

end[/lua]

You probably won’t find much on how local variables work within the Corona documents(though they did make some blog tutorials on it in the past) because it’s more of a Lua language thing.  

Basically you got it right.  A local variable will not be accessible outside of the function it was declared, unless it is passed via a function.  If you declare a variable as local at the top of your Lua file then that variable will not be accessible outside of that Lua file.  If you google local variables in corona a few things come up, including blog posts by Corona Labs.  Those will go into more depth.

The error message you provided is a kind of hard for me to tell you exactly what is because I can’t tell what the line numbers actually are.  It looks like it’s one of your addEventListeners calls is causing the issue.  If you read the error it’s telling you there is a problem in your gameListener’s function and probably at line 249.  Check out what’s going on there.  Maybe it can’t find the functions you are assigning to the listener?

Alright, lastly by passing the ball in i mean your gameListener call would look like:

[lua]

 gameListener(“add”, ball)

[/lua]

and your gameListener function would look more like

[lua]

function gameListeners(event, myBall)

if event == “add” then

Runtime:addEventListener( “enterFrame”, testWinLoose )

myBall:addEventListener( “touch”, cueShot )

myBall:addEventListener(“collision”, lineDestroyed)

elseif event == “remove” then

Runtime:removeEventListener( “enterFrame”, testWinLoose )

myBall:removeEventListener( “touch”, cueShot )

myBall:removeEventListener(“collision”, lineDestroyed)

end

end

[/lua]

So instead of just passing “add” as event we would be passing the ball object which we then call myBall inside the function.   I would probably hold off on changing your code to that method(if you even bother) until you get a grip on what’s going on with your error messages.

THX a lot

Realized the other problem because of your answer.

One of the functions came after the listner function.

The error message pointed towards a line in the beginning of the function, not exactly ther row that makes it break, that makes everything som much harder :slight_smile:

Then the error messages always contain several row numbers, I never know which one to look at.

Anyway, TNX friend. Helped me a lot.

Guess declaring all variables in the beginning as local will be a rule in my house from now on  :stuck_out_tongue:

Regards

bergapappa