Page 1 of 1

How to save values in Love2d?

Posted: Sat Apr 26, 2014 4:13 pm
by kingomar
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!

Re: How to save values in Love2d?

Posted: Sat Apr 26, 2014 5:49 pm
by Jimanzium
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

Code: Select all

love.filesystem.write(filename,data
and

Code: Select all

love.filesystem.read(filename)
An example for these would be

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

Re: How to save values in Love2d?

Posted: Sat Apr 26, 2014 6:05 pm
by HugoBDesigner
You can do this to load (uses last post saving system):

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
This would require the string:split function (not by me):

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
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:

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
And the table-to-string function:

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
To load, you have two ways: "require" at your code's beginning:

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?

Posted: Sun Apr 27, 2014 10:27 am
by MGinshe
this might be useful. it saves in a human readable format, prevents recursion, and retains iterative keys

Re: How to save values in Love2d?

Posted: Mon Apr 28, 2014 9:09 am
by T-Bone
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'".

Re: How to save values in Love2d?

Posted: Sun May 11, 2014 8:55 am
by Robin
MGinshe wrote:this might be useful. it saves in a human readable format, prevents recursion, and retains iterative keys
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.

I recommend using Ser instead of keyvalues.

Re: How to save values in Love2d?

Posted: Tue May 13, 2014 4:35 am
by CrackedP0t
JSON is always useful.
Here's a library you could implement to use it: http://regex.info/blog/lua/json