[SOLVED] Help with custom settings / save files?

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
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

[SOLVED] Help with custom settings / save files?

Post by Zorochase »

Hello,
I know this kind of question has come up multiple times on these forums, however, I've tried possible solutions on other posts and yet they haven't seemed to work (using TSerial, Ser, etc..). I'm trying to save specific settings to a file that can be changed ingame.

What is the best way to save a table to a file (possibly without serialization)?
What is the best type of file to save to for the kind of data I'm trying to save?
In what functions should I call the load/save functions? (love.load, love.quit, love.update?)

Here's the table:

Code: Select all

settings = {
	SCALE_X = 3,
	SCALE_Y = 3,
	cursor_SCALE_X = 2,
	cursor_SCALE_Y = 2,
	version = "v0.1.0"
}
SCALE_X/Y will be used to scale all images (except for the cursor, of course), and the version is printed in the beginning title screen and settings page. I'm thinking of adding another setting, autoskip, which if set to true will skip cutscenes. There are a few different data types here and I'm not exactly sure of which can even be written to a file. So far, the game doesn't consist of much at all. Just the startup states and the cursor are drawn. I figured I'd make the startup sequence last about 8 seconds so the settings and eventually level files could be loaded. Do I even need to make it last that long? Again, I know this sort of thing has been asked before, but I'd really appreciate the help. I'm somewhat new to lua and the LÖVE engine, and I haven't had a lot of practice using libraries and the filesystem functions.

That should be it, any help I can get counts. Thank you!
Last edited by Zorochase on Tue May 23, 2017 1:01 am, edited 1 time in total.
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Help with custom settings / save files?

Post by Lucyy »

What is the best way to save a table to a file
Don't know about the best way, but TSerial seems like a pretty good way to me. If you can't get that to work I suppose you could just write the entire table into the file yourself o.o
I'm curious as to why TSerial doesn't work for you though, mind showing the code you tried?

What is the best type of file to save to for the kind of data I'm trying to save?
Since you want to save a lua table I believe the best file extension for this is .lua. This is because you can easily load it with love.filesystem.load, and you can then execute it to return the table inside.

In what functions should I call the load/save functions? (love.load, love.quit, love.update?)
I think this depends on the way your game works, if your game uses only one save, I'd say you can load it in love.load.
If your game uses a system in which you can have multiple save files or save slots, I'd load them once they're needed.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Help with custom settings / save files?

Post by Beelz »

I asked a similar question a while back... Here is the response to it.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help with custom settings / save files?

Post by ivan »

Zorochase wrote: Mon May 22, 2017 12:47 amWhat is the best way to save a table to a file (possibly without serialization)?
There is no "best way to save data". You don't go to the grocery store and ask for the "best type of fruit", right?
Personally, I like to keep the code simple - saving everything as a "flat" table works pretty well.
Zorochase wrote: Mon May 22, 2017 12:47 amWhat is the best type of file to save to for the kind of data I'm trying to save?
Saving the data as pure Lua means that you can later load it up using dofile/require/loadstring.
Sure, there are security issues involved, but it shouldn't be a problem if you understand what you're doing.
Zorochase wrote: Mon May 22, 2017 12:47 amIn what functions should I call the load/save functions? (love.load, love.quit, love.update?)
Whenever you need the data that is stored in the save file.
Zorochase wrote: Mon May 22, 2017 12:47 amDo I even need to make it last that long?
You don't "need" loading screens at all...
Personally, I like to load assets and data in the background while there is something else happening on the screen.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with custom settings / save files?

Post by zorg »

The above things said, if you're loading in large enough file(s), it may take enough time for you to see that it blocks all further code from being executed, meaning that unless you either use something like a coroutine to load in smaller files, and always "pause" the loading in-between; or straight-up use threads to load files in-paralell to the main thread (whether with or without a loading screen), you will see the game "hang" visually.

Not that this should be a big concern for a small game, or small amounts of data, or anything.

Edit: In reply to a deleted post:
Zorochase post_id=212611 time=1495487460 user_id=139194 wrote: I tried Ser, yet with the code I have to save/load, I just get an error that says, "main.lua.32: attempt to call nil value" Is that because the settings.lua file and the main.lua file are two separate files? I don't see how that'd be an issue...
Here's the code for Ser:

Code: Select all

local serialize = require "ser"
function love.load()
	saved_settings = love.filesystem.load('settings.txt')()
end

function love.quit()
	love.filesystem.write('settings.txt', serialize(settings))
end
Would be great if the code was actually the one you got the error from, since the above is not nearly 32 lines long... and without the traceback info, i can't even reason which function would have raised it.

That said, your code serializes a variable called settings, which i don't see being defined anywhere in the above code, although i'd hope Ser wouldn't error just because you wanted to serialize a nil variable.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

Re: Help with custom settings / save files?

Post by Zorochase »

Thanks for the advice, everyone!
I only have one more question.
I figured out the issue with Ser. When I tried to save the settings with Ser, and there was no existing "settings.txt" file, I would get the error, "attempt to call a nil value." I tried manually creating a settings.txt file inside the directory to which it is supposed to save, and it worked. How do I avoid requiring users to manually create a settings.txt file, as well as a text file for everything else that will be saved? I tried doing this before the file was loaded, but it didn't work; I still got the same error, and the file was not created:

Code: Select all

function love.load()
	if not love.filesystem.exists('settings.txt') then
		love.filesystem.newFile('settings.txt')
	end
end
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with custom settings / save files?

Post by zorg »

My guess is newFile doesn't create the file on disk, just the File object in memory (until you read from/write to it, and flush the changes); i guess opening the file and closing it may work; if not, write to it too.

Code: Select all

function love.load()
	if not love.filesystem.exists('settings.txt') then
		local file = love.filesystem.newFile('settings.txt')
		file:open()
		file:close()
	end
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

Re: Help with custom settings / save files?

Post by Zorochase »

zorg wrote: Mon May 22, 2017 9:57 pm My guess is newFile doesn't create the file on disk, just the File object in memory (until you read from/write to it, and flush the changes); i guess opening the file and closing it may work; if not, write to it too.

Code: Select all

function love.load()
	if not love.filesystem.exists('settings.txt') then
		local file = love.filesystem.newFile('settings.txt')
		file:open()
		file:close()
	end
end
Well, opening and closing the file didn't work, however, writing to the file did. I just had it write a new line, and that created the file. Everything's working now!

Code: Select all

	if not love.filesystem.exists('settings.txt') then
		love.filesystem.write('settings.txt', '\n')
	end
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests