Best way to copy files to documentsDirectory?

Long story short, I have a bunch of *.txt levels for my game in the app resource folder. Since there doesn’t seem to be an easy way of loading them on the go from there, I would like to copy them all to DocumentsDirectory on the first run.

What’s the best way to achieve this? 

What do you mean by “loading them on the go”? I have level data in my resource directory which loads just fine.

That’s interesting. How do you store/load it? I have my levels in JSON in separate txt files. I use the “loadsave” plugin to load these files (https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK). 

When levels are stored in the sandbox they do load fine. When they are in the project folder, however, the app doesn’t detect them (the loaded file is nil). Specifying the path to system.ResourceDirectory doesn’t help either.  

I am laboring under the assumption that, since you are looking to store level data in the resource directory, you have static levels which you won’t be updating dynamically.

I think you’re making this a bit more complicated than it needs to be, insomuch as, you don’t necessarily need to code and store the level data in a different way than you are developing the other parts of your game. If you simply create another module with the name “levels.lua”, and put all of your level data here, then load it like you would any other module, I believe you would accomplish the functionality you are trying to achieve. 

The important part to remember is that JSON data is just data in tables; you could just as easily have Lua tables holding all of your data and read it in as normal. If you take a look at Roland Yonaba’s Jumper lib example, you’ll see a very simple description of “level” data in a Lua table.

What do you mean by “loading them on the go”? I have level data in my resource directory which loads just fine.

That’s interesting. How do you store/load it? I have my levels in JSON in separate txt files. I use the “loadsave” plugin to load these files (https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK). 

When levels are stored in the sandbox they do load fine. When they are in the project folder, however, the app doesn’t detect them (the loaded file is nil). Specifying the path to system.ResourceDirectory doesn’t help either.  

I am laboring under the assumption that, since you are looking to store level data in the resource directory, you have static levels which you won’t be updating dynamically.

I think you’re making this a bit more complicated than it needs to be, insomuch as, you don’t necessarily need to code and store the level data in a different way than you are developing the other parts of your game. If you simply create another module with the name “levels.lua”, and put all of your level data here, then load it like you would any other module, I believe you would accomplish the functionality you are trying to achieve. 

The important part to remember is that JSON data is just data in tables; you could just as easily have Lua tables holding all of your data and read it in as normal. If you take a look at Roland Yonaba’s Jumper lib example, you’ll see a very simple description of “level” data in a Lua table.

Tell me, is there a way to put the finished JSON settings file in the right folder at the installation phase of the apk file?

An app can’t do anything when installed. It isn’t running.

The right way to do this is to:

  1. Every time the app runs, check to see if the file you need is or is not in the documents folder.
  2. If it is, load it.
  3. If it is not, generate it (or copy a template from a folder under the resource directory area). The load it.

Question. @neo_freeman, you used the word ‘finished’ which implies it won’t every be modified. Is that what you meant?

Because, if you’re not going to write changes to the file, there is no reason you can’t just keep in the resource area.

Thanks for the answer. I did just that, I just thought maybe apk installation is like a Windows installer and can put files in the specified folders. And about the finished file, it’s all my weak English. I meant the file with the settings I already have, I wanted to avoid creating it in the code, and just put it in the right folder during installation.