Display Virtual High Score

I want to know How to display a virtual high score. I’ve been trying to figure how to do that with the Ghost-vs-Monster example but had failure. I want to know the simplest way of displaying a virtual while your playing a game. Here is what adding score looks like.

local scoreText  
local score = 0  
   
scoreText = display.newText(score, 0, 0, "HelveticaNeue", 35)  
scoreText:setTextColor(255, 255, 255)  
scoreText.x = 160  
scoreText.y = 30  
  
--This would the score adding up randomly  
local function onCollision(event)  
  
if event.phase == "began" and event.other.name == "ball2" then  
display.remove(event.other)  
event.other = nil  
score = score + math.random( 100 )  
  
scoreText.text = score  
  
end  
end  
  
Red.preCollision = onLocalPreCollision  
Red:addEventListener("collision", onCollision)  

Now from this I would like to know how to display a virtual high score. [import]uid: 17058 topic_id: 19321 reply_id: 319321[/import]

logic is like that:
you score points, they add up to variable “score”
on game exit\game over you call function like that:
if score > highscore then
highscore = score
end

and highscore is your variable that you write to file at game over and load at start of the level\game

you can use one of the many saving modules for that purpose [import]uid: 16142 topic_id: 19321 reply_id: 74544[/import]

@darkconsoles I’m little confuse on how to display the high score on the game over scene [import]uid: 17058 topic_id: 19321 reply_id: 74545[/import]

just like any other text
use .text variable like that:

local function game_over()
if score> highscore then
highscore = score
end

local highscoreText = display.newText("HighScore: ",0,0,nil,30)

highscoreText.text = "HighScore: "… highscore – or just “score” variable
end

[import]uid: 16142 topic_id: 19321 reply_id: 74546[/import]

@darkconsoles I keep on getting an error every time when the scene changes to gameover. The error says

attempt to compare nil with number

I put the function on a timer. Every time the timer is 0 it changes scene and I end up getting an error. I know for sure I’m doing something wrong. [import]uid: 17058 topic_id: 19321 reply_id: 74548[/import]

ok, you want to download ice library, you can find it here:
https://github.com/GrahamRanson/Ice

i played with it for the first time and managed to get working high score system, its not pretty, but whatever if it works))
[lua]require( “ice” )

local scores = ice:loadBox( “scores” )

local high = scores:retrieve(“best”)
print(high)

local gameOver = false

local physics = require(“physics”)
physics.start()
physics.setGravity(0,5)
local ballGroup = display.newGroup()
local score = 0

local score_txt =display.newText("Score: ",0,0,nil,30)
score_txt.x = 100; score_txt.y = 50
score_txt:setTextColor(255,0,0)
score_txt.text = "Score: "… score

local function spawn_ball()
if gameOver == false then
local ball = display.newCircle(ballGroup,0,0,30)
ball.x = math.random(50,300)
ball.y = -50
physics.addBody(ball, {radius = 30})
ball.name = “ball”
end
end

timer.performWithDelay(500, spawn_ball, 0)
local basket = display.newRect(0,0,50,70)
basket.x = display.contentWidth/2
basket.y = display.contentHeight - 80
physics.addBody(basket, “static”)

local function drag(event)
if event.phase == “began” then
event.target.isFocus = true
elseif event.phase == “moved” and event.target.isFocus then
event.target.x = event.x
event.target.y = event.y
end
return true
end

basket:addEventListener(“touch”, drag)
local function show_highScore()
gameOver = not gameOver

scores:storeIfHigher( “best”, score )
scores:save()
local highText = display.newText("", 0,0,nil,30)
highText.x = display.contentWidth/2
highText.y = display.contentHeight/2
if score > high then
highText.text = “HighScore: “… scores:retrieve(“best”)
else
highText.text = “NO NEW HIGH SCORE!”
end
end
local function show_text()
local text = display.newText(”+1”, 0,0, nil, 30)

score = score + 1

if score > scores:retrieve(“best”) then
print(“new high score”)
end

score_txt.text = "Score: "… score
text.alpha = 1
text.x = basket.x
text.y = basket.y - 100
transition.to(text, {time=1000,y = text.y - 50, alpha = 1, onComplete = function() display.remove(text) end})
end

local function onCollision(event)
if event.phase == “began” and event.other.name == “ball” then
display.remove(event.other)
show_text()
end
end

basket:addEventListener(“collision”, onCollision)

timer.performWithDelay(20000, show_highScore, 1)
[lua] [import]uid: 16142 topic_id: 19321 reply_id: 74549[/import]

Here is my drawScores() function from OmniBlaster.

This was my first Corona project, so apologies on the quality of the code, but it works. For my project, I tracked 10 high scores for the player, the name they entered and their rank. Scores are kept in 3 tables (though now I would probably do it in one table) and are saved to separate score files using json.encode/json decode el I used a library out of the common code called “score.lua” to deal with the saving and loading of the scores. Today I would just use json.encode/decode with my own tables to track high scores.

This code only draws the 3 tables out.

local function drawScores()  
 local rank  
 local rankText  
 local ranks={}  
 local names = {}  
 local scores = {}  
 local iPad = 0  
 if system.getInfo("model") == "iPad" then  
 iPad = 40  
 end  
 local msgHeader1 = display.newText("Rank", 50, 170, "Courier-Bold", 16)  
 msgHeader1.x = 50 + iPad  
 local msgHeader2 = display.newText("Player Name", 100, 170, "Courier-Bold", 16)  
 msgHeader2.x = 135 + iPad  
 local msgHeader3 = display.newText(string.format("%8s","Score"), 240, 170, "Courier-Bold", 16)  
 msgHeader3.x = 240 + iPad  
 scr.highScores:insert(msgHeader1)  
 scr.highScores:insert(msgHeader2)  
 scr.highScores:insert(msgHeader3)  
 for i=1, scList:Count() do  
 rank = scList:GetRank(i)  
 if rank == 0 then  
 rankText = "---"  
 else  
 if rank \> award.maxRank then  
 rank = award.maxRank  
 end  
 rankText = award.pilotRanks[rank]  
 end  
 ranks[i] = display.newText(rankText, 50 + iPad, 170 + i \* 18, "Courier", 16)  
 names[i] = display.newText(scList:GetName(i), 110 + iPad, 170 + i \* 18, "Courier", 16)  
 scores[i] = display.newText(string.format("%8d", scList:GetPoints(i)), 240 + iPad, 170 + i \* 18, "Courier", 16)  
 if i == scoreIndex then  
 ranks[i]:setTextColor(255, 255, 255)  
 names[i]:setTextColor(255, 255, 255)  
 scores[i]:setTextColor(255, 255, 255)  
 else  
 ranks[i]:setTextColor(255,192,0)  
 names[i]:setTextColor(255,192,0)  
 scores[i]:setTextColor(255,192,0)  
 end  
 scr.highScores:insert(ranks[i])  
 scr.highScores:insert(names[i])  
 scr.highScores:insert(scores[i])  
 end  
end  

Maybe that will help. [import]uid: 19626 topic_id: 19321 reply_id: 74565[/import]

@darkconsole thanks for your help I was able to get it working on my game and also I like the sample you did. Very sorry for not appreciating that’s my fault. I appreciate your help a lot very thank you. :slight_smile: [import]uid: 17058 topic_id: 19321 reply_id: 74595[/import]