GVovkiv is correct in that you can only load files from your project's "folder" or archive (where main.lua is), and save folder (appdata on windows, etc), but it does not answer the problem here. Lets take a deeper look.
If you take a look at the wiki page for
love.filesystem.getInfo, it states the following:
Returns
table info (nil)
A table containing information about the specified path, or nil if nothing exists at the path.
Given your error message, the directory you're giving to the function is making it a return a nil, and indexing a nil like it's a table is always going to cause a crash. As such, you want to add a guard in your loop:
Code: Select all
for k, file in ipairs(files) do
local info = love.filesystem.getInfo(file)
if info and info.type == 'directory' then -- notice how this line changed; now info.type is checked only if "info" is a non-falsy value
isDirs[k] = file
files[k] = nil
end
end
Now, you may ask - why is getInfo returning nil values in the first place, suggesting that the elements in that path don't exist? Well, if you look at
love.filesystem.getDirectoryItems on the wiki, you'll notice that the function returns
a table of files and subdirectiories in the given path, it does
not give you a path to those files.
As such, you'lll have to do something like:
Code: Select all
for k, file in ipairs(files) do
local info = love.filesystem.getInfo(dir .. "/" .. file) -- prefix the directory, so that we pass the path properly
if info and info.type == 'directory' then
isDirs[k] = file
files[k] = nil
end
end
BTW, the reason I am using = nil instead of table.remove is because table.remove screws up the iteration index and causes problems when iterating like this.
Actually, because you're using ipairs, things will still get screwed up this way as well.
Code: Select all
local test = {1,2,4,5,6,7,9,8,9,10}
test[6] = nil
for k,v in ipairs(test) do
print(k,v)
end
--output:
1 1
2 2
3 4
4 5
5 6
As you see, because I created that gap at index 6 by assigning nil to it, the ipairs loop stops at the first gap (so everything past index six gets ignored). If you want to remove elements from a table as you're iterating through it, you generally use table.remove and iterate in reverse.
Code: Select all
local test = {1,2,3,4,5,6,7,8,9,10}
-- remove element divisible by two
for i = #test, 1, -1 do
if test[i] % 2 == 0 then
table.remove(test, i)
end
end
for k,v in ipairs(test) do
print(k,v)
end
--output:
1 1
2 3
3 5
4 7
5 9
Alternatively, use a while loop and increase the index only when table.remove is not called. In your case though, you can also use pairs in place of ipairs, if you want to use your code as-is. The pairs iterator will not care about gaps in any shape or form, at the cost of worse performance (neglible for one-time or infrequent operations) and unpredictable processing order.