Hi everyone! I made an utility function for my game to load all of items(which are lua objects/tables) which are located inside {project folder}/Objects/Items.
Here is the code:
local lfs = require "lfs"
local itemsPath = system.pathForFile("Objects/Items", system.ResourceDirectory)
print("This is items path: " .. itemsPath)
inventoryFill = {}
function inventoryFill:fill()
for file in lfs.dir(itemsPath) do
local _, i = string.find(file, "%.")
local moduleName = string.sub(file, 1, i - 1)
if moduleName ~= "" and INVENTORY[moduleName] == nil then
INVENTORY[moduleName] = require("Objects.Items." .. moduleName)
end
for k,v in pairs(INVENTORY) do
print(k .. ' ' .. tostring(v))
end
end
end
return inventoryFill
It works no problem in emulator but when i run it on my android device i get an error:
InventoryFill.lua:5: attempt to concatenate local ‘itemsPath’ (a nil value)
I know that there are some restrictions when it comes working with files on android but what am i missing?
system.pathForFile returns path for a file not a directory unless used with nil
this code can iterate and delete through files inside folders
local lfs = require "lfs";
local doc_dir2 = system.DocumentsDirectory;
local doc_path = system.pathForFile("", doc_dir2);
local resultOK, errorMsg;
for file in lfs.dir(doc_path) do
local theFile = system.pathForFile(file, doc_dir2);
if file=="name of your file here" then
resultOK, errorMsg = os.remove(theFile);
end
end
In Android, to use system.ResourceDirectory with system.pathForFile you’ll need to know the file name to fetch. In which case, you’re better off generating the file list beforehand and avoid using lfs post build if you don’t want to add the “require” manually.
Anyway if anybody has similar problems i did it like so:
In the main.lua file check if the game is run on simulator
if system.getInfo("environment") == "simulator" then
If it is then generate json file with all the files inside the folder
local lfs = require "lfs"
local items = {}
local j = 0
local itemsPath = system.pathForFile("Objects/Items",
system.ResourceDirectory)
for file in lfs.dir(itemsPath) do
local _, i = string.find(file, "%.")
local moduleName = string.sub(file, 1, i - 1)
if moduleName ~= "" and moduleName ~= "." and moduleName ~= ".." then
items[moduleName] = moduleName
end
j = j + 1
end
local items_json = json.encode(items)
local res_path = system.pathForFile("")
local items_file, err = io.open(res_path .. "/items.json", "w")
if not items_file then
print("error with opening file: " .. err)
else
items_file:write(items_json)
end
3.Now load everything you need from that json, in my case i wanted to load all the modules
local items_path = system.pathForFile("items.json")
local items_file, err = io.open(items_path, "r")
if not items_file then
print("Error with opening file: " .. err)
return
end
local items_json = items_file:read()
local items = json.decode(items_json)
for _,item in pairs(items) do
if item ~= nil and INVENTORY[item] == nil then
INVENTORY[item] = require("Objects.Items." .. item)
end
end
Hope this helps someone, if you have questions DM me