Trouble displaying the score in the gameover

Hey guys so I’ve been using this tutorial for my score.

https://docs.coronalabs.com/tutorial/games/keepScores/index.html

All works well in the actual game, when I use score.add( value ) it adds and when I use score.set( value ) that works as well.

The problem I’m having is the fact that when I use this in the gameover.lua,

 local scoreText = score.init( { fontSize = 40, x = 130, y = 130, maxDigits = 7, leadingZeros = false })

to display the score as I used it in my game.lua, the score changes during the game, but it will turn to 0 in the gameover.lua and it will not fade in with rest of the scene despite being under show scene.

And if I try to use this to display the score,

local score = display.newText( sceneGroup, "SCORE: " ..score )

I’ll receive the error attempt to concatenate upvalue ‘score’ (a table value).

Hey.

Could you describe what you are doing in gameover.lua? How is it related to your game.lua?

I didn’t read the tutorial in detail, but it seems to me, that this is a module for displaying and saving a variable in your current scene. If you want to pass this variable to another scene, you would have to use composer.setVariable(‘finalScore’, score.get()) in one scene, and composer.getVariable(‘finalScore’) in another scene.

Greetings,

prjoh

Essentially I’m just trying to display the score that the player got during the game in the gameover.lua but the problem is i can’t display the text without an error.

local score = display.newText( sceneGroup, "SCORE: " ..score )

If you’ve followed that tutorial to a letter, then you shouldn’t be touching  score  variable like that. It is just a module, not the actual display object. You create the score display object with  scoreText  and that is what you should be manipulating.

Without seeing any more of your code, I’d guess that you are seeing that error due to scope, i.e. you create the display object in game scene, but then you try to access it in gameover scene. Simply put,  scoreText does not exist in gameover scene. You could pass a reference of it to the gameover scene via composer’s parameters table or recreate the score display object in the gameover scene.

 

So I made progress with your solution, I passed the score through params and I set the score = event.params.score in the gameover.lua. That worked but it only displays the score of the first game. I even set it up to print the event.params.score in the console and it only prints the score from the first game as well, nothing prints in games after. 

Well, that’s a month of blast from the past.

Your issue is still probably just about scope. I can’t be sure or help you any more because I have no idea what you’ve done or what you are trying to do.

Sorry I couldn’t test your solution for awhile cause i was busy with school.

in my game.lua I have this for when the player collides with another car

local function onCollision(event) if event.phase == "began" then composer.gotoScene( "gameover",{ time=400, effect="crossFade", params=score } ) score.save() end

and in my gameover.lua I have to display the score the player got in the game

local score = score function scene:create( event ) local sceneGroup = self.view print( event.params.score ) local score = event.params.score local background = display.newImageRect( sceneGroup,"bg.png", 750, 900) local gameoverscore = display.newText ( sceneGroup, "SCORE: " ..score, 100, 340 )

what happens is everything will work fine and the score will display the first time, but when i pres the replay to play the game again and lose again the value i got before stays and a new value won’t print or display

Code in the create() scene runs once, when the scene is initially loaded (or if you manually unload and reload).

Grabbing the value of score should go in the show() event, in the “will” phase.  The show() event runs every time the scene is ready to be shown.

I would also define background and gameoverscore variables outside of the show() event so it is available to the entire scene.

Wow you guys are extremely helpful, i didn’t know that about create scene and show scene!

This worked, only one problem, the numbers will show the score and reset but the previous score can be seen under the current number.

Did you just move the code from start() to create()?  Then I would expect it to do what you are seeing… you are declaring a new variable each time the scene is shown.

You are most likely not understanding variable scope.  You declare these variables one time, and assign values to them when the scene is displayed.

My suggestions:

Declare the background and gameoverscore variables at the top of the scene… outside any functions.  This will make the variables have scene scope.

In start() create your background.  This runs once.  Also, create your score text with an empty value.

In show(), add code to get the score and place into the text string.

Something like this (code not tested)

local background local gameoverscore function scene:start(event) background = display.newImageRect(sceneGroup, 750, 900) gameoverscore = display.newText(sceneGroup, "", 100, 340) end function scene:show(event) if event.phase == "will" then gameoverscore.text = "SCORE: "..event.params.score end end

Thank you, it worked and you helped me understand the program much better. :slight_smile:

Hey.

Could you describe what you are doing in gameover.lua? How is it related to your game.lua?

I didn’t read the tutorial in detail, but it seems to me, that this is a module for displaying and saving a variable in your current scene. If you want to pass this variable to another scene, you would have to use composer.setVariable(‘finalScore’, score.get()) in one scene, and composer.getVariable(‘finalScore’) in another scene.

Greetings,

prjoh

Essentially I’m just trying to display the score that the player got during the game in the gameover.lua but the problem is i can’t display the text without an error.

local score = display.newText( sceneGroup, "SCORE: " ..score )

If you’ve followed that tutorial to a letter, then you shouldn’t be touching  score  variable like that. It is just a module, not the actual display object. You create the score display object with  scoreText  and that is what you should be manipulating.

Without seeing any more of your code, I’d guess that you are seeing that error due to scope, i.e. you create the display object in game scene, but then you try to access it in gameover scene. Simply put,  scoreText does not exist in gameover scene. You could pass a reference of it to the gameover scene via composer’s parameters table or recreate the score display object in the gameover scene.

 

So I made progress with your solution, I passed the score through params and I set the score = event.params.score in the gameover.lua. That worked but it only displays the score of the first game. I even set it up to print the event.params.score in the console and it only prints the score from the first game as well, nothing prints in games after. 

Well, that’s a month of blast from the past.

Your issue is still probably just about scope. I can’t be sure or help you any more because I have no idea what you’ve done or what you are trying to do.

Sorry I couldn’t test your solution for awhile cause i was busy with school.

in my game.lua I have this for when the player collides with another car

local function onCollision(event) if event.phase == "began" then composer.gotoScene( "gameover",{ time=400, effect="crossFade", params=score } ) score.save() end

and in my gameover.lua I have to display the score the player got in the game

local score = score function scene:create( event ) local sceneGroup = self.view print( event.params.score ) local score = event.params.score local background = display.newImageRect( sceneGroup,"bg.png", 750, 900) local gameoverscore = display.newText ( sceneGroup, "SCORE: " ..score, 100, 340 )

what happens is everything will work fine and the score will display the first time, but when i pres the replay to play the game again and lose again the value i got before stays and a new value won’t print or display

Code in the create() scene runs once, when the scene is initially loaded (or if you manually unload and reload).

Grabbing the value of score should go in the show() event, in the “will” phase.  The show() event runs every time the scene is ready to be shown.

I would also define background and gameoverscore variables outside of the show() event so it is available to the entire scene.

Wow you guys are extremely helpful, i didn’t know that about create scene and show scene!

This worked, only one problem, the numbers will show the score and reset but the previous score can be seen under the current number.

Did you just move the code from start() to create()?  Then I would expect it to do what you are seeing… you are declaring a new variable each time the scene is shown.

You are most likely not understanding variable scope.  You declare these variables one time, and assign values to them when the scene is displayed.

My suggestions:

Declare the background and gameoverscore variables at the top of the scene… outside any functions.  This will make the variables have scene scope.

In start() create your background.  This runs once.  Also, create your score text with an empty value.

In show(), add code to get the score and place into the text string.

Something like this (code not tested)

local background local gameoverscore function scene:start(event) background = display.newImageRect(sceneGroup, 750, 900) gameoverscore = display.newText(sceneGroup, "", 100, 340) end function scene:show(event) if event.phase == "will" then gameoverscore.text = "SCORE: "..event.params.score end end