Creating Composer scenes programmatically.

Hi there! we are creating scenes programatically too with this method.

Have you tried to build it for Android? In our case, the first time the application runs in the device we have no errors but corona detects that ccscene file is missing and generate a empty file sceneName.ccscene inside /data/data/com.example.appname/files/coronaResources/

Next times you run the app, corona tries to load the generated sceneName.ccscene causing an error:

I/Corona ( 3358): GOTO SCENE WelcomeView I/Corona ( 3358): Runtime error I/Corona ( 3358): ?:0: attempt to index a nil value I/Corona ( 3358): stack traceback: I/Corona ( 3358): ?: in function 'load' I/Corona ( 3358): ?: in function 'gotoScene' I/Corona ( 3358): F:\corona\Src\poc\Application.lua:109: in function 'showView' I/Corona ( 3358): F:\corona\Src\poc\controller\ApplicationController.lua:28: in function 'onRegister'

When you delete *.ccscene files from /data/data/com.example.appname/files/coronaResources/ the application runs without problems.

Is there any option to avoid this behavior??

Thanks!

Yes, that is a bug, we detected it as well.

To avoid it we use a workaround: we add a different random number to the scene name each time the app starts. That way the ccscene names are always different.

Good to see this thread is still active. Could someone who has this one page Composer generation scene working please post it here or a link? Have tried to follow Luisheranz’s example but keep getting errors as indicated above. Thanks.

@kilopop, post your code and we will try to help you out

Ok here’s a sample bare bones example. This is contained in a scene called sc1 which is called from main with composer.gotoScene. This produces the above error. [lua] local composer = require( “composer” ) local function dynamicSceneCreator( scene_name ) local scene = composer.newScene( scene_name ) function scene:create( event ) local sceneGroup = self.view end function scene:show( event ) local phase = event.phase if ( phase == “will” ) then print(“will”) elseif ( phase == “did” ) then – remove previous scene’s view if nil~= composer.getScene(“scene_2”) then composer.removeScene(“scene_2”, false) end end end function scene:hide( event ) local phase = event.phase if ( phase == “will” ) then print(“will”) elseif ( phase == “did” ) then print(“did”) end end function scene:destroy( event ) print(“destory”) end – ------------------------------------------------------------------------------- – Listener setup – Scene 1 scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) scene:addEventListener( “destroy”, scene ) --------------------------------------------------------------------------------- return scene end local function gotoDynamicScene( scene_name ) if composer.getScene( scene_name ) == nil then dynamicSceneCreator( scene_name ) end composer.gotoScene( scene_name ) end gotoDynamicScene(“scene_1”) [/lua]

You don’t need to use scenes in files anymore, you can put that whatever you want, in the main for example. So don’t call it using another composer.gotoScene.

Right thanks. How is the syntax of this code though, all in order?

Got a chance to try this and put everything in main.lua. Works perfectly! Thanks Luisherranz, doing composer this way keeps everything so manageable.

Thank you Luis! Works fine with a random number in scene name.

Do you know if there is a possibility to remove all the ccscene empty files when application starts? It would be great :slight_smile:

No problem.

I don’t know a way right now, but maybe somebody from Corona can have more information about this bug.

No problem  :slight_smile:

Dirty way to delete files :S

local lfs = require( "lfs" ) local path = system.pathForFile( ".." , system.DocumentsDirectory ) if path then lfs.chdir(path) path = lfs.currentdir().."/files/coronaResources" if not lfs.chdir(path) then return end for file in lfs.dir(path) do if string.find( file, ".ccscene" ) ~= nil then local results, reason = os.remove( system.pathForFile( file, path )) end end end

can the ccscene’s be prevented? perhaps try

scene._composerFileName = “”

or

scene._composerFileName = nil

just before returning the dynamic scene

scene._composerFileName = nil works! xD

Thkx! 

Hi Luisherranz, your dynamic scene creation method seems really useful. We are currently using the ping pong method between 2 composer scenes, filling create scene with new data each time. But all on one page would be much tidier.

At this point we get a littany of errors but mainly “attempt to concatenate global ‘sceneName’ (a nil value)” which is coming from the original composer.gotoScene(“scene_1”, options) in main.lua…

Could you possibly post a working example of your one page composer solution? The first error we got was that composer didn’t exist when running the gotoDynamicScene so we required composer before the dynamicSceneCreator function. But maybe this means we have set the page up in correctly against your example?

Anyway, a working example would be most appreciated and in fact should be the main method as how to use composer. Why bother repeating the same structure over 26 scenes in our case if one page populated with new data will work.

Thanks Luisherranz.

Hi.

Added this GitHub repo if someone needs it. It’s a barebone project for dynamic Composer scene creation:

https://github.com/janfma/dynamicComposer

Frode

Hi there! we are creating scenes programatically too with this method.

Have you tried to build it for Android? In our case, the first time the application runs in the device we have no errors but corona detects that ccscene file is missing and generate a empty file sceneName.ccscene inside /data/data/com.example.appname/files/coronaResources/

Next times you run the app, corona tries to load the generated sceneName.ccscene causing an error:

I/Corona ( 3358): GOTO SCENE WelcomeView I/Corona ( 3358): Runtime error I/Corona ( 3358): ?:0: attempt to index a nil value I/Corona ( 3358): stack traceback: I/Corona ( 3358): ?: in function 'load' I/Corona ( 3358): ?: in function 'gotoScene' I/Corona ( 3358): F:\corona\Src\poc\Application.lua:109: in function 'showView' I/Corona ( 3358): F:\corona\Src\poc\controller\ApplicationController.lua:28: in function 'onRegister'

When you delete *.ccscene files from /data/data/com.example.appname/files/coronaResources/ the application runs without problems.

Is there any option to avoid this behavior??

Thanks!

Yes, that is a bug, we detected it as well.

To avoid it we use a workaround: we add a different random number to the scene name each time the app starts. That way the ccscene names are always different.

Good to see this thread is still active. Could someone who has this one page Composer generation scene working please post it here or a link? Have tried to follow Luisheranz’s example but keep getting errors as indicated above. Thanks.

@kilopop, post your code and we will try to help you out