Save High Score

I was wondering how I can save this score each time the user gets a high score?

– Variables
currentScore = 0
local scoreTxt = display.newText( currentScore , display.contentWidth/2+100, display.contentHeight/2-250, “MANIFESTO”, 65)
scoreTxt:setFillColor( 0, 1, 1 )

– Listener for your timer, updates score variable and updates the text
local function scoreKeeper( event )
    currentScore = currentScore + 1
    scoreTxt.text = currentScore
end
 
 scoreTimer = timer.performWithDelay( 1000, scoreKeeper, -1 )

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

Rob

Yes, I read it earlier.  The problem is, I don’t know how to implement this in my game.  I’ve only been with Corona for about two weeks.  I’m very excited about this game, I think it will make my family really proud.  I’m just looking for some help.  If you don’t want to help, never mind.  Sorry for wasting your time.

In general you need two things:

  1. a variable to hold the numeric value of the score.  This is something you can do math with.

  2. a display.newText() object that you can show the score in.

Increment the score any way you choose.  After you increment it, update the text object with the new value.

Now there are a few gotcha’s.  First, if you’re using multiple scenes with something like Composer or Storyboard, you need a way to pass that score around (and potentially the text object, though generally that just shows on the game screen, you may want to have other display.newText’s on  your game over, high score screens etc.

The easy thing to do is make score a global, but that’s bad practice.  It’s best to have a table that you require in every scene that has your variables you want to pass around every where.

Finally you may want to have a way to save the high score out so that the next time the app runs, you can remember what the previous high score was.

These concepts are pretty simple:

score = score + someAmount

scoreText.text = score

Of course you have to create the scoreText, position it, etc. and make sure it’s available when you want to change your score.  That tutorial tries to do a lot of this for you.

Rob

If I just use or edit the code on that link you posted, will the highscore be remembered each time the user opens or closes the app?

Thanks for the helpful tips!  I used the high score code you directed me to, and I almost have it working.  My only problem now is that the “score” variable is a table.  When I try to perform math on it, such as :

if   currentScore > score

then

socre = currentScore

end

it gives me an error saying it can’t perform math on a table value.  Any advice?

One of the two values is a table not a number.  If “currentScore” is a display.newText() that would explain it being a table value.  But we don’t see enough of your code to know what currentScore and score are.  You can use the print() function to print out the values and see which one is a number and which one is a table, then you can look back and see why it’s a table not a number.

Rob

The problem is fixed. Peach Pellen helped me.

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

Rob

Yes, I read it earlier.  The problem is, I don’t know how to implement this in my game.  I’ve only been with Corona for about two weeks.  I’m very excited about this game, I think it will make my family really proud.  I’m just looking for some help.  If you don’t want to help, never mind.  Sorry for wasting your time.

In general you need two things:

  1. a variable to hold the numeric value of the score.  This is something you can do math with.

  2. a display.newText() object that you can show the score in.

Increment the score any way you choose.  After you increment it, update the text object with the new value.

Now there are a few gotcha’s.  First, if you’re using multiple scenes with something like Composer or Storyboard, you need a way to pass that score around (and potentially the text object, though generally that just shows on the game screen, you may want to have other display.newText’s on  your game over, high score screens etc.

The easy thing to do is make score a global, but that’s bad practice.  It’s best to have a table that you require in every scene that has your variables you want to pass around every where.

Finally you may want to have a way to save the high score out so that the next time the app runs, you can remember what the previous high score was.

These concepts are pretty simple:

score = score + someAmount

scoreText.text = score

Of course you have to create the scoreText, position it, etc. and make sure it’s available when you want to change your score.  That tutorial tries to do a lot of this for you.

Rob

If I just use or edit the code on that link you posted, will the highscore be remembered each time the user opens or closes the app?

Thanks for the helpful tips!  I used the high score code you directed me to, and I almost have it working.  My only problem now is that the “score” variable is a table.  When I try to perform math on it, such as :

if   currentScore > score

then

socre = currentScore

end

it gives me an error saying it can’t perform math on a table value.  Any advice?

One of the two values is a table not a number.  If “currentScore” is a display.newText() that would explain it being a table value.  But we don’t see enough of your code to know what currentScore and score are.  You can use the print() function to print out the values and see which one is a number and which one is a table, then you can look back and see why it’s a table not a number.

Rob

The problem is fixed. Peach Pellen helped me.