Substitution for system.ResourceDirectory

I’ve been using system.ResourceDirectory in order to read image files for a simpler way to add and remove image files from my game. This would allow me to change the images used for objects instead of hard coding in the path. However I’ve noticed that when I build for android that there is an exception that throws a runtime error if I try to do the same thing that works on iOS. I was wondering if there was a reasonable workaround. All I’m trying to do is take a folder and get an array of all of the files that start with a substring. 

local function getBrickNames() local numberOfFiles = 0 local doc\_path = system.pathForFile( "/images", system.ResourceDirectory ) local filenames = {} for file in lfs.dir(doc\_path) do local filename = tostring(file) if ( string.starts( filename, "brick\_") ) then table.insert(filenames, filename ) end end return filenames end

Thank you,

Jarrod Lofy

Please read the “Gotchas” for system.ResouresDirectory:  https://docs.coronalabs.com/api/library/system/ResourceDirectory.html

Rob

Thank you Rob, 

I had but didn’t understand what I could do instead. I know that I can’t use the current way, but I wasn’t sure about a workaround. All I’m trying to do is get an array of strings of filenames. 

Thank you,

Jarrod

@lofy,

You can’t iterate over ResourceDirectory on Android.  There are two options that come to mind however:

  1. Create A Table Of Files and use it when publishing.

You know what files you have at the time you go to publish, so …

  • Simply modify your code such that when running in the simulator, you create a table of files with their paths. 
  • Then save the table to the documents folder.
  • Copy that folder to your root folder before you publish.
  • Add code to detect when you’re running on Android and to read that file as the index instead of searching.

It sounds complicated, but this is pretty simple actually.

  1. Use documents folder -  You can iterate the documensdirectory, so …
  • Zip the folder you want to iterate/search.
  • Copy that zip file to you app root.
  • Use the zip plugin to unzip it and copy the files to your documents directory.
  • Iterate and get the files from your documents directory.

I don’t like this idea much as it essentially takes up 3x the space of having the files in resource.

Note: I could provide you a solution like #1 that goes one step further and autocreates that directory file for you right in the app folder.  This way you have nothing to maintain and the app runs much like it does today.  I could do this as a level-2 hit, but i’d need to see your project code to test the solution.  If you’re interested, follow up by following the directions here: https://forums.coronalabs.com/topic/69420-hire-a-hitman-problem-solver-is-back/

@roaminggamer

Thank you again. That’s definitely a reasonable workaround. However, because I don’t change these files all too often I did decide that it was just easier to manually keep the file names saved in an array instead. 

Jarrod

Please read the “Gotchas” for system.ResouresDirectory:  https://docs.coronalabs.com/api/library/system/ResourceDirectory.html

Rob

Thank you Rob, 

I had but didn’t understand what I could do instead. I know that I can’t use the current way, but I wasn’t sure about a workaround. All I’m trying to do is get an array of strings of filenames. 

Thank you,

Jarrod

@lofy,

You can’t iterate over ResourceDirectory on Android.  There are two options that come to mind however:

  1. Create A Table Of Files and use it when publishing.

You know what files you have at the time you go to publish, so …

  • Simply modify your code such that when running in the simulator, you create a table of files with their paths. 
  • Then save the table to the documents folder.
  • Copy that folder to your root folder before you publish.
  • Add code to detect when you’re running on Android and to read that file as the index instead of searching.

It sounds complicated, but this is pretty simple actually.

  1. Use documents folder -  You can iterate the documensdirectory, so …
  • Zip the folder you want to iterate/search.
  • Copy that zip file to you app root.
  • Use the zip plugin to unzip it and copy the files to your documents directory.
  • Iterate and get the files from your documents directory.

I don’t like this idea much as it essentially takes up 3x the space of having the files in resource.

Note: I could provide you a solution like #1 that goes one step further and autocreates that directory file for you right in the app folder.  This way you have nothing to maintain and the app runs much like it does today.  I could do this as a level-2 hit, but i’d need to see your project code to test the solution.  If you’re interested, follow up by following the directions here: https://forums.coronalabs.com/topic/69420-hire-a-hitman-problem-solver-is-back/

@roaminggamer

Thank you again. That’s definitely a reasonable workaround. However, because I don’t change these files all too often I did decide that it was just easier to manually keep the file names saved in an array instead. 

Jarrod