Page 1 of 1

Saving and loading from love.filesystem directory

Posted: Sat Sep 01, 2018 7:39 am
by QwertyDragon83
So before, all the saving and loading I've done has been in a separate folder using the standard Lua IO. However, I couldn't figure out how to make the Lua IO deal with anything other than .txt files. Here's a quick program that generates an image, saves it in the love.filesystem save directory, and fails miserably as it attempts to load such image. "..test/test.png does not exist" So I decided to check if the default directory even exists, and it does. There I found my generated image successfully saved. However, love.filesystem.exists(love.filesystem.getSaveDirectory()) returns nil, so according to love, its own default directory doesn't exist.
I tossed the code into an exe, admin ran it, and got the same result. What good is a save directory if you can't access it again? How can I load the image?

Here's the .lua and .love:

Re: Saving and loading from love.filesystem directory

Posted: Sat Sep 01, 2018 8:58 am
by Astorek86
Did you used love.filesystem.setIdentity before using IO-Stuff? If not, than that can be the Problem, because the Behavior of love.filesystem.write, getSaveDirectory etc. can be different...

Try to set the Identity on the very first state of your Game (like, the first line after "function love.load"), maybe that will fix your Problem.

Re: Saving and loading from love.filesystem directory

Posted: Sat Sep 01, 2018 10:25 am
by zorg
Or, just use Config Files, and set the identity there.

Re: Saving and loading from love.filesystem directory

Posted: Sat Sep 01, 2018 4:41 pm
by pgimeno
You should not use getSaveDirectory to save and load files using love.filesystem. To save a file, use love.filesystem.write("filename", ...) and to load it back, use love.filesystem.read("filename", ...)

Explanation: the root directory exposed by love.filesystem is the union of two physical directories, the application's directory and the save directory (the latter takes precedence if there are files named the same). When writing a file to the root dir via love.filesystem, you're actually writing it in your save directory. All LÖVE functions use love.filesystem, therefore they all write to the save directory and read from either the save directory or the application directory.

What love.filesystem.getSaveDirectory does is return the real filesystem path, but you should not be concerned with it when using love.filesystem. It may be sometimes useful for using Lua IO functions or some other functions that require a path in the real filesystem (I don't think there are any in LÖVE but if you use any external libs, or FFI calls, you may need it).