How to access resources in different folders?

I want to organize my resources in different folders in my project folder. However when I move them into different folders; corona can not find them. How do i organize them?

If you want to put a Lua file in a subfolder, you require it like this:

require( "subfolderName.luaFileName" ) -- No .lua extension... notice use of dots 

If you want to put a resource (PNG, WAV, etc.) in a subfolder, you access it like this:

display.newImageRect( "subFolderName/fileName.png", 10, 10 )

All assets used in your project MUST be in the project folder structure.
 
This is WILL NOT WORK:

require( "..projectFolder.subfolderName.luaFileName" ) display.newImageRect( "../projectFolder/subFolderName/fileName.png", 10, 10 )

Semi-Concrete Example
 

aProject | build.settings | config.lua | main.lua | +---images | circle.png | \---scripts common.lua

Now in main.lua we could do this
 

local common = require "scripts.common" local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

Thanks. I will try this.

If there is multiple lua files in a folder that you require; how do you require that.

You require script files individually.

Semi-Concrete Example #2
 

aProject |   build.settings |   config.lua |   main.lua | +---images |       circle.png | \---scripts         common.lua         myModule.lua

Now in main.lua we could do this
 

local common = require "scripts.common"  local bob = require "scripts.myModule" -- bob stores a reference to the module   local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

In  myModule.lua you could do this:

local common = require "scripts.common" local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

Notice that even though common.lua and myModule.lua are in the same folder, they MUST use the entire folder structure when requiring LUA files and also when accessing resources.

This is really all about Lua and Lua folder structures.

So, I suggest you read the PIL if you have not yet done so:

https://www.lua.org/pil/ - Currently at 4th edition, but for much of what you need, the 1st edition is fine (and free to read online):

https://www.lua.org/pil/contents.html

I would read:

  • Ch 1
  • Ch 2
  • Ch 3
  • Ch 4
  • Ch 5
  • Ch 11
  • Ch 12
  • Ch 18
  • Ch 19
  • Ch 21

If you want to put a Lua file in a subfolder, you require it like this:

require( "subfolderName.luaFileName" ) -- No .lua extension... notice use of dots 

If you want to put a resource (PNG, WAV, etc.) in a subfolder, you access it like this:

display.newImageRect( "subFolderName/fileName.png", 10, 10 )

All assets used in your project MUST be in the project folder structure.
 
This is WILL NOT WORK:

require( "..projectFolder.subfolderName.luaFileName" ) display.newImageRect( "../projectFolder/subFolderName/fileName.png", 10, 10 )

Semi-Concrete Example
 

aProject | build.settings | config.lua | main.lua | +---images | circle.png | \---scripts common.lua

Now in main.lua we could do this
 

local common = require "scripts.common" local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

Thanks. I will try this.

If there is multiple lua files in a folder that you require; how do you require that.

You require script files individually.

Semi-Concrete Example #2
 

aProject |   build.settings |   config.lua |   main.lua | +---images |       circle.png | \---scripts         common.lua         myModule.lua

Now in main.lua we could do this
 

local common = require "scripts.common"  local bob = require "scripts.myModule" -- bob stores a reference to the module   local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

In  myModule.lua you could do this:

local common = require "scripts.common" local circleImg = display.newImageRect( "images/circle.png", 100, 100 ) circleImg.x = display.contentCenterX circleImg.y = display.contentCenterY

Notice that even though common.lua and myModule.lua are in the same folder, they MUST use the entire folder structure when requiring LUA files and also when accessing resources.

This is really all about Lua and Lua folder structures.

So, I suggest you read the PIL if you have not yet done so:

https://www.lua.org/pil/ - Currently at 4th edition, but for much of what you need, the 1st edition is fine (and free to read online):

https://www.lua.org/pil/contents.html

I would read:

  • Ch 1
  • Ch 2
  • Ch 3
  • Ch 4
  • Ch 5
  • Ch 11
  • Ch 12
  • Ch 18
  • Ch 19
  • Ch 21