Listing Save Files (Solved)

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
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Listing Save Files (Solved)

Post by tentus » Sat Nov 13, 2010 9:52 pm

-------------
Solved, see this post for the final solution: http://love2d.org/forums/viewtopic.php? ... =10#p22335
-------------

So, I'm working on making save files for my game. You choose singleplayer from the main menu, and the menu table gets repopulated with a list of existing save files, which will be .lua files that will overwrite the default player values. I have the following code:

Code: Select all

local filesTable = love.filesystem.enumerate("")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
This works just fine, except that it also lists the contents of the root directory in the .love file (as it's supposed to, according to the documentation). Short of making a bunch of and not conditions, is there an easy way to not list the contents of my .love file?
Last edited by tentus on Sun Nov 14, 2010 5:23 am, edited 1 time in total.
Kurosuke needs beta testers

User avatar
thelinx
The Strongest
Posts: 848
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Listing Save Files

Post by thelinx » Sat Nov 13, 2010 9:56 pm

Nope.

User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Listing Save Files

Post by zac352 » Sat Nov 13, 2010 10:20 pm

If we're thinking of the same thing,

Code: Select all

default={"main.lua","conf.lua","image.png"}
function checkIsDefault(f)
	for _,v in pairs(default) do
		if v==f then
			return true
		end
	end
	return false
end
local filesTable = love.filesystem.enumerate("")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) and not checkIsDefault(v) then
		table.insert(self.mainmenu, v)
	end
end
Hello, I am not dead.

User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Listing Save Files

Post by ninwa » Sat Nov 13, 2010 10:54 pm

I propose a simpler solution:

Code: Select all

local filesTable = love.filesystem.enumerate("")
for i,v in ipairs(filesTable) do
    if  v:find(".sav") ~= nil and love.filesystem.isFile(v) then
        table.insert(self.mainmenu, v)
   end
end
Assuming save-files have the extension '.sav'

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Listing Save Files

Post by Robin » Sat Nov 13, 2010 11:03 pm

Or, even better, putting all save games in a dedicated subfolder. (You know, like "saves/".)
Help us help you: attach a .love.

User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Listing Save Files

Post by ninwa » Sat Nov 13, 2010 11:13 pm

Robin wrote:Or, even better, putting all save games in a dedicated subfolder. (You know, like "saves/".)
Don't be ridiculous.

User avatar
Motig
Prole
Posts: 28
Joined: Fri Oct 15, 2010 3:43 am

Re: Listing Save Files

Post by Motig » Sat Nov 13, 2010 11:26 pm

Robin wrote:Or, even better, putting all save games in a dedicated subfolder. (You know, like "saves/".)
I vote for this solution!

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Listing Save Files

Post by tentus » Sun Nov 14, 2010 12:43 am

Robin wrote:Or, even better, putting all save games in a dedicated subfolder. (You know, like "saves/".)
I tried before posting, but received the following results:

Code: Select all

local filesTable = love.filesystem.enumerate("saves/")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns nothing.

Code: Select all

local filesTable = love.filesystem.enumerate("saves")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns nothing.

Code: Select all

local filesTable = love.filesystem.enumerate("/saves")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns nothing.

Code: Select all

local filesTable = love.filesystem.enumerate("/saves/")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns nothing.

Code: Select all

love.filesystem.setIdentity("kurosuke/saves")
local filesTable = love.filesystem.enumerate("")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns everything in the root directory (of the save folder and .love file) as well as the saves directory.

Code: Select all

love.filesystem.setIdentity("/saves")
local filesTable = love.filesystem.enumerate("")
for i,v in ipairs(filesTable) do
	if love.filesystem.isFile(v) then
		table.insert(self.mainmenu, v)
	end
end
Returns the root but not the saves directory.

Anyone care to explain what I'm doing wrong, because I don't see it. The old "bash-head-on-keyboard" strategy of coding is letting me down. (and yes, I am sure that the folder and files are there and named correctly... the returns-too-much result confirms that.)

I would like to use a save folder, because I'm already storing some configuration settings in the root of the save area, but with the above code refusing to cooperate...
Kurosuke needs beta testers

User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Listing Save Files

Post by Mud » Sun Nov 14, 2010 2:47 am

Code: Select all

love.filesystem.setIdentity("kurosuke/")
local filesTable = love.filesystem.enumerate("saves")
for i,v in ipairs(filesTable) do
   if love.filesystem.isFile(v) then
      table.insert(self.mainmenu, v)
   end
end
(and of course, make sure you have something in the saves folder before testing)

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Listing Save Files

Post by tentus » Sun Nov 14, 2010 2:52 am

Mud wrote:

Code: Select all

love.filesystem.setIdentity("kurosuke/")
local filesTable = love.filesystem.enumerate("saves")
for i,v in ipairs(filesTable) do
   if love.filesystem.isFile(v) then
      table.insert(self.mainmenu, v)
   end
end
(and of course, make sure you have something in the saves folder before testing)
Still no dice. Just replicated the results on another machine too, so I doubt it's my desktop...
Kurosuke needs beta testers

Post Reply

Who is online

Users browsing this forum: MrFariator and 31 guests