composer:access data from another scene

hi,

I have followed the documentation about the access of value from another scene :

https://docs.coronalabs.com/guide/system/composer/index.html

But i receive a nill value in my menu.lua when i print(params.someKey), it’s the data of my game.lua. I put this print() in my scene:show. normaly i should receive the return : 1.

what i do wrong ? Thanks for your assist.

a part of my game.lua

local composer = require( "composer" ) local scene = composer.newScene() local background={} local circle={} local count=1 local options = { effect="slideRight", time=1000, params = { someKey=count, someOtherKey=2 } } local function randomSide() sidex=math.random(0,500) sidey=math.random(0,500) end local function displacement() count=count+1 print("displacement",count) circle:setFillColor(0,0,0) randomSide() local function displacement2() transition.to(circle,{time=2000, x=sidex, y=sidey, onComplete=displacement}) end displacement2() end --------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE -- unless "composer.removeScene()" is called. --------------------------------------------------------------------------------- -- local forward references should go here --------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view background=display.newRect(0,0,5000,5000) background:setFillColor(1,1,1) sceneGroup:insert(background) circle = display.newCircle(display.contentWidth\*.5,display.contentHeight\*.5,50) circle:setFillColor(1,0,0) sceneGroup:insert(circle) timer.performWithDelay(2000,displacement) local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) composer.gotoScene( "menu", options ) end end ..............

and my menu.lua

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") ..... function scene:show( event ) local sceneGroup = self.view local phase = event.phase local params = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). print(params.someKey) 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 ...........

In menu.lua:

local params = event .phase

should be

local params = event .params

Rob

Thanks Rob,

But it don’t work.

attempt ton index local ‘params’ (a nil value)

--part of menu.lua function scene:show( event )     local sceneGroup = self.view     local phase = event.phase     local params = event.params     if ( phase == "will" ) then       -- Called when the scene is still off screen (but is about to come on screen).     print(params.someKey)     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

Perhaps you could download this project:

https://github.com/coronalabs/sample-game-project

The gameover.lua scene goes back to your main.lua scene and shows how it works. I’m not seeing enough of your code to see the problem.   The code in the sample uses a table printing function in utility.lua to dump the event table to make sure you’re passing things correctly.

Maybe you can print the values of your options table just before you call composer.gotoScene() in  your button handler.

Rob

hi,

the error is in my main.lua

When i do

local options = { effect="slideRight", time=1000, params = { someKey=4, someOtherKey=2 } } print(options.someKey) --result = nill

the easy way is store my value in a table with a external module like your example above.

For the information to beginners :

--data.lua local myTable={ level=1 time=10 } return myTable

--main.lua or other local myTable=require("data") print(mytable.level) --result is 1

Thanks for your advice and good night.

Hi @espace3d,

In this line…

[lua]

print(options.someKey)

[/lua]

…this is “nil” because there is no such value directly within “options”. You’ve put the “someKey” variable inside of the “params” table (inside of “options”), so to get it, you need to call:

[lua]

print(options.params.someKey)

[/lua]

Brent

Keep in mind that in your scenes, the values are either referenced:

event.params.someKey

or

local params = event.params params.someKey

Rob

ok thanks for the explanation.

In menu.lua:

local params = event .phase

should be

local params = event .params

Rob

Thanks Rob,

But it don’t work.

attempt ton index local ‘params’ (a nil value)

--part of menu.lua function scene:show( event )     local sceneGroup = self.view     local phase = event.phase     local params = event.params     if ( phase == "will" ) then       -- Called when the scene is still off screen (but is about to come on screen).     print(params.someKey)     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

Perhaps you could download this project:

https://github.com/coronalabs/sample-game-project

The gameover.lua scene goes back to your main.lua scene and shows how it works. I’m not seeing enough of your code to see the problem.   The code in the sample uses a table printing function in utility.lua to dump the event table to make sure you’re passing things correctly.

Maybe you can print the values of your options table just before you call composer.gotoScene() in  your button handler.

Rob

hi,

the error is in my main.lua

When i do

local options = { effect="slideRight", time=1000, params = { someKey=4, someOtherKey=2 } } print(options.someKey) --result = nill

the easy way is store my value in a table with a external module like your example above.

For the information to beginners :

--data.lua local myTable={ level=1 time=10 } return myTable

--main.lua or other local myTable=require("data") print(mytable.level) --result is 1

Thanks for your advice and good night.

Hi @espace3d,

In this line…

[lua]

print(options.someKey)

[/lua]

…this is “nil” because there is no such value directly within “options”. You’ve put the “someKey” variable inside of the “params” table (inside of “options”), so to get it, you need to call:

[lua]

print(options.params.someKey)

[/lua]

Brent

Keep in mind that in your scenes, the values are either referenced:

event.params.someKey

or

local params = event.params params.someKey

Rob

ok thanks for the explanation.