Page 1 of 1

love.filesystem.getWorkingDirectory returns user directory

Posted: Fri Aug 02, 2013 12:33 pm
by Factis
Hello everyone, I got a problem.
So, I need to load files from the folder where .love file is located, so I used love.filesystem.getWorkingDirectory() to get the directory, but this command only returns user directory as love.filesystem.getUserDirectory() command would do.
So, I tried some other ways such as os.execute('dir > tmp.txt'), but it still returns all the files that are in the user directory.

My game is located at: "C:/Documents and Settings/Admin/Рабочий Стол"
I've tried moving my working directory to "C:/" but still love.filesystem.getWorkingDirectory() keep returning me "C:/Documents and Settings/Admin"

Please, help me!

Edit: My apologise, I've re-read what love.filesystem.getWorkingDirectory() command do, and it returns current working directory, sorry, I'm plain dumb.

But there still a problem, how do I return path to the directory where .love file is located?

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Fri Aug 02, 2013 12:44 pm
by Walz.
I think you should be able to access files which are in the same folder as your .love with io

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Fri Aug 02, 2013 1:41 pm
by bartbes
It returns the working directory, as it says. Now, your problem seems to be that you expect that to be the folder your .love is in, it's generally not. (And the io library uses the working directory as a base, btw.)

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Fri Aug 02, 2013 3:20 pm
by Factis
So is there no way to get directory where .love file located? Please, I really need it.

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Fri Aug 02, 2013 3:58 pm
by MPQC
https://love2d.org/wiki/love.filesystem
Files that are opened for read will be looked for in the save directory, and then in the .love archive (in that order). So if a file with a certain filename (and path) exist in both the .love archive and the save folder, the one in the save directory takes precedence.
So, to read a file located in /folder from your current directory (ie "C:/Documents and Settings/Admin/Рабочий Стол/folder/tmp.txt") you would do this:

Code: Select all

exists = love.filesystem.exists("folder/tmp.txt")
	if (exists) then
		for lines in love.filesystem.lines("folder/tmp.txt") do
			-- do stuff here
		end
	end
https://love2d.org/wiki/love.filesystem

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Fri Aug 02, 2013 6:42 pm
by Ranguna259
There's no need to get the current dir, just do what MPQC said if you want to load a file from the local dir you just need to type the name of the file.

Hope that helped

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Thu Aug 08, 2013 8:54 am
by T-Bone
MPQC wrote:https://love2d.org/wiki/love.filesystem
Files that are opened for read will be looked for in the save directory, and then in the .love archive (in that order). So if a file with a certain filename (and path) exist in both the .love archive and the save folder, the one in the save directory takes precedence.
So, to read a file located in /folder from your current directory (ie "C:/Documents and Settings/Admin/Рабочий Стол/folder/tmp.txt") you would do this:

Code: Select all

exists = love.filesystem.exists("folder/tmp.txt")
	if (exists) then
		for lines in love.filesystem.lines("folder/tmp.txt") do
			-- do stuff here
		end
	end
https://love2d.org/wiki/love.filesystem
That won't work if you package your game into a .love, since love.filesystem only looks inside the .love, not in the folder containing the .love. To my knowledge, there's no obvious way to do that.

I guess one option is to not package your game into a .love, and create a script (.bat file on Windows) for starting the game. An alternative solution that may or may not work is to create a script that before launching the game changes the current working directory to wherever you want it to be.

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Sun May 25, 2014 2:35 pm
by Gael
If someone is still interested, I think I have the solution.
Try love.arg.options.game.arg[1], it works for me (I don't know whether it exists for all Löve versions ; I'm on 0.9.1).
It is not a function : it's directly the value, stored as a string.

Code: Select all

print(love.arg.options.game.arg[1])
-- it prints a path like "/home/user/thing/stuff.love"

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Mon May 26, 2014 3:42 am
by davisdude
Interesting. How on Earth did you find out about that?

Re: love.filesystem.getWorkingDirectory returns user directo

Posted: Mon May 26, 2014 7:00 am
by bartbes
In newer love versions you can access the directory your fused game is in by mounting it, therefore love contains [wiki]love.filesystem.getSourceBaseDirectory[/wiki] to find that directory and [wiki]love.filesystem.mount[/wiki] to mount it.