Share Your Code

Very simple scoring method

Posted by mdcapps, Posted on July 14, 2011

This is a very simple way of updating a user's score and then saving it to be called again when the app is restarted, basically saving the score indefinitely.

There is only really one file to this, and thanks to peach pellen for providing some of the code snippets in her wonderful tutorials -- really guys check it out www.techority.com

anyway, here is the main.lua file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--[[
SCORING SAMPLE CODE BY MAX PRICE
WITH THANKS TO PEACH PELLEN FOR THE CODE SNIPPETS
--]]
 
display.setStatusBar( display.HiddenStatusBar )
 
 
local function save( event )    -- function to save score       
                local path = system.pathForFile( "score.txt", system.DocumentsDirectory )  -- where the file will be saved              
                local file = io.open( path, "w+b" ) -- opening the file
                
                                file:write(score .."")          -- writing the variable 'score' in the score.txt file 
                io.close( file ) -- closing the file
                                -- Saves our data
 
end
 
 
local function resumeStart()
                        local path = system.pathForFile( "score.txt", system.DocumentsDirectory )   -- where the file will be saved       
                                local file = io.open( path, "r" ) -- opens the file under the variable file 
 
                                if file then -- if there is a file then
                                        local contents = file:read( "*a" ) -- read the contents of the file, and read the whole string(*a)
                                        
                                        local prevState = explode(", ", contents) -- put the contents of score.txt into a table
                                                print('file')
                        score = prevState[1] -- read the table at position 1 and assign the variable score
 
                                        io.close( file ) -- close file 
 
                                else -- if there is no file
                                                score=0 -- the score then starts at 0
                                end
                        end
 
--ANSCA sample code--
function explode(div,str)
  if (div=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr
end
--ANSCA sample code^--
 
 
resumeStart() -- call the starting function 
 
 
 
local scoretext = display.newText('Score:',200,20,native.systemFont,20)
local t = display.newText('',280,20,native.systemFont,20) -- score text
t.text=score -- update t to the score
 
 
local function scoretapped() -- function called when user presses the add button
        score=score+50 -- increases the score by 50
        save() -- calls save, so when the user reloads the application the score is still there
        t.text=score -- updates t to reflect new score
end
 
local scoretap = display.newRect(0,400,320,50) -- scoring button
scoretap:setFillColor(0,0,255)
scoretap:addEventListener('tap', scoretapped) -- adding an event listener.

It's all commented, so hopefully it's easy to understand

thanks a lot,

Max


Replies

kingmike40
User offline. Last seen 2 years 30 weeks ago. Offline
Joined: 17 Jan 2011

I have used this code for my game and it works but it only works upto the number 9. No double digits show up. It just stops at 9.
I changed the code for my game and there is several lines that I don't understand what they do.

Can anybody tell me why it stops at 9? I would appreciate any help.

local function save( event ) -- function to save score
local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved
local file = io.open( path, "w+b" ) -- opening the file

file:write(score .."") -- writing the variable 'score' in the score.txt file
io.close( file ) -- closing the file
-- Saves our data

end

local function resumeStart()
local path = system.pathForFile( "score.txt", system.DocumentsDirectory ) -- where the file will be saved
local file = io.open( path, "r" ) -- opens the file under the variable file

if file then -- if there is a file then
local contents = file:read( "*a" ) -- read the contents of the file, and read the whole string(*a)

local prevState = explode(", ", contents) -- put the contents of score.txt into a table
print('file')
score = prevState[1] -- read the table at position 1 and assign the variable score

io.close( file ) -- close file

else -- if there is no file
score=0 -- the score then starts at 0
end
end

--ANSCA sample code--
function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-3)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
--ANSCA sample code^--

-- call the starting function

resumeStart()

local scoretext = display.newText('High Score:',(_W*.3),(_H-30),native.systemFont,20)
local t = display.newText(score,(_W*.5),(_H-30),native.systemFont,20) -- score text
t.text=score -- update t to the score

local function scoretapped() -- function called when user presses the add button

if (hole_count.text > score) then
score = hole_count.text
t.text = score -- updates t to reflect new score
save( "score.txt", score )

end

end

mdcapps
User offline. Last seen 5 weeks 6 days ago. Offline
Joined: 21 Jan 2011

Quick thing -- remember to put your code in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 brackets, makes it much easier to read! 
 
Moving on, there are a few things that I have spotted that may fix your problem
 
<lua>
local scoretext = display.newText('High Score:',(_W*.3),(_H-30),native.systemFont,20)
local t = display.newText(score,(_W*.5),(_H-30),native.systemFont,20) -- score text
t.text=score -- update t to the score -- dont think you need this line
 
local function scoretapped() -- function called when user presses the add button
 
if (hole_count.text > score) then 
score = hole_count.text --why is this .txt? why not just make hole_count a variable? 
t.text = score -- updates t to reflect new score
save( "score.txt", score ) -- you dont these in brackets
 
end
 
end

Dont know if that will help, but it should do!

Max