[Resolved] Accessing images in subdirectory from .lua files in different subdirectory

I am using subdirectories to organize most of my files. I only keep main.lua and a few other files in the root of my project.

This is working well for me, except when I need to access image files from a lua file and that lua file is in a subdirectory.

See example below for clarification:

Path Layout:

aProject\  
|  
+---main.lua  
|  
+---images\  
| |  
| +---anImage.png  
|  
+---libs\  
 |  
 +---aLib.lua  

Contents of aLib.lua

module(..., package.seeall)  
  
local imgPath = "images/anImage.png"  
  
function newImage()  
 local retImg = display.newImageRect( imgPath, 100, 100 ) -- THIS FAILS  
 return retImg  
end  

Code in main.lua:

local aLib = require("libs.aLib"  
.  
.  
  
local imgPath = "images/anImage.png"  
  
local imageRect1 = display.newImageRect( imgPath, 100, 100 ) -- THIS WORKS  
  
local imageRect2 = aLib.newImage() -- THIS FAILS  
  

Note: The call to aLib.newImage() works, but the internal call to newImageRect() fails.

My question is, “Is there some secret sauce for code in lua files, which are in subdirectories, to access images/sounds in other subdirectories?” I’d love to see a code example to this.

Note: I’ve been searching around the forums and have thus far not found a solution, but if a solution does exist please forgive my not (yet) finding it.

Cheers,
Ed [import]uid: 110228 topic_id: 25946 reply_id: 325946[/import]

Sigh… I found the issue. I’m not sure what it is, but filing a question in the forums sometimes helps give one the extra debug kick.

My problem wasn’t with paths at all. The problem was with the optional ‘group’ argument. In my actual code I called display.newImageRect() and passed a group in as the first argument.

I just noticed that the group was invalid. Once I fixed that, code started working.

So, apologies to all. However, if you read this because you’re looking to answer the question: ‘How to use subdirectories?’, then the above code should outline what you need to know.

Cheers again,

Ed [import]uid: 110228 topic_id: 25946 reply_id: 104997[/import]

Don’t worry Ed, I’ve done this (post and then basically answer my own question) several times. As long as you share the solution it’s all good. :slight_smile: [import]uid: 41884 topic_id: 25946 reply_id: 104998[/import]

Thanks for the thorough follow up Ed, glad you got your problem figured out :slight_smile:

Marking as resolved. [import]uid: 52491 topic_id: 25946 reply_id: 105025[/import]