Getting file names from directory

I need to get and store the file names from game’s directory, this code works in simulator;

local path = system.pathForFile("res/test/pist/t1/", system.ResourceDirectory)
local pistols = {}; local i=1;
for file in lfs.dir(path) do
   pistols[i] = file
   i = i+1
end

so this is okay in pc and getting file names fine but when i build and make apk for my phone, it gives an Runtime error like this;

C:\Users\Me\Desktop\asd\main.lua:93: bad argument #1 to 'dir' (string expected, got nil)

I guess it s because path gives the path from computer, not for mobile, how can i fix that ? or you have an idea to get the file names with an another way ?

I don’t remember if this applied to both iOS and Android, but at least Android is case sensitive. Windows isn’t case sensitive, which would explain why it doesn’t crash on the simulator.

On that note, if the files that you are looking for are .lua files, then they won’t be found after compilation either.

I am looking for .png files to display later.

local path = system.pathForFile(nil)

I found out this code returns the main path, i made it to display in screen as text, at simulator it gives C:\Users\Me\Desktop\asd but when i use the apk in phone it doesn’t display anything because path returns nil so it doesnt have a main path that’s why i can’t add the directory to continue. Do you know why ?

it actually returns the true path if i make system.DocumentsDirectory but returns nil in ResourcesDirectory.

@roaminggamer sir do you know the solution ? system.ResourcesDirectory returns nil when i want the path on Android.

Just upload a small sample project that demonstrates your issue.

local path = system.pathForFile(nil) -- doesn't change if i give system.ResourcesDirectory in 2nd parameter

if (path == nil) then
   local t = display.newText("nil", 250, 50, native.systemFont, 30)
else
   local t = display.newText(path, 250, 50, native.systemFont, 30)
end

So this code displays the project’s main path in simulator but it dispalys “nil” text on Android. Because system.pathForFile returns nil on Android. The reason i want the path is i will use it on continue to get the file names in project’s folder.

for file in lfs.dir(path.."/res/test/pist/t1") do
    pistols[i] = file
    i = i+1
end

This works on simulator and stores the file names but doesn’t work on Android because of path is nil. I will prolly have 1000 images on folder at total so i must take their names from folder and use it in my code for my needs.

Manuallly
Since you’re looking at the resource directory, this means the files there will never change after the build. So, the easiest way is to do this is manually. i.e. Create a table somewhere in your code with paths to all the files.

Dynamically
Of course, if you’re like me, you may prefer to have your game code do this for your. So, if you change file names, add files, remove files, the game code ‘adjusts’ while you develop.

You can do this too.

Just write a snippet of code that executes every time main.lua is executed and structure it like this:

If on simulator, discover all files in folder, save <relative> paths in a table, json encode the table, and write it to a file.

... later
Load the file as needed, json decode it, and violla you have a list of files in the path/folder.

Now, every time you run your game in the simulator, it will rebuild the file and then use it. However, when you build the game and distribute it, the game will skip the generate step (on the device) and use the last written file (from the last simulator run), because all files LUA, TXT, PNG, etc are included in the build.

SSK has all the code needed to do the individual steps:


Since you have most of the code already, the only trick is to know when you’re on the simulator or not:

_G.onSimulator        = system.getInfo("environment") == "simulator"

above taken from: https://github.com/roaminggamer/SSK2/blob/master/ssk2/core/variables.lua

1 Like