Organizing you project's files question

I was reading the tutorial on how to get organized, which is very helpful.

http://coronalabs.com/blog/2012/07/10/best-practices-for-organizing-projects/

I just wanted to be extra careful and wanted to make sure that the files can be read properly by corona or certain devices. Is there anything that I have to be careful about?

I read some of the comments, and am curious if they still apply or changed since then. These are the comments:

_ ---- _

An important note if you’re going to be opening files from a directory. For example maybe you’ve got some configuration data in data/config.json. You must use system.pathForFile() when opening the file with io.open(), you cannot just use a relative path like you can with display.newImage(). This is tricky because a relative path passed in will work in the simulator but will *not* work on device.

_ _

_ One thing which has been mentioned, but which I use a lot, are the use symbolic/hard links to import common code into the project. _

_ You have to be careful, because the compiler doesn’t play nice with softlinked directories in a few cases, but it means that common images and code can be stored in one place rather than duplicated across multiple projects. _

_ ---- _

_ I thought subfolders only would work for iOS not Android. _

_ _

That’s great. I guess I hadn’t seen any big announcement about it being safe to use subfolders in all cases (as of earlier this year there were still limitations on sound and video files under Android). I poked around on the forums, and it seem like this is now supported on all platforms/devices. So that’s great. I’m going to go start organizing my projects now.

_ I’m still a little annoyed that I have to have 12 icons in the root directory. It would sure be nice if we could get the build tools to look in, oh, I don’t know, an “icons” subfolder _

  1. Can’t speak to this one except that it’s probably true; you’re going to want to use system.pathForFile() when using io commands. Most Corona users probably don’t need to worry about this since display.* uses relative paths.

  2. Unclear what this means. I’ve never tried using symbolic linked folders in my projects before though.

  3. Subfolders work on both platforms AFAIK, though again you have to use system.pathForFile. Something important I also learned recently is that while you can point at those folders, some files inside cannot be detected. Some image/sound formats for Android specifically, but for example you can’t use LFS to detect Lua files.

  4. There is still no way to organize icons that I know of.

Thank you Richard. I appreciate your answers. I will be using some .json files for sprites and animation and physics because of the 3rd party tools.

Now I am wondering if system.pathForFile() will actually run for both simulator and devices. Also I wonder if io commands and system.pathForFile() will actually effect the performance.

As for unreadable data in folders for Androids, that’s very unfortunate and its a pretty big hole in the system. Now, I am not too sure if I should organize them in forlders.

Any suggestions??

Oh, I would follow that articles idea for organizing. I use a much similar structure for folders. But what I do recommend doing is, instead of writing some sort of way to auto detect lua/sound/graphics files, just manually declare them in a table somewhere in your code. That way you don’t have to remember filenames OR hope both OSes are good enough to find the file you’re thinking of.

Thank you for your recommendation.

Is this manual declaration in a table a common or good practice? Can you also elaborate more about it, may be with example. Lets say I have a attack.json file in a data folder?

I am also still testing out system.pathForFile() since I haven’t used it before.

Thank you again.

Okay, so specifically here’s what’s going on:

  1. system.pathForFile() works fine on both platforms, and should be used if needed. (Since Android doesn’t actually have a resourceDirectory, that function effectively simulates it.)

With two exceptions:

i. Neither platform can find *.lua. These files are compiled, so when you use require() it’s just pointing at compiled code anyway.

ii. Android can’t see .html / .htm / .3gp / .m4v / .mp4 / .png /.jpg / .ttf either. This is a file-system limitation.

That means you can’t auto-detect those formats. Other formats (.json, .po, etc.) should be detectable easily using system.pathForFile() and the Lua File System (lfs).

  1. Arrays of assets are a good idea because you can write your own convenience functions to do things faster. Like, to use sprites in Corona you normally have to:

a. Create the imageSheet reference using graphics.newImageSheet()

b. Create the animation table

c. Create the sprite using display.newSprite()…while having (a) and (b) in scope. 

But if you had them all in their own Lua file with a table, you could write a convenience function like makeSprite() that just needs a table index and sprite name. (ex: makeSprite(1, “cool dude”)

It’s super easy to make files and keep track of assets on your own, ex:

-- assets.lua, or whatever you want to call it... local assets = {} -- assets[1] = { type="image", file="test.png" } assets[2] = { type="sheet", file="sheet1.png" } function assets.fetchName( index ) return assets[index] end return assets -- end of file -- Then you can just 'local assets = require("assets")' anywhere to add visibility to this table.

Thank you Richard. I will give it a try and see how it goes. I will have more questions probably.  :slight_smile:

  1. Can’t speak to this one except that it’s probably true; you’re going to want to use system.pathForFile() when using io commands. Most Corona users probably don’t need to worry about this since display.* uses relative paths.

  2. Unclear what this means. I’ve never tried using symbolic linked folders in my projects before though.

  3. Subfolders work on both platforms AFAIK, though again you have to use system.pathForFile. Something important I also learned recently is that while you can point at those folders, some files inside cannot be detected. Some image/sound formats for Android specifically, but for example you can’t use LFS to detect Lua files.

  4. There is still no way to organize icons that I know of.

Thank you Richard. I appreciate your answers. I will be using some .json files for sprites and animation and physics because of the 3rd party tools.

Now I am wondering if system.pathForFile() will actually run for both simulator and devices. Also I wonder if io commands and system.pathForFile() will actually effect the performance.

As for unreadable data in folders for Androids, that’s very unfortunate and its a pretty big hole in the system. Now, I am not too sure if I should organize them in forlders.

Any suggestions??

Oh, I would follow that articles idea for organizing. I use a much similar structure for folders. But what I do recommend doing is, instead of writing some sort of way to auto detect lua/sound/graphics files, just manually declare them in a table somewhere in your code. That way you don’t have to remember filenames OR hope both OSes are good enough to find the file you’re thinking of.

Thank you for your recommendation.

Is this manual declaration in a table a common or good practice? Can you also elaborate more about it, may be with example. Lets say I have a attack.json file in a data folder?

I am also still testing out system.pathForFile() since I haven’t used it before.

Thank you again.

Okay, so specifically here’s what’s going on:

  1. system.pathForFile() works fine on both platforms, and should be used if needed. (Since Android doesn’t actually have a resourceDirectory, that function effectively simulates it.)

With two exceptions:

i. Neither platform can find *.lua. These files are compiled, so when you use require() it’s just pointing at compiled code anyway.

ii. Android can’t see .html / .htm / .3gp / .m4v / .mp4 / .png /.jpg / .ttf either. This is a file-system limitation.

That means you can’t auto-detect those formats. Other formats (.json, .po, etc.) should be detectable easily using system.pathForFile() and the Lua File System (lfs).

  1. Arrays of assets are a good idea because you can write your own convenience functions to do things faster. Like, to use sprites in Corona you normally have to:

a. Create the imageSheet reference using graphics.newImageSheet()

b. Create the animation table

c. Create the sprite using display.newSprite()…while having (a) and (b) in scope. 

But if you had them all in their own Lua file with a table, you could write a convenience function like makeSprite() that just needs a table index and sprite name. (ex: makeSprite(1, “cool dude”)

It’s super easy to make files and keep track of assets on your own, ex:

-- assets.lua, or whatever you want to call it... local assets = {} -- assets[1] = { type="image", file="test.png" } assets[2] = { type="sheet", file="sheet1.png" } function assets.fetchName( index ) return assets[index] end return assets -- end of file -- Then you can just 'local assets = require("assets")' anywhere to add visibility to this table.

Thank you Richard. I will give it a try and see how it goes. I will have more questions probably.  :slight_smile: