How to pass parameters in Storyboard?

I want to pass parameter (score) . I am using storyboard and I am using this score module that I found in coronalabs website: 

http://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/

in each scenes I have scoreText inside function scene:createScene( event ) : 

[lua]–create score text
scoreText = score.init({
fontSize = 20,
font = “Helvetica”,
x = display.contentCenterX,
y = 20,
maxDigits = 7,
leadingZeros = true,
filename = “score.txt”,
})
group:insert(scoreText)[/lua]

When I go back to main menu my score is still in the screen. 

Question: How do I pass parameters so that I can pass my updated score to all my scenes except the main menu and exit menu? 

That module does not return a display object.  You need to do:

group:insert(scoreText.scoreText)

But to answer your question, how do I pass parameters?  When you call storyboard.gotoScene() you can pass an options table to it like this:

local options = {

     effect = “crossFade”,

     time = 500,

     params = {

          yourKey = yourValue,

          yourOtherKey = yourOtherValue

     }

}

storyboard.gotoScene(“yourscene”, options)

Then inside yourscene’s createScene() and enterScene(), you get an event table.  That event table will have your params table in it:

function scene:createScene( event )

      local group = self.view

      local params = event.params

      print( params.yourKey)

      print( params.yourOtherKey)

end

Thank you Rob Miracle. 

Thank you! I appreciate it. 

What is the purpose of having another scoreText? 

I tried group:insert(scoreText.scoreText) and I get an error: 

 

 ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':'stack traceback:

 

Then I added the ‘:’ and I got the same error. 

It looks like you’re trying to insert a value and not a display object.

This works, but if try to insert scoreText you will get that error message.

local scoreText = 100 local scoreTextObject = display.newText( "Score:" .. scoreText, 100, 200, display.contentWidth, display.contentHeight \* 0.5, native.systemFont, 16 ) local dg = display.newGroup() dg:insert(scoreTextObject)

Best regards,

Tomas

Also, there’s a small bug (probably) that happens when you try to pass params to the new instance of an existing scene. So avoid that.

Thank you Tomas. 

The code you send me works.

But I want to pass the score value from the previous scene to another scene. When I return to my mainMenu scene the score still appears in the screen. 

I have this in all my scenes, so that I can display my score:  

[lua]function scene:createScene( event )

local group = self.view

local scoreText = score.init({
fontSize = 20,
font = “Helvetica”,
x = display.contentCenterX,
y = 20,
maxDigits = 7,
leadingZeros = true,
filename = “scorefile.txt”,
})
group:insert(scoreText)

end[/lua]

So now I will try passing score with gotoScence. If there is another way to do this please let me know. 

I appreciate your time. 

Shirley 

You must insert the text display object into the group:

When you’re about to change scene you you pass an options table with your parameters

local options = { effect = "crossFade", time = 100, params = { score = 1000, } } storyboard.gotoScene("scene\_game", options)

And in scene_game you print the score out to a text object and insert into the group:

function scene:enterScene( event ) local group = self.view local params = event.params local scoreText = display.newText( "Score: " .. params.score, 100, 200, native.systemFontBold, 24 ) group:insert(scoreText) --code here executes every time the scene is entered regardless --of it's creation state. end

Now that it’s inserted into the group it will only appear in the scene scene_game. If you change scene the scoreText all of the objects in the display group will disappear (not deleted though).

Best regards,

Tomas

Just to clarify, the module you took from the tutorial returns a generic object, not the display.newText() object.  That generic object has a member called “scoreText” which is the disiplay.newText() object on the screen.  You can’t insert generic objects into groups, only display objects.

Since you named your object “scoreText”  and that object has a member named “scoreText”, then scoreText.scoreTect should have been a reference to the display.newText() object that you could insert into a group.  You could have named your object billybob in which case it should have been billybob.scoreText.  I’m unsure why you got that error, but it looks like you have worked past it.

Rob

Thank you very much! 

Shirley 

Thank you very much for the details! 

Shirley

That module does not return a display object.  You need to do:

group:insert(scoreText.scoreText)

But to answer your question, how do I pass parameters?  When you call storyboard.gotoScene() you can pass an options table to it like this:

local options = {

     effect = “crossFade”,

     time = 500,

     params = {

          yourKey = yourValue,

          yourOtherKey = yourOtherValue

     }

}

storyboard.gotoScene(“yourscene”, options)

Then inside yourscene’s createScene() and enterScene(), you get an event table.  That event table will have your params table in it:

function scene:createScene( event )

      local group = self.view

      local params = event.params

      print( params.yourKey)

      print( params.yourOtherKey)

end

Thank you Rob Miracle. 

Thank you! I appreciate it. 

What is the purpose of having another scoreText? 

I tried group:insert(scoreText.scoreText) and I get an error: 

 

 ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':'stack traceback:

 

Then I added the ‘:’ and I got the same error. 

It looks like you’re trying to insert a value and not a display object.

This works, but if try to insert scoreText you will get that error message.

local scoreText = 100 local scoreTextObject = display.newText( "Score:" .. scoreText, 100, 200, display.contentWidth, display.contentHeight \* 0.5, native.systemFont, 16 ) local dg = display.newGroup() dg:insert(scoreTextObject)

Best regards,

Tomas

Also, there’s a small bug (probably) that happens when you try to pass params to the new instance of an existing scene. So avoid that.

Thank you Tomas. 

The code you send me works.

But I want to pass the score value from the previous scene to another scene. When I return to my mainMenu scene the score still appears in the screen. 

I have this in all my scenes, so that I can display my score:  

[lua]function scene:createScene( event )

local group = self.view

local scoreText = score.init({
fontSize = 20,
font = “Helvetica”,
x = display.contentCenterX,
y = 20,
maxDigits = 7,
leadingZeros = true,
filename = “scorefile.txt”,
})
group:insert(scoreText)

end[/lua]

So now I will try passing score with gotoScence. If there is another way to do this please let me know. 

I appreciate your time. 

Shirley 

You must insert the text display object into the group:

When you’re about to change scene you you pass an options table with your parameters

local options = { effect = "crossFade", time = 100, params = { score = 1000, } } storyboard.gotoScene("scene\_game", options)

And in scene_game you print the score out to a text object and insert into the group:

function scene:enterScene( event ) local group = self.view local params = event.params local scoreText = display.newText( "Score: " .. params.score, 100, 200, native.systemFontBold, 24 ) group:insert(scoreText) --code here executes every time the scene is entered regardless --of it's creation state. end

Now that it’s inserted into the group it will only appear in the scene scene_game. If you change scene the scoreText all of the objects in the display group will disappear (not deleted though).

Best regards,

Tomas

Just to clarify, the module you took from the tutorial returns a generic object, not the display.newText() object.  That generic object has a member called “scoreText” which is the disiplay.newText() object on the screen.  You can’t insert generic objects into groups, only display objects.

Since you named your object “scoreText”  and that object has a member named “scoreText”, then scoreText.scoreTect should have been a reference to the display.newText() object that you could insert into a group.  You could have named your object billybob in which case it should have been billybob.scoreText.  I’m unsure why you got that error, but it looks like you have worked past it.

Rob

Thank you very much! 

Shirley