## Saving problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
IceQB
Citizen
Posts: 67
Joined: Sat Nov 03, 2012 1:11 pm

### Saving problem

Hi
Can you look at my saving code and say where is error, because my files are not saving numbers in file?

Code: Select all

function love.update(dt)
var = {}
var.score = seconds
var.highscore = 0

file = love.filesystem.newFile('scores.lua')
file:write("var.highscore = ", var.highscore)
file:close()
dofile("scores.lua")

if var.score > var.highscore then
var.highscore = seconds
end

When i restarting game highscore = 0
and second question, i must make file scores.lua, and prepare it?
(Do I need to write in scores.lua

Code: Select all

var.highscore = 
?)

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: Saving problem

What your code does, is writing the var.highscore and then reading it again. That is why you always get highscore = 0.

Here are two of possible improvements:
• Do not put this code into love.update. Instead only load the old high score on start up (love.load) and only save it, when a game is over
And for your other question: You have basically two options for saving and loading data. Either save data in Lua-language. Then loading is very easy, because you can just run the code. Writing is a bit more difficult. And also some people say that this method is unsafe. Or you save the data in an own format, that you make up. Then you have to write an additional loading function. In this case you could also very simply just save the plain highscore value without any extra code. You'd then use love.filesystem.read instead of load.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Saving problem

micha wrote:And also some people say that this method is unsafe.
It's not.

Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

### Re: Saving problem

IceQB wrote:Hi
Can you look at my saving code and say where is error, because my files are not saving numbers in file?

Code: Select all

function love.update(dt)
var = {}
var.score = seconds
var.highscore = 0

file = love.filesystem.newFile('scores.lua')
file:write("var.highscore = ", var.highscore)
file:close()
dofile("scores.lua")

if var.score > var.highscore then
var.highscore = seconds
end

There is a lot of fundamental misunderstanding there. First you certainly do not want to write a new highscore to disk every time the current score becomes higher than the current high score. No game I know does that, it is an insane disk wrecking and performance degradation exercise. You only update the highscore at fixed points e.g. after a game is completed or maybe when the user opens the high score list.

Here is a simple example for you. It saves the highscore whenever the game is closed (i.e. close the game window) and restores the saved highscore when the game is restarted.

Code: Select all

score     = 0
highscore = 0

if savedData then savedData() end

end

-- Just to show that it is working
function love.draw()

love.graphics.print("Score: " .. score, 10, 10)
love.graphics.print("Highscore: " .. highscore, 10, 60)

end

-- Just a quick way to change score for testing
function love.mousepressed()

score = score + 1

end

function love.quit()

-- Save highscore
if score > highscore then

file = love.filesystem.newFile("highscore.lua", "w")
file:write("highscore = " .. score)
file:close()

end

end


By the way, this issue comes up so often that I think it would be a good idea to include a super-simple key/value database (basically just a glorified table) in LÖVE. So that the answer to "How do I save the highscore becomes"

Code: Select all

love.db.set("Highscore", highscore)

Bascially the idea is that love.db refers to a table which is automatically created, saved, and loaded. To make it clear the table is game specific. I.e. love.db.* would refer to a different table for every game, and it would be saved in the game's save directory.

IceQB
Citizen
Posts: 67
Joined: Sat Nov 03, 2012 1:11 pm

### Re: Saving problem

If I correctly understand action of filesystem.newFile, it must make new file so if i for test write

Code: Select all

file = love.filesystem.newFile("data.txt")
Love dont make any txt file, and the same is happening while saving score.

Edit: I imrove code but effect is the same.

Code: Select all

var = {}
var.score = 0
var.highscore = 0

end
function love.update(dt)
var.score = seconds
if var.score > var.highscore then
var.highscore = seconds
end
end

function love.draw()
local statistics = ("highscore%d"):format(var.highscore)
gfx.print(statistics, 100, 0 )

local statistics = ("score%d"):format(var.score)
gfx.print(statistics, 100, 10 )
end

function love.quit()
if var.score > var.highscore then
file = love.filesystem.newFile("highscore.lua", "w")
file:write("var.highscore = " .. score)
file:close()
end
end


Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Saving problem

Hexenhammer wrote:By the way, this issue comes up so often that I think it would be a good idea to include a super-simple key/value database (basically just a glorified table) in LÖVE.
Why not make a proposal for that: https://bitbucket.org/rude/love/issues

Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

### Re: Saving problem

IceQB wrote:If I correctly understand action of filesystem.newFile, it must make new file so if i for test write
Learning to program step 1: Learn English. Sorry, but your English is so bad that I can barely understand what you are trying to say. And of course that means that you probably cannot really understand our answers either. You could try finding a programming forum where people use your native language, however, sooner or later you will need English.

Code: Select all

var = {}
var.score = 0
var.highscore = 0

No bug here.

Code: Select all

 function love.load()
end

You have to load AND run the Lua file, you only load it.

Code: Select all

  function love.update(dt)
var.score = seconds

What "seconds"? You have never defined a variable called "seconds" and thus seconds is "nil" i.e. this line actually deletes "var.score"!

Code: Select all

if var.score > var.highscore then

And thus it crashes here. "var.score" no longer exists. And the whole approach is wrong anyway. Only update the highscore before you save it.

Code: Select all

function love.draw()
local statistics = ("highscore%d"):format(var.highscore)
gfx.print(statistics, 100, 0 )

local statistics = ("score%d"):format(var.score)
gfx.print(statistics, 100, 10 )
end

"gfx" is another undefined variable.

Here is the whole thing fixed. It still doesn't do anything sensible, though. Using "dt" as a score value makes no sense.

Code: Select all

var = {}
var.score = 0
var.highscore = 0
gfx = love.graphics

if savedData then savedData() end
end

function love.update(dt)
var.score = dt * 1000
end

function love.draw()

local statistics = ("highscore%d"):format(var.highscore)

gfx.print(statistics, 100, 0 )

local statistics = ("score%d"):format(var.score)
gfx.print(statistics, 100, 10 )

end

function love.quit()
if var.score > var.highscore then
file = love.filesystem.newFile("highscore.lua", "w")
file:write("var.highscore = " .. var.score)
file:close()
end
end


Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm