Accessing files

I’m trying to access files inside a subfolder using a table, like such

local levelData = require("LevelData") local levels = {} for i,v in pairs(levelData) do local level = require("Levels/"..v.name..".lua") level:create() table.insert(level,#levels+1,level) end

levelData is this table:

local levels = { ["Level1"] = { name = "Level1", completed = 0, } } return levels

Then I have the scene template inside Levels/Level1.lua (https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/)

Is there a question here?

Are you getting errors?

What are you expecting to happen?

What is happening?

Rob

Shouldn’t

table.insert(level,#levels+1,level)

be

table.insert(levels,#levels+1,level)

@gillenation:

Without more information, as Rob requested, the only thing I can do to help is say that your require() call is set up wrong. Lua requires things in module form, not file form:

local blablabla = require("folder1.folder2.blablabla") -- Instead of local blablabla = require("folder1/folder2/blablabla.lua")

So when you require your level file, it should be

local level = require("Levels." .. v.name) -- Instead of local level = require("Levels/" .. v.name .. ".lua")

Why? Putting v.name after Levels/ and following it with .lua will result in a require path like “Levels/Level1.lua” instead of “Levels.Level1”.

  • Caleb

Is there a question here?

Are you getting errors?

What are you expecting to happen?

What is happening?

Rob

Shouldn’t

table.insert(level,#levels+1,level)

be

table.insert(levels,#levels+1,level)

@gillenation:

Without more information, as Rob requested, the only thing I can do to help is say that your require() call is set up wrong. Lua requires things in module form, not file form:

local blablabla = require("folder1.folder2.blablabla") -- Instead of local blablabla = require("folder1/folder2/blablabla.lua")

So when you require your level file, it should be

local level = require("Levels." .. v.name) -- Instead of local level = require("Levels/" .. v.name .. ".lua")

Why? Putting v.name after Levels/ and following it with .lua will result in a require path like “Levels/Level1.lua” instead of “Levels.Level1”.

  • Caleb