Unable to put images into folder and reference them?

Recently, I’ve learned how to use images for my game but I’ve ran into an issue. I would like to categorize my files my putting image files inside a folder called “tileTextures”, Unfortunately, this causes an error.

16:43:37.639  WARNING: C:\Users\User\Documents\Corona Projects\MMORPG\main.lua:5: Failed to find image ‘tileTextures.grass.png’

16:43:37.639  ERROR: Runtime error

Code:

–// Variables \–

local settings = require(“assets.settings”)

–local grass = display.newImageRect(settings.grass, 50, 50 )

local grass = display.newImageRect(“grass.png”, 50, 50)

grass.x = display.contentCenterX

grass.y = display.contentCenterY

– Side note, settings.grass = “tileTextures.grass.png”

so what I did next was move the grass.png out of the folder and re referenced it ass “grass.png”. So how would I placed it into a folder without it causing errors?

When accessing images in folders, you need to use a forward slashes or two backslashes, i.e.

-- One forward slash (the easiest and best method). local grass = display.newImageRect( "tileTextures/grass.png", 50, 50) -- In Lua, a backslash is used to escape characters, so if you use them, you need two of them. local grass = display.newImageRect( "tileTextures\\grass.png", 50, 50) -- don't use this

When you require code files, then you use a dot like you’ve done with your settings variable.

Edit :  I was debugging one HTML5 project that kept crashing and I found that the reason why it was crashing was specifically because it used “\” instead of “/” at some point. So, don’t use two backslashes with pointing to folders!

While I think we handle the “backslash” issue under the hood, it’s a better practice to not think in Windows terms where the folder divider is a backslash, but think for towards the Unix/Linux way of doing things with a single forward slash. macOS, iOS and tvOS are all a Unix variant under the hood. Android is running on Linux and HTML5 is likely going to think in those terms since most web servers are running on Linux.

Plus it saves you a character of typing.

While we are on the subject of file names, be careful with using upper case letters. Windows and macOS are both case in-sensitive operating systems and that’s where the simulator runs.  “tileTextures” and “tiletextures” are the same folder name. But iOS and Android are case sensitive operating systems and those are treated as different names. It’s a best practice to avoid having any upper case letters in your file names or folder names.