Saving Score?

Hi, I know I asked this already but, it seems I still don’t know how to save in a way I can understand. Ok so what I want is to save a score only one.

This is a code sample from peach

[code]local background = display.newImage (“background.png”)

score = require (“score”)

local border = 5

local scoreInfo = score.getInfo()

score.init({
x = 40,
y = 5}
)
score.setScore(0)

local function addtoit (event)
if event.phase == “ended” then
score.setScore (score.getScore()+107)
end
end
background:addEventListener(“touch”, addtoit) [/code]

Ok In the code above demonstrates how to add score. Now I want to know how to save the score when I exit the app, or touching a button to save a score if one is greater than the other.

So basically save a score only if the score is greater.

Help is appreciated thanks

:slight_smile: [import]uid: 17058 topic_id: 21037 reply_id: 321037[/import]

Check out ice. It has that functionally built in.

http://developer.anscamobile.com/code/ice [import]uid: 84637 topic_id: 21037 reply_id: 83140[/import]

@Danny ok thanks I’ll try to understand [import]uid: 17058 topic_id: 21037 reply_id: 83143[/import]

I actually have a specific tutorial on saving the highest score :slight_smile: See here; http://techority.com/2012/01/13/easy-saving-of-highscore-in-corona-sdk-apps/

Peach :slight_smile: [import]uid: 52491 topic_id: 21037 reply_id: 83307[/import]

That tutorial is really useful, thanks for sharing. Could a similar this be used to lock levels until the previous level is completed? I’ve tried to use ice but I just don’t understand it! [import]uid: 116264 topic_id: 21037 reply_id: 83312[/import]

@peach yes I forgot about that but one thing how would I do the high score ego on game over scene?

Like when the game ends the high score displays on a game over scene

[import]uid: 17058 topic_id: 21037 reply_id: 83339[/import]

@Will - Thanks! Yes, I actually have a tutorial for that as well! Check it out here; http://techority.com/2012/01/22/easy-level-unlocking-with-ego/

@Sebitttas - You would load the score just like you do in that tutorial, except rather than simply printing it in the terminal you’d display it on the screen. (Just do new text, eg; myText, then do myText.text = loadedScore - or whatever you called the highscore when you loaded it.)

Peach :slight_smile: [import]uid: 52491 topic_id: 21037 reply_id: 83346[/import]

@peach but don’t I have to use this

highscore = loadFile ("highscore.txt") 

To load my highscore in my game over scene [import]uid: 17058 topic_id: 21037 reply_id: 83354[/import]

@peach also another thing the high score website you gave me. I get an error saying expected then near ‘&’ and I’m trying to figure it out. What is the solution to that [import]uid: 17058 topic_id: 21037 reply_id: 83358[/import]

Which highscore website, sorry?

RE loading, yes, that’s how you’d do it - is that a problem?

Peach :slight_smile: [import]uid: 52491 topic_id: 21037 reply_id: 83482[/import]

@peach I was just making sure that how you would reload a highscore in another instead of the same.

Also http://techority.com/2012/01/13/easy-saving-of-highscore-in-corona-sdk-apps/ This website for highscore saving in ego. When I copied and paste the code and tried it. I got an error saying

on main.lua:54: ‘then’ expected near ‘&’.

I was trying to do that and still nothing, even though there is already a then still nothing
[import]uid: 17058 topic_id: 21037 reply_id: 83512[/import]

Crap >. sign into some symbols. I’ve fixed it now.

You should learn to read the code though, that way if you look and know the error is on line 54 and see;

if score *bunch of symbols* highscore then

Then you would know logically something was wrong and could fix it.

(One suggestion I often make is typing out the code rather than copying it as it helps you memorize things.)

Peach :slight_smile: [import]uid: 52491 topic_id: 21037 reply_id: 83545[/import]

@peach oh I did not know it changed the symbol. I was looking over it a lot I did not know the symbol changed, I thought that meant like saving. I’am you know a beginner so I’m still learning how to figure theses stuff. But thanks for clearing it out for me :slight_smile: [import]uid: 17058 topic_id: 21037 reply_id: 83589[/import]

@peach again thanks see I’m learning yay :slight_smile: [import]uid: 17058 topic_id: 21037 reply_id: 83590[/import]

Thanks very much for you tutorial on how to lock levels, it has helped so much! I have one question about it - will it still remember the currentLevel.txt value after an update from the market? [import]uid: 116264 topic_id: 21037 reply_id: 83594[/import]

@willjhoward um I would not really know. I just sent the game. Also working on the update for the new game. I would tell you after the update of my new game to see how it goes :slight_smile: [import]uid: 17058 topic_id: 21037 reply_id: 83597[/import]

saved documents should remain intact when updating an app but if app is removed then reinstalled it will not [import]uid: 7911 topic_id: 21037 reply_id: 83599[/import]

Hey guys,

As jstrahan says it will work fine when updated - the only time it is cleared is if the app is deleted from the phone. (Glad you like it Will :))

@sebitttas - I know, you are learning :slight_smile:

Peach :slight_smile: [import]uid: 52491 topic_id: 21037 reply_id: 83707[/import]

Hey Peach

I was trying to implement your system of loading high scores, but how do i go about saving the high score as i proceed after completion of a level instead of saving the score upon exiting the app?

regards [import]uid: 23812 topic_id: 21037 reply_id: 134342[/import]

Hey I’ll try to help you, what you want to is the following which is an example:

---This is at startup during game   
highscore = loadFile ("highscore.txt")  
local points = 0 --Example I'm using this can be anything you want for keeping track of score  
  
--This would go at the bottom of your game.lua or whatever you name it  
local function checkForFile ()  
if highscore == "empty" then  
highscore = 0  
saveFile("highscore.txt", highscore)  
end  
end  
checkForFile()  
  
local function onSystemEvent ()  
if points \> tonumber(highscore) then  
saveFile("highscore.txt", points)  
end  
end  
Runtime:addEventListener( "enterFrame", onSystemEvent )  
  
--Then we save the file before the scene is changed  
--The delay scene change is to give time for the file to save  
local function dead ()  
timer.cancel(tik)  
saveFile("score.txt", points)  
local function change ()  
director:changeScene("gameover", "flip")  
end  
timer.performWithDelay(1000, change, 1)  
end  
end  
tik = timer.performWithDelay(1000, dead, 0)  
  

Below is at the gameover.lua

--Here you want the call your highscore, and score  
highscore = loadFile("highscore.txt")  
highscore = highscore  
points = loadFile("score.txt")  
points = points  
  
--This is to display your highscore  
--And regular score if you want  
local highscoreDisp = display.newText(highscore, 0, 0, "Arial Black", 20)  
highscoreDisp.x = 240  
highscoreDisp.y = 140  
localGroup:insert(highscoreDisp)  
  
local current = display.newText("", 0,0,"Arial Black", 20)  
current.x = 240  
current.y = 175  
current.text = "Score: ".. points  
localGroup:insert(current)  

This is how you would save your highscore using Ego. [import]uid: 17058 topic_id: 21037 reply_id: 134345[/import]