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
I need to save variables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Leonardo2450
- Prole
- Posts: 15
- Joined: Wed Jul 17, 2019 12:45 am
- Location: Esta xd
I need to save variables
-Leonardo2450-
[DATA EXPUNGED] Argentinian looking for something to do in his spare time.
[DATA EXPUNGED] Argentinian looking for something to do in his spare time.
Re: I need to save variables
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.
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.
Re: I need to save variables
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:
Sorry for my bad english.
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}
Re: I need to save variables
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:
Usage:
If you save your data as a list (numerically indexed table) it's even simpler:
Usage:
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!
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
Code: Select all
out = simpleSer({
playerName = "Joe",
width = love.graphics.getWidth(),
height = love.graphics.getHeight(),
})
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
Code: Select all
out = superSimpleSer({"Joe", love.graphics.getWidth(), love.graphics.getHeight()})
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!
Who is online
Users browsing this forum: No registered users and 221 guests