Saving settings between sessions?

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

Re: Saving settings between sessions?

Post by Robin »

If you open a file for writing (

Code: Select all

local f = love.filesystem.newFile("bla")
f:open("w")
), the file is created.

AFAIK, newFile doesn't even try to open a file, but f:open() does.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Saving settings between sessions?

Post by bartbes »

Indeed, newFile doesn't open, and opening with write will create a new file.

EDIT: I might be able to give you a working example this afternoon.
User avatar
mrklotz
Prole
Posts: 17
Joined: Mon Jan 25, 2010 1:53 pm

Re: Saving settings between sessions?

Post by mrklotz »

Robin wrote:If you open a file for writing (

Code: Select all

local f = love.filesystem.newFile("bla")
f:open("w")
), the file is created.

AFAIK, newFile doesn't even try to open a file, but f:open() does.
Yes that's what I mean, if you open a non existant file with write it would be created, however to open a file you would have to first set a fileobject and according to the docs (wich are probably wrong) you can use love.filesystem.newFile("filename") only to assign file objects to existing files. Meaning you can't assign a fileobject preventing you from using fileobject:open("w").
http://love2d.org/docs/love_filesystem_newFile_1.html wrote:love.filesystem.newFile( filename )

Creates a new File object. An error will occur if the specified file does not exist.
bartbes wrote: EDIT: I might be able to give you a working example this afternoon.
That would be awesome., I'll do some own research in the meantime.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving settings between sessions?

Post by Robin »

mrklotz wrote:Yes that's what I mean, if you open a non existant file with write it would be created, however to open a file you would have to first set a fileobject and according to the docs (wich are probably wrong) you can use love.filesystem.newFile("filename") only to assign file objects to existing files. Meaning you can't assign a fileobject preventing you from using fileobject:open("w").
I fixed the docs.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Saving settings between sessions?

Post by bartbes »

Haven't tested it, but this should work.

Code: Select all

--conf.lua

function love.conf(t)
	t.screen = false
	--whatever else you want
end

--main.lua

function love.load()
	local w, h
	if love.filesystem.exists("configuration.txt")
		local f = love.filesystem.newFile("configuration.txt")
		f:open("r")
		local it = f:lines()
		w = tonumber(it()) --read first line as width
		h = tonumber(it()) --and second as height
		f:close()
		--you might want to do some more checks for valid data here
	else
		w = 800
		h = 600
	end
	setres(800, 600)
end

function savetheshit(w, h) --the function that saves it, w being width, h being height
	local f = love.filesystem.newFile("configuration.txt")
	f:open("w")
	f:write(("%d\n%d"):format(w, h))
	f:close()
end

function setres(w, h)
	love.graphics.setMode(w, h, false, true, 0)
end
User avatar
mrklotz
Prole
Posts: 17
Joined: Mon Jan 25, 2010 1:53 pm

Re: Saving settings between sessions?

Post by mrklotz »

bartbes wrote:Haven't tested it, but this should work.
You forgot the "then" after the first if- fork but other than that it runs perfectly fine. ;)
It's a pretty clear example but I'd like to add 2 questions, I've browsed the lua tutorial wiki as well pil to some extent but haven't found what I wanted to know.

So basically if I want to write a "clean" config file there are two problems, with clean config files I mean something like this:

Code: Select all

[sectionheader]
setting1= 800
setting2 = 600
setting3= true

[sectionheader2]
anothersetting= 12
Ok so the first question: Is there a easy way to "break up" the strings to use them in tonumber() , basically something like

Code: Select all

tonumber(breakup("setting1= 800"))
would return 800, --meaning the breakup() would only return the numerals out of the string
of course the string "setting1= 800" would be supplied by f:lines() in the real code.

The second question: In such a config file there would be lines that can/should be ignored by the read function (sectionheaders, empty lines), of course you could simply write a function that checks if a line starts with "[" or an empty space, but there should be a simpler way like telling f:lines() wich line to read, right?

Edit: Now that I think about it I'd like to add another thing, is there something similiar to tonumbers() for booleans? converting the strings "true" and "false"?
If not it's not a big deal, just use 0 /1 in the config and when the var from the conf == 0 , set the var to false.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving settings between sessions?

Post by Robin »

I think you overly complicate things for yourself.

You know Lua was originally a config language?

Just make the config file use Lua syntax and load the file, for example with

Code: Select all

love.filesystem.load("configfile")()
-- don't forget the last (), that's important.

p.s. 1000th post :nyu:
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Saving settings between sessions?

Post by bartbes »

Or pattern matching:

Code: Select all

local key, value = str:match("(%w+)=(%d+)")
if key and value then
    conf[key] = value
end
Or similar, lines that do not match the pattern are ignored.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests