Saving problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
IceQB
Citizen
Posts: 67
Joined: Sat Nov 03, 2012 1:11 pm

Saving problem

Post by IceQB »

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 = 
?)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Saving problem

Post by micha »

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
  • Instead of "dofile" better use love.filesystem.load
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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving problem

Post by Robin »

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

Anyone who has that much access to the filesystem is already "in", so you don't need to worry about security here --- unless you download your highscore table from the internet.
Help us help you: attach a .love.
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Saving problem

Post by Hexenhammer »

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


function love.load()

  -- Load highscore
  savedData = love.filesystem.load("highscore.lua")
  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.
User avatar
IceQB
Citizen
Posts: 67
Joined: Sat Nov 03, 2012 1:11 pm

Re: Saving problem

Post by IceQB »

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

 function love.load()
love.filesystem.load("highscore.lua")
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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving problem

Post by Robin »

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
Help us help you: attach a .love.
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Saving problem

Post by Hexenhammer »

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()
love.filesystem.load("highscore.lua")
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

function love.load()
  savedData = love.filesystem.load("highscore.lua")
  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
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Saving problem

Post by Helvecta »

micha wrote:Instead of "dofile" better use love.filesystem.load
Learned something new today, thanks for that tidbit Micha!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 45 guests