save/load code only counts to 9

I used some code from some place on corona’s website but can’t find it again. I got it to work with my game but the high score only goes to 9 while the players score (hole_count) goes to double and triple digits. Would somebody please tell me what is going wrong. I don’t understand every line in the code
but I did get it to work but only up to 9. I am guessing that it has something to do with single and double digits but the research I have done hasn’t uncovered anything obvious.
Thanks for any help I do appreciate it.

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

–score = score + 1 – increases the score by 50
– save() – calls save, so when the user reloads the application the score is still there
–hole_count.text = t.text

if (hole_count.text > score) then
score = hole_count.text
– score = hole_count.text
t.text = score – updates t to reflect new score
save( “score.txt”, score )

end

end [import]uid: 17539 topic_id: 13756 reply_id: 313756[/import]

Hey,

You need to post your code in < lua > tags, otherwise no one will read it. (It gives people headaches ;))

Put < lua > before your code and < / lua > after it please.

Peach [import]uid: 52491 topic_id: 13756 reply_id: 50582[/import]

I’m sorry I didn’t know about the tags.
thanks
michael [import]uid: 17539 topic_id: 13756 reply_id: 50594[/import]

Here is the code in lua tags. I don’t know what is meant by brackets.
[lua]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

–score = score + 1 – increases the score by 50
– save() – calls save, so when the user reloads the application the score is still there
–hole_count.text = t.text

if (hole_count.text > score) then
score = hole_count.text
– score = hole_count.text
t.text = score – updates t to reflect new score
save( “score.txt”, score )

end

end[/lua] [import]uid: 17539 topic_id: 13756 reply_id: 50596[/import]

What brackets, sorry?

And no worries, but please read the rules - they’re short and it helps keep the forum neat :wink: http://developer.anscamobile.com/forum/2011/05/05/forum-rules-and-guidelines

It’s hard to follow some of the above code; can you try manually stating score = 99999 then saving it and restarting, just to see what is returned?

I can’t see why it wouldn’t save over 9 - is the terminal printing any errors?

Let me know - if I can’t help you find a solution based on your answers I’ve got some slightly different (but similar) code in a tutorial I can link you to.

Peach :slight_smile: [import]uid: 52491 topic_id: 13756 reply_id: 50704[/import]

Hi Peach and thanks for taking the time to help.

I changed the file to read 99999 and it displayed on the simulator in the position of high score, but
did not change, but my game scores will not go past 100(perfect score) which is almost impossible to earn. I changed the score to 99 and the same thing happen, no change. I save 1.9 in the systemDocumentDirectory, score.txt. and it would go up to 9.9 but the players score was 18.7. I changed the amounts the game scores to 1.1 just to see what would happen and that is where the
decimal came from.

I also changed the scoring to 100 points instead of the 1 point and the high score would not change above 900 although the players score was 1100. So I changed the score in the systemDocumentDirectory to 1100 and saved. The 1100 will show up in the high score position but will change to 2 when the players score is 2. If the players score is at 1 the high score remains 1100. It is almost like I have a decimal out of place.

I know this is confusing since you don’t know all of the code or how the game is played, but I do appreciate the help.
thanks
Michael [import]uid: 17539 topic_id: 13756 reply_id: 50739[/import]

I forgot to tell you that I am using an old version of corona 268. I downloaded it in January 2011. I don’t know if this makes a difference. [import]uid: 17539 topic_id: 13756 reply_id: 50740[/import]

I FIXED IT!!!

I gave up for a while and went to the park to walk around. As I was driving up to the park. I had a thought, Yes I do think. Maybe what I needed was to put tonumber() on the hole_count.text.
I tried it and that didn’t make any difference , but I also didn’t get any errors. I then put tonumber()
on the score and it worked. It will count past 9 and save the score and the score updates with a new high score.
I am sharing this with the forum because I thought that somebody might benefit from it later and
I am hoping somebody could tell me why it works.

Line 59 was in post earlier was.

[lua]if (hole_count.text > score) then – old line only count to 9
score = hole_count.text
– score = hole_count.text
t.text = score – updates t to reflect new score
save( “score.txt”, score )[/lua]

I changed it to

[lua]if (tonumber(score) < tonumber(hole_count.text )) then – this made the difference, but I don’t know why
score = hole_count.text
t.text = score – updates t to reflect new score
save( “score.txt”, score )[/lua]
[import]uid: 17539 topic_id: 13756 reply_id: 50770[/import]

Argh! Why does everything exciting happen while I’m asleep?!

I feel VERY happy for you; figuring stuff out that has been “bugging” you is always great - well done :smiley: [import]uid: 52491 topic_id: 13756 reply_id: 50871[/import]