Help Creating a Score Counter and saving Highscore

@Alex & @Rob

I agree with your statements to a certain extent.

I spent 6 months trying to learn Java and O.O.P. before I stumbled upon Corona.

I then spent 3 months learning Corona but I learned nothing until I threw myself in the deep end and decided to make the game I wanted to by learning as I went. That’s where the forums are invaluable, I’ve learned 90% of what I know now from kind and patient people like Rob and other people like myself. After 18 months I still have problems with the tutorials from time to time, Rob can vouch for that.

Alex you have just pointed out what the problem for new starters is yourself, there is a lot of information in the tutorial, stack overload!

Look how I explained creating and saving a high score, just the absolute minimum to start off with to get the job done.

Step1 - create a text file, read the text file, save it to the documents directory.

Step 2 - load the high score table

Step 3 - Check to see if your score is higher than the highScore and save it

Simple but effective.

Now when Spid3rtech has got his head around the absolute basics as I gave him he can then understand better ways to do it like have a table to hold the top 10 scores, hence a json file, hence Rob’s loadsave.lua.

When I first started with Corona me and Rob were going back and forth with his loadsave.lua module, I spent days trying to make it work and it is a simple module to work with, good work by the way Rob.

Tiny steps.

I agree with what QuizMaster has to say… i wasn’t learning anything until i threw my self in the deep end… well there’s different people in this world. Hey @QuizMaster have you released your game yet? Im pretty exited to play it.!

So that’s what your busy with :stuck_out_tongue: Sorry for taking up your time, and thanks! For letting me take up your time xD

Hi Sonic, nice to hear from you again.

I’m a week or two away from completing, I wanted it done by my birthday so I could celebrate but that’s next Wednesday.

I’ve been working on it every single day since March last year!

Spid3rtech, happy to help as long as it’s not too much!

I hope it isn’t! Thanks with everything so far.

local scoreTimer local score = 0 local updateScore = function() score = score + 1 end scoreTimer = timer.performWithDelay(1000,updateScore,-1) --when you detect collision timer.cancel(scoreTimer)

Thanks for the reply. I tried it out but It’s just the same, no text or anything.

I do have this:

local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value local removeScene if other == 1 then display.remove(Player) --display.remove(box[i]) audio.play( soundTable["Dead"] ) timer.cancel( boxTimer ) timer.cancel( box2Timer ) timer.cancel( box3Timer ) --timer.cancel( box4Timer ) --Runtime:addEventListener("enterFrame", cancelMaybeTimer ) display.remove(scoreTxt) Runtime:addEventListener("enterFrame", resetScore) Runtime:addEventListener("enterFrame", cancelTimer) Runtime:removeEventListener( "accelerometer", onAccelerate ) --timer.cancel( scoreText ) --Player:removeEventListener("touch", scoreText) --composer.removeScene("Game1") composer.gotoScene( "GameOver", {time = 500, effect = "zoomOutIn"}) --Runtime:addEventListener( "collision", changeScene ) composer.removeScene("Game1") end return true end end 

and I put

timer.cancel(scoreTimer)

into the onCollision code, but everything is the same. No Score or anything…

I don’t see where you are creating your score text object, you are doing this?

local options = { text = "", x = 100, y = 200, width = 128, font = native.systemFontBold, fontSize = 18, align = "right" } local myText = display.newText( options ) myText:setFillColor( 1, 0, 0 ) -- and more importantly -- myText.text = score

Alright, so here is the entire “Game1.lua” code:

EDIT: I removed the code. If needed, just ask.

I bolded and underlined the one’s you gave me, so you know where I put them.

I am pretty knew at this, so my apologies for the messy code.

Otherwise, here is the ERROR I am recieving: 

_ addEventListener: listener cannot be nil: nil stack traceback: _

[C]: in function 'error’

_ ?: in function ‘getOrCreateTable’ _

_ ?: in function ‘addEventListener’ _

_ ?: in function ‘addEventListener’ _

_ Game1.lua:178: in function <Game1.lua:161> _

_ ?: in function <?:221> _

So I have no idea what’s happening :confused:

Ok then. I just saw it did not underline or bold the code…

Here is where the things are:

two of the codes you gave me are under:

 local box = {} local i = 1 local boxTimer local box2 = {} local i = 1 local box2Timer local box3 = {} local i = 1 local box3Timer local box4 = {} local i = 1 local box4Timer local delayTimer local box5 = {} local i = 1 local box5Timer 

and the other one is inside the “onCollision” Code

I don’t want you to look through the ENTIRE thing, just where I put your code, sorry for posting the entire code though.

Sorry, you can’t just throw my code into your code and expect it to work, that’s not what it was meant for.

I just copied and pasted the text example from Corona Docs.

My code snippet was just an example of how to update the score every second, it needs to be modified to fit into your code to function properly.

For starters you will need to put myText.text = score inside the updateScore function to see the score visibly increase while the game is running.

You will also need to save the score to a json file and load it in your game over module, see Rob Miracles loadsave.lua for this it does all the hard work for you.

The error you are getting on line 178? Where is line 178 in all that code? When you insert the code you have the option to turn on line numbering, but even then all the comments “–” are confusing things.

I’m very busy with my own project so I can’t go through your code and fix everything, maybe someone else has the time, good luck I hope you get it all working, sorry I can’t help you more, I have my own problems.

Alright, Thanks for your time! Although I already Do have a modified version that works with my code, the only problem with that is that when it resets after you restart the game, it just gets stuck at 1 instead of going up.

Just know that I don’t want you to do all of my work for me, I just need help, I don’t want things to just be done.

Here is the Code for the Modified version.

You don’t need to do anything, just throwing it out there!
 

score = 0 local scoreTxt = display.newText( "0", 0, 0, "Helvetica", 40 ) scoreTxt.x = display.contentCenterX scoreTxt.y = 150 local function updateScore(event) score = score + 1 -- just a timer so you can keep track.. scoreTxt.text = string.format("%d", score ) end scoreTimer = timer.performWithDelay(1000, updateScore, 0) -- AFTER SOME CODE AND ETC: local function resetScore(event) score = 0 scoreTxt.text = string.format("Score: %d", score ) end -- PUT THIS ONE IN YOUR "onCollision" TYPE OF CODE: display.remove(scoreTxt) Runtime:addEventListener("enterFrame", resetScore) Runtime:addEventListener("enterFrame", cancelTimer)

I also need help on this. It’s a pretty big issue for a lot of users. So if anyone knows how…

And also, thanks Spid3rtech! Your last post helped!

I can see why your score is getting stuck at 1

Runtime:addEventListener(“enterFrame”, resetScore)
Runtime:addEventListener(“enterFrame”, cancelTimer)

These will fire 30 to 60 times every second!

And if you don’t cancel them they will keep on running even if you are in a different module.

Sweet! Thank you!

But now, do you think you can help me get started on how to save the score to the GameOver screen? Because this is one of the things I am having trouble with… which is why I started this forum.

Open notepad on your computer and create a simple text file.

In that text file type 0

That’s a zero by the way, and save it to your game folder where your Game1.lua is and call it highScore.txt

If you want to start the highScore with a higher number then put that instead of zero when you create it.

In your main.lua put this code in.

local highScore \_path = system.pathForFile( "highScore.txt", system.ResourceDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil

It reads the highScore.txt you just put in your game folder and gives the local variable highScore the value you typed in before.

It then writes the highScore.txt to the system.Documents directory where it needs to be when you build the game for the device, don’t worry about this part.

At the top of every module in your game only where you use the score put this:

local highScore \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil

The above loads the highScore.

Keep using the score the way you are now.

In your Game1.lua when the game is over check the score to see if it’s higher than the highScore

if score \> highScore then highScore = score \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil end

The above writes the new highScore to the highScore.txt document.

“w” means write to and “r” means read from.

That’s the basics, you can easily see how it works and you can add to it all you like, have fun.

I realize that you said you don’t want links, but what QuizMaster is describing, is exactly what is included in the tutorial below. This tutorial is specifically for displaying AND saving scores, which is exactly what you want:

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

Your best bet would be to use this tutorial. If you encounter problems with the tutorial, then you can post here, asking questions about points inside the tutorial would be the best way to make sure everyone is on the same page.

@Alex@Panc, well see… your link is good but for someone like me its incredible confusing… @QuizMaster made this very easy to understand. What im thinking is im going to make a YouTube video to help out the ones making their first games and need a simple score…