Reputation: 87
I'm trying to design a game in lua (This is my first game since I'm new to this language) Okay.. So I have run a score counter that adds up +1 on screen tap. What I want to do is I want to save the high score permanently.
I want to display the high score ('30' in this case) separately on the screen. And this is where I'm stuck. I tried tweaking with the I/O library, but this made things go more complicated. Can anyone help me with this please?
This is what I have tried:
local f1 = io.open ("scoredata.txt", "a")
function gameOver()
disp_tempscore()
if score>highScore -- Here initial value of highScore was 0
then
highScore=score
io.write (highScore)
end
score=0 -- This turns the score 0. (There is a separate score counter that counts the score and stores in 'score')
mainScreen()
local f2 = io.open ("scoredata.txt", "r")
if f2~= nil
then
save_high = io.read(highScore)
end
text_display2= display.newText("BEST : " .. highScore, 0, 0, "Helvetica", 90)
text_display2.x = centerX
text_display2.y = centerY + 80
text_display2.alpha=1
Now here the score for 'BEST :' appears the highest score, BUT only for multiple runs at same time. What I mean to say is when I start the game in corona simulator and play the game for 5 times (suppose), then highest score shows correct data. But, when I exit simulator and restart it, the highest score vanishes and displays 0. How can I store the data (score in this case) permanently?
EDIT: (This is what I had tried last (one of the hits I tried))
local function disapp (event) -- An event listener that responds to "tap"
local obj = event.target
display.remove (obj)
audio.play(sound)
transition.cancel (event.target.trans)
score=score+1
curr_scoreDisp = score
scoreText.text=score
local f1 = io.open (path, "a") --Writing score to file
if score>highScore -- Initial value of highScore is 0
then
highScore=score
f1:write(highScore)
end
io.close(f1)
Then :
local function disp_Permscore()
local f1 = io.open (path, "r")
f1:read(highScore)
text_display2= display.newText("BEST : " .. highScore, 0, 0, "Helvetica", 90)
text_display2.x = centerX
text_display2.y = centerY + 80
text_display2.alpha=1
io.close(f1)
end
This is another function that reads score from file and then displays it. Now? Does this help in rectifying the problem in any way?
Upvotes: 1
Views: 843
Reputation: 329
You're using the file:read()
function incorrectly. It takes an 'option' argument and returns what it read. Thus,
f1:read(highScore)
should be
highScore = f1:read("*n")
where the "*n"
argument specifies you want to read a number.
Edit:
To prevent trouble with files not being written/read properly, try the following (untested) code:
if score > highScore then
highScore = score
local f1 = io.open(path, "w") -- opening in write mode erases content
f1:write(highScore) -- write the new highscore
f1:close() -- close the file
end
for writing, and
local f1 = io.open(path, "r") -- open the file in read mode
highScore = f1:read("*n") -- read the saved highscore
if highScore == nil then -- if the highscore could not be read
--error code here (e.g. set highscore to 0, notify the user, etc.)
end
f1:close() -- close the file
for reading.
Upvotes: 0
Reputation: 29571
I think the issue is that you never write to the score file:
io.write (highScore)
should be
f1:write(highScore)
and you should close the file as soon as you no longer need it (will prevent loss of data due to a crash, e.g.). Also don't forget to use system.pathForFile( filename, system.DocumentsDirectory)
as described in Corona docs, so the file is put in the correct location.
Upvotes: 0