Passing parameters to require a file

I’m trying to use “params” in composer to pass information to another scene (depending on which button the user selected) and then use the params to require a file.  Initially this was working when I was doing it in scene:create (picture.lua), but now that I moved it to scene:show I’m getting the error “attempt to index local ‘params’ (a nil value)” .  Is there something I’m doing wrong or is this a bug?  I simplified all the files (below) and still had the error with today’s daily build.  Thanks for the help.

main.lua

[lua]
local composer = require( “composer” )

local widget = require( “widget” )

local function onButtonSelect(event)

firstVisit = false

local options = {

effect = “fade”,

  time = 500,

  params = {

      requiredFile = event.target.fileName

      }

}

composer.gotoScene( “picture”, options )

return true

end

local button1 = widget.newButton

{

    label = “button 1”,

    onEvent = onButtonSelect,

    emboss = false,

    --properties for a rounded rectangle button…

    shape=“roundedRect”,

    width = 200,

    height = 40,

    cornerRadius = 2,

    fillColor = { default={ 1, 0, 0, 1 }, over={ 1, 0.1, 0.7, 0.4 } },

    strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },

    strokeWidth = 4

}

button1.x = display.contentCenterX

button1.y = 100

button1.fileName = “file1”

local button2 = widget.newButton

{

    label = “button 2”,

    onEvent = onButtonSelect,

    emboss = false,

    --properties for a rounded rectangle button…

    shape=“roundedRect”,

    width = 200,

    height = 40,

    cornerRadius = 2,

    fillColor = { default={ 1, 0, 0, 1 }, over={ 1, 0.1, 0.7, 0.4 } },

    strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },

    strokeWidth = 4

}

button2.x = display.contentCenterX

button2.y = 200

button2.fileName = “file2”

[/lua]

picture.lua

[lua]
local composer = require( “composer” )

local scene = composer.newScene()

– “scene:create()”

function scene:create( event )

    local sceneGroup = self.view

end

– “scene:show()”

function scene:show( event )

    local sceneGroup = self.view

    local phase = event.phase

    --PARAMETERS passed in from the menu

local params = event.params

–REQUIRE the MODULE

local m = require (params.requiredFile)

    if ( phase == “will” ) then

        – Called when the scene is still off screen (but is about to come on screen).

    elseif ( phase == “did” ) then

        – Called when the scene is now on screen.

        – Insert code here to make the scene come alive.

        – Example: start timers, begin animation, play audio, etc.

    end

end

– “scene:hide()”

function scene:hide( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

    elseif ( phase == “did” ) then

    end

end

– “scene:destroy()”

function scene:destroy( event )

    local sceneGroup = self.view

end


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene
[/lua]

file1.lua

[lua]
local t = {}

print( “file1.lua has been loaded.” )

return t
[/lua]

file2.lua

[lua]
local t = {}

print( “file2.lua has been loaded.” )

return t
[/lua]

Try changing your code like this:

local fileToRequire function scene:create( event ) local sceneGroup = self.view fileToRequire = event.params.requiredFile end function scene:show( event ) local sceneGroup = self.view local phase = event.phase local m = require( fileToRequire ) -- ... end function scene:hide( event ) end function scene:destroy( event ) fileToRequire = nil end

The only reason I am not accessing event.params in the create block is because I want to access it each time I come in from the menu.  Create only runs when the file is first created.  Is there a reason it is not working in the show block?  Initially I was destroying the scene each time I came out so that it would run create again, but the second time I would go in, I was getting a nil error while trying to insert into a group (even though I printed both the object and the group and they weren’t nil).  Thanks for the help.

I ended up using a global variable because I couldn’t figure out how to get params to work in scene:show.  (I got the same error when I changed the require to a print statement.)

Try changing your code like this:

local fileToRequire function scene:create( event ) local sceneGroup = self.view fileToRequire = event.params.requiredFile end function scene:show( event ) local sceneGroup = self.view local phase = event.phase local m = require( fileToRequire ) -- ... end function scene:hide( event ) end function scene:destroy( event ) fileToRequire = nil end

The only reason I am not accessing event.params in the create block is because I want to access it each time I come in from the menu.  Create only runs when the file is first created.  Is there a reason it is not working in the show block?  Initially I was destroying the scene each time I came out so that it would run create again, but the second time I would go in, I was getting a nil error while trying to insert into a group (even though I printed both the object and the group and they weren’t nil).  Thanks for the help.

I ended up using a global variable because I couldn’t figure out how to get params to work in scene:show.  (I got the same error when I changed the require to a print statement.)