Tracking Player Score

Hi everyone,

I am using my main.lua pretty much exclusively like a controller class that calls other external lua files; each which have their own fields and methods for creating unique objects.

For example, I have my main calling a Block.lua file that creates new blocks that can be used in the game and has logic for tracking and destroying them as well. I also have a data field that indicates that when destroyed it should increase the score by a certain amount.

I have quite a few other objects that are also used in the game, each with their own logic as to how they affect the score.

Hence my problem…

What is the best practice for floating this back to my main.lua so I can keep track of the score and display it to the user?

Any ideas/advice would be greatly appreciated.

Thanks!

PixelAmp

[import]uid: 10060 topic_id: 5836 reply_id: 305836[/import]

Thanks Hunnenkoenig! That looks really close to what I was looking for so I will give it a shot!

Thanks again!

[import]uid: 10060 topic_id: 5836 reply_id: 20362[/import]

I made a score system with the help of a friend of mine and it seems to work fine.

I don’t know, if this is, what you are looking for, but here it is (we are still working on the saving of last reached level and actual level):

into main.lua:

[code]-- Load Scoresystem
require(“score”)
myScore = Score.init(180,260) – the coordinates where to show the numbers
require “sqlite3”
db = 0
Level = 0
LastReachedLevel = 0

function saveState()
local dbcommand = [[UPDATE stat SET currentscore=]]…myScore.score
if LastReachedLevel < Level then
dbcommand = dbcommand … [[AND lastlevel=]]…LastReachedLevel…[[;]]
end
db:exec(dbcommand)
end[/code]

Score.lua
(based on peachpellen’s code with custom images for the numbers)

[code]local numbers = {
[string.byte(“0”)] = “images/0.png”,
[string.byte(“1”)] = “images/1.png”,
[string.byte(“2”)] = “images/2.png”,
[string.byte(“3”)] = “images/3.png”,
[string.byte(“4”)] = “images/4.png”,
[string.byte(“5”)] = “images/5.png”,
[string.byte(“6”)] = “images/6.png”,
[string.byte(“7”)] = “images/7.png”,
[string.byte(“8”)] = “images/8.png”,
[string.byte(“9”)] = “images/9.png”,
[string.byte(" ")] = “”
}

Score = {}
Score.__index = Score

function Score.init(dispX, dispY)
local o = {}
o.score = 0
o.displayData = {
posX = dispX,
posY = dispY
}
o.theBackgroundBorder = 0
o.displayIndex = -1
o.displayGroup = 0
setmetatable(o, Score)
return o
end

function Score:addPoint(point)
self.score = self.score + point
end

function Score:update()
self.displayGroup:remove(2)
local numbersGroup = display.newGroup()
self.displayGroup:insert(numbersGroup)
– go through the score, right to left
local scoreStr = tostring(self.score)
local scoreLen = string.len(scoreStr)
local i = scoreLen
– starting location is on the right. notice the digits will be centered on the background
local x = self.displayGroup.contentWidth - self.theBackgroundBorder
local y = self.displayGroup.contentHeight / 2
while i > 0 do
– fetch the digit
local c = string.byte( scoreStr, i )
local digitPath = numbers[c]
local characterImage = display.newImage(digitPath)
– put it in the score group
numbersGroup:insert(characterImage)
– place the digit
characterImage.x = x - characterImage.width / 2
characterImage.y = y
x = x - characterImage.width
i = i - 1
end
end

function Score:display(group)
local displayGroup = display.newGroup()
displayGroup.x = self.displayData.posX
displayGroup.y = self.displayData.posY
local theBackground = display.newImage( “images/scorebg.png” )
self.theBackgroundBorder = 10
displayGroup:insert( theBackground )
local numbersGroup = display.newGroup()
displayGroup:insert( numbersGroup )
self.displayGroup = displayGroup
self:update()
group:insert(self.displayGroup)
self.displayIndex = group.numChildren - 1
end

function Score:remove(group)
group:remove(self.displayIndex)
end

function Score:loadTop()
local tmp = db:nrows(“SELECT topscore FROM stat”)

return tmp[0]
end

function Score:setTop(value)
local ctop = db:nrows(“SELECT topscore FROM stat”)
ctop = ctop[0]
if ctop < value then
local dbcommand = [[UPDATE stat SET topscore=]]…value…[[;]]
db:exec(dbcommand)
end
end[/code]

Paste into your levels’ lua files:

-- Show the score myScore:display(localGroup) -- Things to use in your code, where you want to update score myScore:addPoint(100) -- can be anything, like levelNr?100 myScore:update()
[import]uid: 6587 topic_id: 5836 reply_id: 20351[/import]

I removed the code: myScore:remove(localGroup)
from the third window, because it caused, that some objects were removed shortly before the level change.

It is not necessary, but I thought, it is. [import]uid: 6587 topic_id: 5836 reply_id: 20678[/import]