How can I load a file from the documents directory during initialization?

I have a file called "levels.lua" which holds all my level data. I load the data by calling require("levels") at the top of main.lua

I also have a file called "customlevels.lua" which is where users are allowed to save their own custom levels. In the simulator, I have customlevels.lua sitting in my root directory, and I require it just like the main levels file. This works great.

Unfortunately, now that I'm trying to run on a device, I need to have customlevels.lua output to the documents directory. I've got it doing that now, and I can see that the file is being created properly in the sandbox.

But I can't call require on a file in the documents directory (Can I?) so I need to do something like the following:

(in main.lua)

local path = system.pathForFile("customlevels.lua",system.DocumentsDirectory)
local file = io.open(path, "r")
if(file) then
blah...

Has anyone tried to do this? Is there something obvious I'm missing?

don't require. load it. if missing (first time) then means that its not there so use a require

pseudo code follows

if customLevelLua does not exist
create (customLevelLua) --> or require it. -> means sets everthing to default.
else
io.read customelevelLua from documents directory.

.c

Sure, I can use io.read, but then how do I turn the string into useful data? loadstring() is disabled in corona, as is dofile().

My understanding is that "loading" is allowed only during initialization of the app, and only require does it, to enforce that standard.

@Big C,
just one quick question, since we have the sandboxed LUA environment, how do we load a module, we only have require as the way to *load* the module.

@simon, the approach you are thinking of is quite novel and good, but unfortunately, here's a snippet from the CoronaSDK API here

>>>>>
2) When running on device, things work a bit differently. Beforehand, i.e. at build time, Corona searches for Lua files that can serve as Lua libraries in order to compile and package them. These Lua files MUST be in the same directory as your main.lua file in order to be packaged for you during device builds, otherwise require() will fail at runtime. Please also see the note below about file name case sensitivity.
>>>>>>

And you can see from the pseudocode that Carlos has posted, you will have to load the data using file:io functions, but if LUA was not sandboxed, you would have had loadstring or dofile options.

you cannot require files from a directory other than the one where you have the main.lua, which is the resources directory.

what you need is to save the data in a table form and load it from the file when you need it again.

I had a *PropertyBox* class, which I have discontinued now, there would be similar ones that you can find. There have been many other libraries that do the save/load thing.

cheers,

?:)

I feel like there might be a semantic problem preventing me from understanding what I should do here. Currently, my customlevels file looks like this

1
2
3
4
5
6
7
8
9
level[1] = {
     name = "levelname",
     height = 20,
     width = 20,
     fancy = false,
     blocks = {20,25,32,47,48,49,},
     }
level[2] = {
...

Use JSON

I agree, use JSON.

Awesome. Problem solved!

Now, I just need to figure out how to best allow users to share their customlevels data between devices... maybe OpenFeint can help me here?

views:1688 update:2011/10/15 21:01:16
corona forums © 2003-2011