0
\$\begingroup\$

If i use this code

-- some lua file
-- some code
local IMAGE_FILES = {"file1";"file2";"file3";"file4}
-- some code
function loadRes()
    -- some code
    -- load all images:
    for i,v in ipairs(IMAGE_FILES) do
        img[v] = love.graphics.newImage("data/img/"..v..".png")
    end
    -- some code
end

i have 2 problems!

  1. If i have/want to rename a file, i have to rename it in the IMAGE_FILES table,too. (If i forget to rename it in one place, either in the .lua-file or in the folder, i run into problems)
  2. If i need/want to add another file into the folder, i have to add its name to the variable,too.

For this reasons i want to load data(for example images, music, etc) out of a folder without having to let lua/love2d know the file names beforehand. In other words, is there a way to say to lua/love2d: load all files contained in this "data/img/" folder.

Secondly: Can you get the file type(file ending, like .png) with the solution for my first question?

I'm relatively new to lua and love2d, so i'd really appreciate any help. =)

\$\endgroup\$

closed as off-topic by Anko, Seth Battin, Tom 'Blue' Piddock, congusbongus, Engineer Oct 17 '15 at 10:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, Seth Battin, Tom 'Blue' Piddock, congusbongus, Engineer
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Can this on Stack Overflow be of any help? \$\endgroup\$ – Alexandre Vaillancourt Sep 25 '15 at 12:47
  • \$\begingroup\$ Yes, this did help indeed! I created minimalistic test code and will post it as an answer. Sadly i can't vote up for your comment (or at least don't know how that's possible). \$\endgroup\$ – frosty Sep 25 '15 at 16:06
  • \$\begingroup\$ No worries; if the question has been answered somewhere else on stack overflow, there is no need to re-post the answer. And you could upvote the comment (but I don't know if you have the privilege with your current reputation). \$\endgroup\$ – Alexandre Vaillancourt Sep 25 '15 at 16:09
  • \$\begingroup\$ @frosty How about just "minimal test code"? \$\endgroup\$ – Engineer Oct 17 '15 at 10:18
1
\$\begingroup\$

Just in case someone has a similar problem as i had:

First locate the directory in which you want to go (using windows, don't forget to escape the backslash signs). e.g.(windows) dir = ".\\data\\img\\"

Then read and save the filenames in a table (for this you can look at the link from Alexandre Vaillancourt in the question comment section or here:)

local i = 1
love.graphics.setBackgroundColor(255,255,255)
p = io.popen('dir "'..dir..'" /b')  --Open directory look for files, save data in p. (with option "/b" everything contained in the given directory is listed with simple format)
for file in p:lines() do                    --Loop through all files
    data[i] = tostring(file)
    i = i + 1
end

Last but not least, load whatever you want with the strings that are now contained in the data table. Attention: if you want to load images make sure that either the directory only contains images or don't forget to sort out strings that don't contain image data names (folder names, music data names etc). After you have eliminated possible mistakes you can use the string at position x to create an image out of it. For example like this:

img[x] = love.graphics.newImage(data[x])
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.