Hello everyone! i would like to use love.filesystem for one prupose and that is to know the values of certain variables as In:
Medium-Unlocked = false
So how can i write to the file system? and how can i make one, read one, and know the values from the file? Thanks!
How to save values in Love2d?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How to save values in Love2d?
Hi, you should look at this wiki page: http://www.love2d.org/wiki/love.filesystem
It has all the functions for the love2D fileyste. The basic things you'll need are and
An example for these would be
It has all the functions for the love2D fileyste. The basic things you'll need are
Code: Select all
love.filesystem.write(filename,data
Code: Select all
love.filesystem.read(filename)
Code: Select all
easy_unlocked = true
medium_unlocked = false
function saveGame()
local save = ""
if(easy_unlocked) then
save = save.."easy_unlocked = true;"
end
if(medium_unlocked) then
save = save.."medium_unlocked = true;"
end
love.filesystem.write("save.txt",save)
end
function loadGame()
toload = love.filesystem.load("save.txt")
toload()
end
Last edited by Jimanzium on Sat Apr 26, 2014 6:22 pm, edited 2 times in total.
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: How to save values in Love2d?
You can do this to load (uses last post saving system):
This would require the string:split function (not by me):
But I think it'd be better if you use lua as your main form of saving-loading data:
To save them, you can do this:
And the table-to-string function:
To load, you have two ways: "require" at your code's beginning:
Or later, with [wiki]love.filesystem.load[/wiki]:
Code: Select all
function loadData()
if love.filesystem.exists("savedata.txt") then
local txt = love.filesystem.read("savedata.txt")
local s = txt:split(";")
for i = 1, #s do
local s2 = s[i]:split(" = ")
local value = s2[2]
if value == "true" then
value = true
elseif value == "false" then
value = false
elseif value == "nil" then
value = nil
elseif tonumber(value) then
value = tonumber(value)
end
_G[s2[1]] = value
end
end
end
Code: Select all
function string:split(delimiter)
local result = {}
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
To save them, you can do this:
Code: Select all
function saveData(t) --t is a table of variables names, like {"speed", "gravity", "mapname"}
local s = ""
for i = 1, #t do
s = s .. t[i] .. " = "
if type(_G[t[i]]) == "table" then
s = s .. tabletostring(t[i])
elseif type(_G[t[i]]) == "string" then
s = s .. "\"" .. _G[t[i]] .. "\""
else
s = s .. tostring(_G[t[i]])
end
if i ~= #t then
s = s .. "\r\n"
end
end
love.filesystem.write("savedata.lua", s)
end
Code: Select all
function tabletostring(t)
local t = t
if type(t) == "string" then
t = _G[t]
end
local ret = "{"
for i, v in pairs(t) do
if type(i) ~= "number" then
ret = ret .. i .. " = "
end
if type(v) == "table" then
ret = ret .. tabletostring(v)
elseif type(v) == "string" then
ret = ret .. "\"" .. v .. "\""
else
ret = ret .. tostring(v)
end
ret = ret .. ", "
end
return string.sub(ret, 1, -3) .. "}"
end
Code: Select all
require "savedata"
Or later, with [wiki]love.filesystem.load[/wiki]:
Code: Select all
local chunk = love.filesystem.load("savedata.lua")
chunk()
Re: How to save values in Love2d?
this might be useful. it saves in a human readable format, prevents recursion, and retains iterative keys
Re: How to save values in Love2d?
While I don't think it's very good practise, I typically just write valid Lua code when saving stuff, and that way I can load it by just doing "require 'savefilename'".
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: How to save values in Love2d?
That has several problems. For one thing, circular data structures are not reproduced completely. For another, strings are not quoted, which is a problem if you try to serialize a string that contains things like newlines and quote characters. A third big one is that it only supports two kinds of tables: sequences with no non-sequence keys and tables with only string keys. It also has quadratic run-time behaviour (N^2) due to the use of string concatenation. I could go on.MGinshe wrote:this might be useful. it saves in a human readable format, prevents recursion, and retains iterative keys
I recommend using Ser instead of keyvalues.
Help us help you: attach a .love.
- CrackedP0t
- Citizen
- Posts: 69
- Joined: Wed May 07, 2014 4:01 am
- Contact:
Re: How to save values in Love2d?
JSON is always useful.
Here's a library you could implement to use it: http://regex.info/blog/lua/json
Here's a library you could implement to use it: http://regex.info/blog/lua/json
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
Here, have an umlaut. Ö
Who is online
Users browsing this forum: Ahrefs [Bot] and 4 guests