Help with local high scores

I want to have local high scores on my game. I have tried to use some examples in code exchange, but i havent understanded them.
Basically i need to save numbers and load them again. I have thinked something like this: I have 10 different variable saved, which is like top 10 list. When i play my game and get score, if it is higher than example variable 5, but smaller than variable 4, then save it to variable 5. And if i close my game those variables doesnt destroy.
And then i have high score list, where those 10 variables are loaded.

Please help me. [import]uid: 18445 topic_id: 7943 reply_id: 307943[/import]

Camoon, can somebody help me? [import]uid: 18445 topic_id: 7943 reply_id: 28383[/import]

I have just made this, maybe it’s not the best way but it works:

local ui = require ("ui")  
  
--Create default scores  
  
local scores = {}  
  
for i = 1, 10, 1 do  
scores[i] = math.random(1,10)  
end  
local place = {}  
  
for i = 1, 10, 1 do  
place[i] = ui.newLabel {  
text = i..".",  
 textColor = {255, 255, 255, 255},  
 size = 50,  
 align = "left",  
 bounds = {0, 10, display.contentWidth/5, 40},  
};  
  
place[i].x = display.contentWidth/8  
if i == 1 then  
place[i].y = display.contentHeight/7  
  
else  
  
place[i].y = place[i-1].y + 80  
  
end  
  
end  
  
--Create all lists in an array  
  
local list = {}  
  
--Place lists and set their scores  
for i = 1, 10, 1 do  
list[i] = display.newText(scores[i], 0, 0, native.systemFontBold, 50)  
  
list[i].x = display.contentWidth/2  
if i == 1 then  
list[i].y = display.contentHeight/7+40  
  
else  
  
list[i].y = list[i-1].y + 80  
  
end  
  
end  
  
--Sort the scores  
local function sort()  
for i = 1, 5, 1 do  
for i = 10, 1, -1 do  
print(scores[i-1])  
if i\>1 then  
if scores[i] \> scores[i-1] then  
local tempScore = scores[i]  
scores[i] = scores[i-1]  
scores[i-1] = tempScore  
end  
end  
  
end  
--Update the list  
  
print (scores[i])  
for i = 1, 10, 1 do  
  
list[i].text= scores[i]  
end  
end  
  
end  
  
local btn = display.newImage("image.png")  
btn:addEventListener("touch", sort)  

[import]uid: 24111 topic_id: 7943 reply_id: 28474[/import]

Thanks for help, but that isnt exactly, what i am looking for. Your code mades top 10 list. I need, that i can save scores, so when i close my game and then relaunch, i can load scores again. So they doesnt destroy. I am not professional, so could someone help me littlebit. [import]uid: 18445 topic_id: 7943 reply_id: 28475[/import]

Well I dont know how to do it since I can’t test my game on a real device yet.
But please tell me if you find a solution. [import]uid: 24111 topic_id: 7943 reply_id: 28478[/import]

You can also try this, I will try it later today:

http://developer.anscamobile.com/code/simple-local-score-framework [import]uid: 24111 topic_id: 7943 reply_id: 28481[/import]

Thanks. I tried it and it works almost perfectly. I have just one problem: printing scores. print(#scores) works in command shell, but when i am trying to show scores in my game screen, it doesnt work. It only show number of scores i have saved. Here is my code for showing scores:

[code]json = require(“json”)
lime = require(“lime”)

local scores = lime.localScores({
level = 1
})

pisteet2 = display.newText( " " … #scores, display.contentWidth*0.25, display.contentHeight/2, nil, 40 )
pisteet2:setTextColor( 255, 255, 255)
localGroup:insert(pisteet2) [import]uid: 18445 topic_id: 7943 reply_id: 28487[/import]

If it show the numbers of your score then what is the problem? [import]uid: 24111 topic_id: 7943 reply_id: 28488[/import]

in the help text you can find this:

[lua] – iterate over scores
for i, s in ipairs(scores) do
local addedAt = lime.formatTimestamp(s.timestamp)
print(s.score, addedAt)
end[/lua]

URL: https://github.com/Abica/lime [import]uid: 42078 topic_id: 7943 reply_id: 28489[/import]

It shows numbers in command shell, not in the game. I want to that there is top 10 high score list, where numbers are shown.

I quite new corona developer, so what does this mean:

for i, s in ipairs(scores) do local addedAt = lime.formatTimestamp(s.timestamp) print(s.score, addedAt) end

It didnt work with that piece of code. I just get error: lime.lua:124: attempt to index upvalue ‘scoresFile’ a nil value [import]uid: 18445 topic_id: 7943 reply_id: 28493[/import]

try this:

[code]

–IMPORT SCORE FUNCTIONS
json = require(“json”)
lime = require(“lime”)
lime.add({
score = 500,
level = 52,
difficulty = “hard”
})

local scores = lime.localScores({
level = 52,
difficulty = “hard”
})
list = {}
for i, s in ipairs(scores) do
local addedAt = lime.formatTimestamp(s.timestamp)
print(s.score, addedAt)

list[i] = display.newText(s.score…" " … addedAt, 0, 0, native.systemFontBold, 50)
if i > 1 then
list[i].y = list[i-1].y + 50
end
end
– clear all scores for a level,
lime.clear({
level = 52,
difficulty = “hard”
})

– write scores to a file
lime.save()

[/code] [import]uid: 24111 topic_id: 7943 reply_id: 28495[/import]

Very big thanks to you! It works perfectly, but i have still one small problem: i use director in my game and now score texts doesnt disappear. Normaly text has “name”, but now there are list[i]
And localGroup:insert(list[i]) doesnt work. So how i have to do it, that it works with director? [import]uid: 18445 topic_id: 7943 reply_id: 28497[/import]

try localGroup:insert(list) [import]uid: 24111 topic_id: 7943 reply_id: 28498[/import]

Not working. Can it done director friendly someway? Damn this is hard. [import]uid: 18445 topic_id: 7943 reply_id: 28502[/import]

Never mind. I figured that out myself. Finally its working! :slight_smile: [import]uid: 18445 topic_id: 7943 reply_id: 28510[/import]

What did you do? [import]uid: 24111 topic_id: 7943 reply_id: 28514[/import]

Wrong:

list[i] = display.newText(s.score.." " .. addedAt,display.contentWidth/7, display.contentHeight/6,nil, 40)  
 if i \> 1 then  
 list[i].y = list[i-1].y + 50  
 end  
localGroup:insert(list[i])  

Right:

[code]
list[i] = display.newText(s.score…" " … addedAt,display.contentWidth/7, display.contentHeight/6,nil, 40)
localGroup:insert(list[i])
if i > 1 then
list[i].y = list[i-1].y + 50
end [import]uid: 18445 topic_id: 7943 reply_id: 28526[/import]