I need to save variables

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
Leonardo2450
Prole
Posts: 5
Joined: Wed Jul 17, 2019 12:45 am
Location: Esta xd

I need to save variables

Post by Leonardo2450 » Mon Sep 09, 2019 10:01 pm

Actually my project in Love2D is moving on the right track. However, I need to start with the loading / saving system and I have no idea how to do it. Can you help me? What is the easiest way and which is the most efficient?

First of all, Thanks

User avatar
raidho36
Party member
Posts: 1860
Joined: Mon Jun 17, 2013 12:00 pm

Re: I need to save variables

Post by raidho36 » Mon Sep 09, 2019 11:11 pm

The easiest way is to shove your data into a table, generate JSON out of it, and write it into a file. To load it, you read contents of the file, pass it into JSON decoder, and on the output you get a table with the same data you put in it during saving.

You can use whatever format you wish for this, whichever way you see fit to write down some ones and zeroes. But most of the time you can get away with a simple text format. And JSON in particular is very widely supported.

User avatar
AdrianN
Citizen
Posts: 63
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: I need to save variables

Post by AdrianN » Tue Sep 10, 2019 5:27 am

Hi, I think serialize Lua's file is a good way. Encode and decode JSON is a bit unnecessary, because Lua can do it.

Using Ser library to serialize your data
https://github.com/gvx/Ser

Config your identity file
https://love2d.org/wiki/Config_Files

Example:

Code: Select all


--load data
if love.filesystem.getInfo("score.lua") then
      data =love.filesystem.load("score.lua")()
      self.score=data.score
else
    love.filesystem.write("score.lua",serialize({score=0}))
end

--save data

if love.filesystem.getInfo("score.lua") then
	local old_data=love.filesystem.load("score.lua")()

	if old_data.score<self.score then
		love.filesystem.write("score.lua",serialize({score=self.score}))
	end
end

--Ser output score.lua

return {score=0}

Sorry for my bad english.

User avatar
ivan
Party member
Posts: 1479
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: I need to save variables

Post by ivan » Tue Sep 10, 2019 5:18 pm

The easiest way by far is to save your data as a Lua table.
Lua tables can be loaded using require/dofile/loadstring which is considerably simpler than parsing JSON.
So the problem becomes "how do you serialize a Lua table?"
If you keep your tables simple, you can do it with very little code:

Code: Select all

-- simple and limited serialization
-- keys must be strings, values could be strings or numbers
function simpleSer(t)
  local out = "return {\n"
  for n, v in pairs(t) do
    if type(v) == "string" then
      v = string.format("%q", v)
    else
      v = tostring(v)
    end
    out = out..(n.."="..v..",\n")
  end
  out = out.."}"
  return out
end
Usage:

Code: Select all

out = simpleSer({
  playerName = "Joe",
  width = love.graphics.getWidth(),
  height = love.graphics.getHeight(),
})
If you save your data as a list (numerically indexed table) it's even simpler:

Code: Select all

-- super simple and limited serialization
-- keys must be numbers, values could be strings or numbers
function superSimpleSer(t)
  local list = {}
  for k, v in ipairs(t) do
    if type(v) == "string" then
      v = string.format("%q", v)
    end
    list[k] = v
  end
  return "{"..table.concat(list, ",").."}"
end
Usage:

Code: Select all

out = superSimpleSer({"Joe", love.graphics.getWidth(), love.graphics.getHeight()})
In reality, you rarely need to save more complex stuff.
Generally speaking, it depends on what you are saving - if you just want to save the game's options or progress, then it's best to keep it simple.
If you want to save complex stuff then you need a special "file format" or something like the "Ser" library.
Robin's "Ser" library is pretty good if you want to save nested tables and so on (but it's usually overkill 99% of the time).
Good luck!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], ShoesForClues and 3 guests