How to save values in Love2d?

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
kingomar
Prole
Posts: 37
Joined: Mon Mar 17, 2014 8:01 pm

How to save values in Love2d?

Post 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!
Check out my game:
- Youtube Video : http://goo.gl/91x6dT
- Download Link : http://goo.gl/6llhmp
User avatar
Jimanzium
Party member
Posts: 103
Joined: Sun Jun 03, 2012 2:39 pm
Contact:

Re: How to save values in Love2d?

Post 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
Last edited by Jimanzium on Sat Apr 26, 2014 6:22 pm, edited 2 times in total.
User avatar
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?

Post 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()
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
MGinshe
Prole
Posts: 31
Joined: Sun Apr 17, 2011 3:50 am
Location: New Zealand

Re: How to save values in Love2d?

Post by MGinshe »

this might be useful. it saves in a human readable format, prevents recursion, and retains iterative keys
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How to save values in Love2d?

Post 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'".
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to save values in Love2d?

Post 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.
Help us help you: attach a .love.
User avatar
CrackedP0t
Citizen
Posts: 69
Joined: Wed May 07, 2014 4:01 am
Contact:

Re: How to save values in Love2d?

Post by CrackedP0t »

JSON is always useful.
Here's a library you could implement to use it: http://regex.info/blog/lua/json
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests