Not referenced LUA files do not get packed to application

I have some level data files in lua format like this:

return {  width = 15,   height = 25, etc. }

The map files are all in a subdirectory called maps  and the file itself is named like map_1.lua , map_2.lua , and so on … The string to load the level is created dynamically like.

local lvlnumber = 4 LoadLevel("maps.map\_" .. lvlnumber) function LoadLevel( filename )   require( filename ) end

Problem is: in simulator everything is working fine, but when I build the app, the map files are not in the application bundle. I suppose that files that are not referenced directly in the code do not get copied to the application bundle?

If yes, zipping them before building and unzipping on first start on device might solve the problem. 

Or maybe you know a different approach?

Thank you,

René

Your code (if that is exactly what you have) is the wrong way round. You need to define the LoadLevel before calling it and you’re not returning anything from it - simply calling require without storing it’s returned value will only load a module but not give you a reference to anything. Try this:

function LoadLevel( filename ) return require( filename ) end local lvlnumber = 4 local level = LoadLevel( "maps\map\_" .. lvlnumber )

Having said that, I’m not sure that ‘.’ is the correct subfolder denomination to use.

Thank you horacebury,

my real LoadLevel method is more elaborated and returns something. And yes, you are right, it gets loaded before. I just wanted to show, that I’m loading the map files with a require() command. Since I use require (and the map files are lua files) the dot notation is necessary, I think.

Ok, I investigated further: in the app bundle there is no laud file at all. So all lua files seems to be packed into somewhere, the main executable I guess. So my levelfiles are there, I can even load them, but I cannot count the files in the maps folder to create a level selection menu.

You will not be able to count Lua files in the binary because the get turned into one binary file during compilation. This is what Corona does.

My advice is to store the level definitions as data files and load them using the JSON API. The format is very similar to Lua code but of course you won’t be able to store functions or logic in them.

Yes, I think you are right. I cannot count the files in the in a folder in the resource directory.

Nevertheless I found a workaround: using Sublime Custom Build settings – when I press Build I run a python script before the app gets compiled to the Corona Simulator. This Python scripts analysis my maps folder and writes all necessary data into a JSON file. Thsi JSON file I can read on app start and so I know many worlds/levels I have. 

Why not changing the format of the map files: the map files are exported as LUA directly from Tiled Map editor which is quite comfortable. Until now it looks like it works well.

Thank you horacebury taking the time.

If you have .lua files at compile time you know what Lua modules you have so you don’t need to scan the directory to find out what level definitions you have. Why don’t you just keep a list of the modules in a table in your main.lua and use that? I really think you’re going about this the long way. I understand that Tiled exports as modules, but to go from there to where you are sounds like a lot of work.

Sure this would be possible, but I do not want the hard coded way. I just want to add a new map file to the maps folder, no further editing of config files. 

But maybe you are right, this is not a KISS approach (but it’s cooler :slight_smile:

Your code (if that is exactly what you have) is the wrong way round. You need to define the LoadLevel before calling it and you’re not returning anything from it - simply calling require without storing it’s returned value will only load a module but not give you a reference to anything. Try this:

function LoadLevel( filename ) return require( filename ) end local lvlnumber = 4 local level = LoadLevel( "maps\map\_" .. lvlnumber )

Having said that, I’m not sure that ‘.’ is the correct subfolder denomination to use.

Thank you horacebury,

my real LoadLevel method is more elaborated and returns something. And yes, you are right, it gets loaded before. I just wanted to show, that I’m loading the map files with a require() command. Since I use require (and the map files are lua files) the dot notation is necessary, I think.

Ok, I investigated further: in the app bundle there is no laud file at all. So all lua files seems to be packed into somewhere, the main executable I guess. So my levelfiles are there, I can even load them, but I cannot count the files in the maps folder to create a level selection menu.

You will not be able to count Lua files in the binary because the get turned into one binary file during compilation. This is what Corona does.

My advice is to store the level definitions as data files and load them using the JSON API. The format is very similar to Lua code but of course you won’t be able to store functions or logic in them.

Yes, I think you are right. I cannot count the files in the in a folder in the resource directory.

Nevertheless I found a workaround: using Sublime Custom Build settings – when I press Build I run a python script before the app gets compiled to the Corona Simulator. This Python scripts analysis my maps folder and writes all necessary data into a JSON file. Thsi JSON file I can read on app start and so I know many worlds/levels I have. 

Why not changing the format of the map files: the map files are exported as LUA directly from Tiled Map editor which is quite comfortable. Until now it looks like it works well.

Thank you horacebury taking the time.

If you have .lua files at compile time you know what Lua modules you have so you don’t need to scan the directory to find out what level definitions you have. Why don’t you just keep a list of the modules in a table in your main.lua and use that? I really think you’re going about this the long way. I understand that Tiled exports as modules, but to go from there to where you are sounds like a lot of work.

Sure this would be possible, but I do not want the hard coded way. I just want to add a new map file to the maps folder, no further editing of config files. 

But maybe you are right, this is not a KISS approach (but it’s cooler :slight_smile: