love.filesystem.getWorkingDirectory returns user directory

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
Factis
Prole
Posts: 14
Joined: Sun Sep 23, 2012 5:38 pm

love.filesystem.getWorkingDirectory returns user directory

Post 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?
User avatar
Walz.
Prole
Posts: 13
Joined: Mon May 06, 2013 2:42 pm

Re: love.filesystem.getWorkingDirectory returns user directo

Post by Walz. »

I think you should be able to access files which are in the same folder as your .love with io
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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.)
Factis
Prole
Posts: 14
Joined: Sun Sep 23, 2012 5:38 pm

Re: love.filesystem.getWorkingDirectory returns user directo

Post by Factis »

So is there no way to get directory where .love file located? Please, I really need it.
User avatar
MPQC
Citizen
Posts: 65
Joined: Fri Jun 28, 2013 2:45 pm

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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.
Gael
Prole
Posts: 2
Joined: Sun May 11, 2014 9:59 am

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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"
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: love.filesystem.getWorkingDirectory returns user directo

Post by davisdude »

Interesting. How on Earth did you find out about that?
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: love.filesystem.getWorkingDirectory returns user directo

Post 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.
Post Reply

Who is online

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