So for loading and working with images/sprites/etc I made a little module called "ghelp" (stands for "graphics help" of course :]) and one of the functions is a preload function that creates a table of images by first getting the names of all the files in a specific folder that contains the images (in my case "img/", puts them in a table called "files", then a new table called "images" is created and for each entry in the "files" table newImage is called to import the image for use in the game, with the name of the file used as the key and the extension removed to make it easier to write (so I can use, for example, images.foo instead of images["foo.png"]). Here is the code for that:
Code: Select all
preloadimgs = function()
local dir = "img/"
local files = love.filesystem.getDirectoryItems(dir)
local images = {}
local k = ""
for i = 1, #files, 1 do
k = files[i]
images[k:gsub("%p%a%a%a", "")] = love.graphics.newImage(dir..files[i])
end
return images
end
My question is is this the correct way to do this kind of thing or are there better ways? Also would this way work for other kinds of assets or just images?