Help needed - can't find the error, maybe a bug?

Hello,

in a very simple composer app, I’m consistently getting a “bad argument #1 to ‘unpack’ (table expected, got nil)”, whenever an event handling function is called.

I’ve tried everything I’ve thougth of… it happens running on the simulator, but I also compiled it for windows and exactly the same result.

I’ve reduced the code to this:

local composer = require( "composer" ) local scene = composer.newScene() local math = require "math" -- include Corona's "widget" library local widget = require "widget" local \_gW = display.contentWidth local \_gH = display.contentHeight local \_gFontNormal = \_gW / 16 ---------------------------------- local screenBtn local screenBtn2 local logo local function screenTouchListener( event ) print("W") end local function screen2TouchListener( event ) print("WWWW") composer.gotoScene( "level1", "fade", 1000 ) return true end local function frameUpdate () introd.y = introd.y - 3 -- es introd,y - 0.7 y -2 mientras se toca la pantalla if introd.y \< (\_gH \* -1.2) then logo.isVisible = true screenBtn.isVisible = false screenBtn2.isVisible = true end end function scene:create( event ) local sceneGroup = self.view introd = display.newText { text = "This is a text that will scroll by the screen etc etc lorem ipsum ... whatever", width = \_gW / 1.5, x = \_gW / 2, y = \_gH \* 2, font = native.newFont("MoriaCitadel.TTF"), align = "center", fontSize = \_gFontNormal / 1.3 } introd:setTextColor(0.2,0.2,0.7) logo = display.newImageRect("LOGO2.png", \_gW, \_gH) logo.x = \_gW / 2 logo.y = \_gH / 2 logo.isVisible = false screenBtn = widget.newButton { shape = "rect", fillColor = {default = { 0,0,0}}, width=\_gW, -- \* 2, height=\_gH, -- \* 2, label = "no text really needed", x = \_gW / 2, y = \_gH / 2 } screenBtn.isVisible = true screenBtn2 = widget.newButton { shape = "rect", fillColor = {default = { 0,0,0}}, width=\_gW, -- \* 2, height=\_gH, -- \* 2, label = "same as above", x = \_gW / 2, y = 100 --\_gH / 7 } screenBtn2.isVisible = false sceneGroup:insert( screenBtn ) sceneGroup:insert( screenBtn2 ) sceneGroup:insert( introd ) sceneGroup:insert( logo ) end function scene:show( event ) local phase = event.phase if phase == "will" then elseif phase == "did" then Runtime:addEventListener( "enterFrame", frameUpdate ) screenBtn:addEventListener( "touch", screenTouchListener ) screenBtn2:addEventListener( "touch", screen2TouchListener ) end end function scene:hide( event ) if event.phase == "will" then Runtime:removeEventListener( "enterFrame", frameUpdate ) elseif event.phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

The thing is, if the user touches the screen, so screenTouchListener (or screen2TouchListener) is called, I get the mentioned error.

Any advice most welcome.

Thanks in advance,

jaime

Well the error is saying that something is expecting a table and it’s getting a nil. I suspect this is due to an error in level1.lua and composer.gotoScene() is trying to load it and failing because of the error. These can be a bit harder to track down.  There is probably more useful information in your console log that could be useful.

Rob

Thanks for your reply, Rob.

However, nothing to do with level1 (I should have removed that line from the code for clarity).

Even with both listener functions (screenTouchListener and screen2TouchListener) reduced to a simple print( “…” ), the print gets done, and then the error pops.

Really looks as if something is happening in the return (end) of the listener function.

More useful information, maybe. Touching screenBtn (thus calling screenTouchListener), results in this:

"W" bad argument #1 to 'unpack' (table expected, got nil) stack traceback: [C]: in function 'unpack' ?: in function '\_setState' ?: in function '?' ?: in function \<?:449\> ?: in function \<?:169\> Debugging session completed (traced 0 instructions). Program completed in 7.30 seconds (pid: 3160).

Thanks again,

Jaime

Your buttons might be trying to set their “over” color which isn’t defined. So it’s trying to unpack a nil value. Remove the listeners and try it. It’ll probably still throw an exception.

Behind the scenes it’s essentially doing a widget:setFillColor( unpack( fillColor.over ) )

If you’re going to use widget.newButton() you shouldn’t provide your own touch listeners. The buttons have their own touch listeners. I think  @txzeenath found the problem though. You need to provide two color tables to widget.newButton.

Rob

Thank you so much!

Yes, just the “over” color missing, now working great. I should have spotted that myself :frowning:

Agreed having a “touch” listener for a button is not very standard, but should work - now it works just fine.

Thanks again for your help to you two!

Well the error is saying that something is expecting a table and it’s getting a nil. I suspect this is due to an error in level1.lua and composer.gotoScene() is trying to load it and failing because of the error. These can be a bit harder to track down.  There is probably more useful information in your console log that could be useful.

Rob

Thanks for your reply, Rob.

However, nothing to do with level1 (I should have removed that line from the code for clarity).

Even with both listener functions (screenTouchListener and screen2TouchListener) reduced to a simple print( “…” ), the print gets done, and then the error pops.

Really looks as if something is happening in the return (end) of the listener function.

More useful information, maybe. Touching screenBtn (thus calling screenTouchListener), results in this:

"W" bad argument #1 to 'unpack' (table expected, got nil) stack traceback: [C]: in function 'unpack' ?: in function '\_setState' ?: in function '?' ?: in function \<?:449\> ?: in function \<?:169\> Debugging session completed (traced 0 instructions). Program completed in 7.30 seconds (pid: 3160).

Thanks again,

Jaime

Your buttons might be trying to set their “over” color which isn’t defined. So it’s trying to unpack a nil value. Remove the listeners and try it. It’ll probably still throw an exception.

Behind the scenes it’s essentially doing a widget:setFillColor( unpack( fillColor.over ) )

If you’re going to use widget.newButton() you shouldn’t provide your own touch listeners. The buttons have their own touch listeners. I think  @txzeenath found the problem though. You need to provide two color tables to widget.newButton.

Rob

Thank you so much!

Yes, just the “over” color missing, now working great. I should have spotted that myself :frowning:

Agreed having a “touch” listener for a button is not very standard, but should work - now it works just fine.

Thanks again for your help to you two!