Sending data between scenes, need help?

Hi guys,

I’m trying to send data between scenes but having an issue on how to go about doing it. What i’m attempting to do is the user enters text into a native.newTextField and the input data will be sent across to a different scene when loaded and is displayed as a display.newText on the scene.

Example:

The user enters “HelloWorld” in the textbox on the first scene.

The user then navigates to the next scene.

At the top of the next scene is says “HelloWorld” or what ever the user enters.

Be a big help is anyone knows how the best way to go about doing this is.

Thanks again,

Matt.

Three Four options:

  1. Use a global (not great, but easy)

  2. Use a common module.  i.e. Create a module, require it in both scenes, and have the scenes store their data in the module. (See Rob’s note below for link to example/article.)

  3. Assuming you’re using composer scene library (not the tool; also if you’re using storyboard, switch; that is old) use the params option to pass data to the next scene: http://docs.coronalabs.com/daily/api/library/composer/gotoScene.html#scene-options

  4. kilopop’s suggestion (actually same as 2 w/o need to make new module; just use the composer module/library).  Good suggestion.  (just be cautious about name conflicts)

For @rominggamer’s #2 suggestion:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob

If you’re using composer for instance.

local composer = require"composer"

Then…

[lua] composer.myVar1 = “whatever”
composer.myNum = 64 [/lua]

And they persist in any module or scene as long as you require composer.

This is what I have come up with but i’m still having an issue getting it to function, on page1.lua its not changing the variable, and on page2.lua it’s still not setting the display.newText to myData.myVariable.

myData.lua

[lua]

–my global space

local myData = {}

myVariable = “”

return myData

[/lua]

page1.lua

[lua]

local composer = require( “composer” ) 

local myData = require( “mydata” )

local widget = require( “widget” )

local scene = composer.newScene() 

local function btnNext( event )

    composer.gotoScene( “page2”, { effect=“slideLeft”, time=800,} )

    myData.myVariable = textName

    print( myVariable )

end

function scene:create( event )

local sceneGroup = self.view

–Background

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 )

sceneGroup:insert(bg)

–textName

local textName = display.newText(“Name:”, 

173, 210, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 18 )

textName:setFillColor( 0, 0, 0 )

sceneGroup:insert( textName )

–textBoxName

local textBoxName = native.newTextField( 160, 130, 300, 40 )

textBoxName.size = 20

textBoxName:addEventListener( “userInput”, textBoxName )

sceneGroup:insert( textBoxName )

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = btnNext

}

btnNext.x = 235

btnNext.y = 445

sceneGroup:insert( btnNext )

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

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

[/lua]

page2.lua

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local myData = require( “mydata” )

function scene:create( event )

local sceneGroup = self.view

–Background

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 )

sceneGroup:insert( bg )

–textNamelbl

local textNamelbl = display.newText(myData.myVariable, 

172, 200, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textNamelbl:setFillColor( 0, 0, 0 )

sceneGroup:insert( textNamelbl )

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

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

[/lua]

Thanks,

Matt.

check the option 2 post again.

I think your problem is not using myData[dot].

  1. local myData = {}
  2.  
  3. myData.myVariable = “”
  4.  
  5. return myData

T.

Just a follow up on @kilopop’s suggestion.  While that will indeed work, you have to be aware that it is not safe.

We might already be using:

composer.someNumber

or in the future we might choose to use composer.someNumber.  Because of that we added two methods to the composer library:  .setVariable and .getVariable where you can set your own key-value pairs using composer and the inbetwee-scene transport method is a future safe method. 

Rob

Hi T,

I am using the myData.lua which has the 

[lua]

–my global space

local myData = {}

myVariable = “”

return myData

[/lua]

I had a look in the console and it printed so I think the issue is in the page1.lua and page2.lua with the setting and the getting of the variable. Any ideas?

Thanks again,

Matt.

Did you follow ToeKnee’s advice and change line 4 of mydata.lua to read:   myData.myVariable = “”

Sorry i didn’t realise that was what was meant. I have now tested it in the simulator by putting a value in the myData.myVariable and sure enough it appeared on page2.lua which is exactly what I wanted to happen. However, I couldn’t test my textField as its not supported in the simulator so I then built it leaving the myData.myVariable = “” and tested it on my device and a small issue appeared. The textField on page1.lua didn’t send the data across to myData.lua when btnNext was clicked as it didn’t appear on page2.lua, also I got an error message when I left the textField blank and clicked btnNext is there a way that will disable the button until there is something entered in the textField. My function code for my button and textField looks like this…

[lua]

local function btnNext( event )

    composer.gotoScene( “page2”, { effect=“slideLeft”, time=800,} )

    myData.myVariable = texBoxtName

end

[/lua]

[lua]

local textBoxName = native.newTextField( 160, 130, 300, 40 )

    textBoxName.size = 20

    textBoxName:addEventListener( “userInput”, textBoxName )

    sceneGroup:insert( textBoxName )

[/lua]

Thanks again,

Matt.

Three Four options:

  1. Use a global (not great, but easy)

  2. Use a common module.  i.e. Create a module, require it in both scenes, and have the scenes store their data in the module. (See Rob’s note below for link to example/article.)

  3. Assuming you’re using composer scene library (not the tool; also if you’re using storyboard, switch; that is old) use the params option to pass data to the next scene: http://docs.coronalabs.com/daily/api/library/composer/gotoScene.html#scene-options

  4. kilopop’s suggestion (actually same as 2 w/o need to make new module; just use the composer module/library).  Good suggestion.  (just be cautious about name conflicts)

For @rominggamer’s #2 suggestion:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob

If you’re using composer for instance.

local composer = require"composer"

Then…

[lua] composer.myVar1 = “whatever”
composer.myNum = 64 [/lua]

And they persist in any module or scene as long as you require composer.

This is what I have come up with but i’m still having an issue getting it to function, on page1.lua its not changing the variable, and on page2.lua it’s still not setting the display.newText to myData.myVariable.

myData.lua

[lua]

–my global space

local myData = {}

myVariable = “”

return myData

[/lua]

page1.lua

[lua]

local composer = require( “composer” ) 

local myData = require( “mydata” )

local widget = require( “widget” )

local scene = composer.newScene() 

local function btnNext( event )

    composer.gotoScene( “page2”, { effect=“slideLeft”, time=800,} )

    myData.myVariable = textName

    print( myVariable )

end

function scene:create( event )

local sceneGroup = self.view

–Background

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 )

sceneGroup:insert(bg)

–textName

local textName = display.newText(“Name:”, 

173, 210, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 18 )

textName:setFillColor( 0, 0, 0 )

sceneGroup:insert( textName )

–textBoxName

local textBoxName = native.newTextField( 160, 130, 300, 40 )

textBoxName.size = 20

textBoxName:addEventListener( “userInput”, textBoxName )

sceneGroup:insert( textBoxName )

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = btnNext

}

btnNext.x = 235

btnNext.y = 445

sceneGroup:insert( btnNext )

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

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

[/lua]

page2.lua

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local myData = require( “mydata” )

function scene:create( event )

local sceneGroup = self.view

–Background

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 )

sceneGroup:insert( bg )

–textNamelbl

local textNamelbl = display.newText(myData.myVariable, 

172, 200, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textNamelbl:setFillColor( 0, 0, 0 )

sceneGroup:insert( textNamelbl )

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

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

[/lua]

Thanks,

Matt.

check the option 2 post again.

I think your problem is not using myData[dot].

  1. local myData = {}
  2.  
  3. myData.myVariable = “”
  4.  
  5. return myData

T.

Just a follow up on @kilopop’s suggestion.  While that will indeed work, you have to be aware that it is not safe.

We might already be using:

composer.someNumber

or in the future we might choose to use composer.someNumber.  Because of that we added two methods to the composer library:  .setVariable and .getVariable where you can set your own key-value pairs using composer and the inbetwee-scene transport method is a future safe method. 

Rob

Hi T,

I am using the myData.lua which has the 

[lua]

–my global space

local myData = {}

myVariable = “”

return myData

[/lua]

I had a look in the console and it printed so I think the issue is in the page1.lua and page2.lua with the setting and the getting of the variable. Any ideas?

Thanks again,

Matt.

Did you follow ToeKnee’s advice and change line 4 of mydata.lua to read:   myData.myVariable = “”

Sorry i didn’t realise that was what was meant. I have now tested it in the simulator by putting a value in the myData.myVariable and sure enough it appeared on page2.lua which is exactly what I wanted to happen. However, I couldn’t test my textField as its not supported in the simulator so I then built it leaving the myData.myVariable = “” and tested it on my device and a small issue appeared. The textField on page1.lua didn’t send the data across to myData.lua when btnNext was clicked as it didn’t appear on page2.lua, also I got an error message when I left the textField blank and clicked btnNext is there a way that will disable the button until there is something entered in the textField. My function code for my button and textField looks like this…

[lua]

local function btnNext( event )

    composer.gotoScene( “page2”, { effect=“slideLeft”, time=800,} )

    myData.myVariable = texBoxtName

end

[/lua]

[lua]

local textBoxName = native.newTextField( 160, 130, 300, 40 )

    textBoxName.size = 20

    textBoxName:addEventListener( “userInput”, textBoxName )

    sceneGroup:insert( textBoxName )

[/lua]

Thanks again,

Matt.