gameover.lua reading score/highScore from file.

Hi, I’ve always used OF for my score keeping, but the recent error I got when submitting to the app store (while using the new gameNetwork) is making me want to implement a local score/highscore that will save to file, and then read to file when I need to display it at a gameover.lua (or any other screen, such as menu, etc.)

I think I’m making some basic mistakes here, but wanted to find out if anyone could point me in the right direction for this;

menu.lua items (to call up score from score.txt file)

[lua]loadScores = function( )
local scores = {}
local str = “”
local n = 1

local path = system.pathForFile(“score.txt”, system.DocumentsDirectory)

local file = io.open ( path, “r” )
if file == nil then
return 0, 0
end

local contents = file:read( “*a” )
file:close()

for i = 1, string.len( contents ) do
local char = string.char( string.byte( contents, i ) )

if char ~= “|” then
str = str…char
else
scores[n] = tonumber( str )
n = n + 1
str = “”
end
end

return scores[1], scores[2]
end
– DISPLAY SCORE/HIGHSCORE –
score, highScore = loadScores( )
local lastScoreLabel = display.newText("Last Score " … score, 0, 0, native.systemFont, 15 )
lastScoreLabel.x = display.viewableContentWidth / 2
lastScoreLabel.y = 20
lastScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(lastScoreLabel)

local highScoreLabel = display.newText("High Score " … highScore, 0, 0, native.systemFont, 15 )
highScoreLabel.x = display.viewableContentWidth / 2
highScoreLabel.y = lastScoreLabel.y + lastScoreLabel.height
highScoreLabel:setTextColor( 0, 0, 0, 100 )
localGroup:insert(highScoreLabel)[/lua]

game.lua items (to save score/highscore to a score.txt file located on device)

[lua]saveScores = function( s, hs )–{{{
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory )

local file = io.open ( path, “w” )

local contents = tostring( s )…"|"…tostring( hs)…"|"
file:write( contents )
file:close( )
end

loadScores = function(score, highScore)
if score > highScore then
highScore = score
savetoFile(highScore)
end
end
saveScores(score, highScore)[/lua]

gameover.lua (to display score/highscore at the end of the game)

[lua]loadScores = function(score, highScore)–{{{
local scores = {}
local str = “”
local n = 1

local path = system.pathForFile( “score.txt”, system.DocumentsDirectory )

local file = io.open ( path, “r” )
if file == nil then
return 0, 0
end

local contents = file:read( “*a” )
file:close()

for i = 1, string.len( contents ) do
local char = string.char( string.byte( contents, i ) )

if char ~= “|” then
str = str…char
else
scores[n] = tonumber( str )
n = n + 1
str = “”
end
end

return scores[1], scores[2]
end --}}}
score, highScore = loadScores( )
– DISPLAY LAST AND HIGHSCORE ON SCREEN ------------------------------
local lastScoreLabel = display.newText("Final Score " … score, 0, 0, native.systemFont, 15 )
lastScoreLabel.x = display.viewableContentWidth / 2
lastScoreLabel.y = 20
lastScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(lastScoreLabel)

local highScoreLabel = display.newText(“Record Score” … highScore, 0, 0, native.systemFont, 15 )
highScoreLabel.x = display.viewableContentWidth / 2
highScoreLabel.y = lastScoreLabel.y + lastScoreLabel.height
highScoreLabel:setTextColor(255, 139, 0)
localGroup:insert(highScoreLabel)[/lua]
in both instances in menu.lua and gameover.lua if I l don’t uncomment the below
[lua]local highScoreLabel = display.newText(“Record Score” … highScore, 0, 0, native.systemFont, 15 )[/lua]
I get an error “attempt to concatenate global ‘highScore’ (a nil value)”

So I guess my question is; what am I missing from the above to work properly… or do I have it all wrong?

Thanks in advance for the help or direction! [import]uid: 10379 topic_id: 16695 reply_id: 316695[/import]

I’m sure someone knowledgeable can help debug your code for you, but I’d still like to mention that it’s worth looking at ice library. It is insanely easy to use for save/load system, and you don’t have to spend hours working out your own:

http://developer.anscamobile.com/code/ice

Naomi [import]uid: 67217 topic_id: 16695 reply_id: 62470[/import]

Thanks! I’m gonna look into it right now :slight_smile: hopefully I’ll get it faster than debug of the above [import]uid: 10379 topic_id: 16695 reply_id: 62481[/import]

Hey I think some of it is because from my quick read over you don’t ever really call or name highScores on its own. Also it would appear that your function loadscores and savescores, while both global, in each module are somewhat different which could be throwing it off.

I would recommend making a separate module, call it savedata or something and only write your loadScores and saveScores functions there and then call on them when needed instead of retyping them each time they are needed. [import]uid: 23649 topic_id: 16695 reply_id: 62516[/import]

later I’m posting on my site some free code that will save and load any game info and handle highscore list
j-strahan.com [import]uid: 7911 topic_id: 16695 reply_id: 62521[/import]

Thank you all for the input, and my apologies for not looking in earlier, I was stuck in traffic. I’m going to play around with Ice as well.

@ jerryapplebaum12 - sadly I’m realizing that now :expressionless:

@jstrahan - thanks anything will be appreciated :slight_smile: [import]uid: 10379 topic_id: 16695 reply_id: 62529[/import]

welcome
should be uploaded to my site in an hour or so [import]uid: 7911 topic_id: 16695 reply_id: 62534[/import]

jstrahan - Just downloaded the js.lua … sorry I don’t use twitter much, but in the next few days I’ll have my twitter follows figured out (yea, i live under a rock!) and I’ll re for sure!!! [import]uid: 10379 topic_id: 16695 reply_id: 62570[/import]

no problems i just crawled out from under the twitter rock myself
by the weekend i should have some examples on the site using js.lua also check out graffiti for Corona™SDK on the site [import]uid: 7911 topic_id: 16695 reply_id: 62574[/import]

I watched all of the videos you have to demo Graffiti PS I like it, I can already think of number of ways to use it with my projects. You have both free and full versions! I’ll check this out as soon as I’m done with my highscore issue, but with JS I’m already making quick progress! [import]uid: 10379 topic_id: 16695 reply_id: 62576[/import]

good to hear
i have a few other videos on my youtube channel just search on youtube for jstrahan73
glad js is helping if you have any problems let me know i wont be up much longer its 11:30pm here but ill answer as soon as i can
one thing i forgot to put in the comments of js was when you load the file back in you need to use tonumber to convert numbers to numbers. loading variables always load them as strings [import]uid: 7911 topic_id: 16695 reply_id: 62578[/import]

Thanks! Understood. I’m on the west coast so it’s 9:40pm here. I’ll be up for just a bit longer myself. I’ll search youtube as well :slight_smile: [import]uid: 10379 topic_id: 16695 reply_id: 62586[/import]

Graham is lucky, here Ice is actually Meth-D and illegal and banned :wink:

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16695 reply_id: 62666[/import]

haha [import]uid: 7911 topic_id: 16695 reply_id: 62668[/import]

Hey, Jayant, about Ice, what do you mean? Am I missing something important here? Or are you just being funny, and there’s nothing I should truly be concerned about?

Honestly, I was ecstatically happy about it – compared to the most crude form of save/load system I struggled to create using JSON (and managing the linear table I created, sheesh, I didn’t like it, although I learned a lot in the process), I find Ice extremely flexible, amazing… and super duper easy to use.

Every game developer would need some form of save/load system pretty early in the development process, and if one is a newbie like me, dealing with save/load system early on, when he/she is just starting to learn to code… I’d think Ice is a great solution (just like Director class is a great solution for modularizing the project).

Naomi [import]uid: 67217 topic_id: 16695 reply_id: 62695[/import]

it was a joke ICE is a term for drugs [import]uid: 7911 topic_id: 16695 reply_id: 62697[/import]

Hahahaha, thanks @jstrahan. I’m from Japan (and grew up on that island of a country), and don’t know much about drugs or drugs related terminology – no wonder I didn’t clue in properly.

Naomi [import]uid: 67217 topic_id: 16695 reply_id: 62703[/import]

dont know much bout drugs myself could never see wasting my money on any of that when i could use it to get the next best apple product. haha
really my brother owns a pawn shop and i work there when im off from my regular job and we get all kind of people in there
which by the way is where i just got my macbook pro last years model for $400.00 sweet [import]uid: 7911 topic_id: 16695 reply_id: 62708[/import]

my daughter loves reading manga books and wants to go to japan one day [import]uid: 7911 topic_id: 16695 reply_id: 62709[/import]