Save/Load a Table

Here’s another link I found on saving/loading tables using a similar method, but contains more code:
http://lua-users.org/wiki/SaveTableToFile

Your method worked perfectly

Thanks. I recently added booleans and am working on nils. Let me know if you need those.

@Zoyt No thanks. Just needed to be able to save and load tables, I wrote a program that lets me design levels and then I save them and load them in other games

Edit: Double post

If I’m reading this correctly, you’re saving variables to storage by basically creating a Lua statement…then reading the data back in by storing the saved Lua statement.

What happens when someone replaces the data with code that does something malicious, like use your computer to send spam or format a hard drive?

Fortunately, there aren’t enough Codea users to make it worthwhile to try to break into our data and use us for spam.

@tomxp411 - It’s easy (I’ve tried it), not to mention to can access the source code of every app itself, but as@Ignatz said, Codea doesn’t have that many users that want to hack the apps. Simeon did mention earlier that he’d look into restricting users from accessing our code in our apps.

Yea, when you publish an app, any Jailbroken iPad (or someone using iExplorer) can modify the code easily. So if, for example, your game gets relatively popular, lots of user might simply add a line of code to save the highscore as very high

I’m just pointing out that it’s a bad - a VERY bad programming practice in the real world; we shouldn’t be promoting this as a good practice at all, especially when serializing (using something like JSON or XML) is not any more difficult. Yes, deserializing is a little bit more complex, but that’s the tradeoff with Lua: since code and data are basically the same thing, you have to work harder to write secure code.

Sorry if I seem overbearing. I write police software for a living. =)

@tomxp411 - Nice. I agree. I do hope for this feature soon.

I’ve got to do this at some point for my own project anyway… So I will try to throw something together this week.

I started looking at JSON as a serialization format, and found a bunch of implementations in Lua:

http://lua-users.org/wiki/JsonModules

I’m still going to roll something, just for learning and as a sample for other users. When it’s ready, I’ll post it in the code sharing section.

I use JSON, though it is bloated, simply because all this stuff that you go through to fix and optimize a serializer is a pain, and I want to write other things.

That said, I (and oters) wrote serializers that persisted to am image and wrote that to wither Dropbox: or Documents:, so that’s another option.

@tomxp411: In Lua 5.1 there are ways to do sandboxed loading, primarily through use of the setfenv() function, which allows you to specify the global environment that a function has access to (yeah, in Lua 5.1, every function actually has it’s own “global” environment…it just happens to be _G by default).

In cmodule’s load() function I use this facility to load files that are intended to be data only, exposing only what is necessary for the data files to compile by allowing you to pass in an optional “environment” table; when the data file is compiled it’s only access to the outside world is what is defined in this table (for example, in my UI kit prototype, I load view templates that are Lua tables, and provide an environment that only has the Codea data types available, like color(), vec2(), etc, for setting the view properties).

I think JSON is great, if you’re writing javascript, or writing server comm code that needs to send serialized data or parse JSON responses…but for game data? It should be fine to use Lua. Since it’s already a data description language (and a very good and fast one at that) I wouldn’t preclude it’s use if it’s possible to use it without creating a security concern. I believe in most cases it is. Anyway, even if you did release an app on the app store, and someone hacked the bundle and changed data, if you are properly sandboxing your input the worst they could really do is hose their own app/device I think. Codea itself is already sandboxed so there’s really only so much someone with malicious intent could get away with.

Thanks for the info, @toadkick. I’m still new to Lua in general, so I’ve got a lot to learn.

Speaking of serialization libraries:

Here’s something I whipped up. It’s not JSON. It’s not XML. It’s a psuedo-INI file format that I use with my projects on various platforms. It’s Human readable, and the actual format is very simple to understand.

http://gist.github.com/compiledtom/6443652

you only need to include the Serialization tab in your project. The main tab is just a test rig, and the Output tab just shows a sample of what the data looks like.

The test rig actually creates an object, serializes it, deserializes it to a new object, then serializes it again. It writes both serializations out to the Output tab so that you can see a full round trip.

It’s worth noting that I haven’t gone to the trouble to instantiate classes or userdata objects (like vec2.) If you look at lines 4 and 26 of output.lua, you will see that I’ve got special key names for objects.

If you want to deserialize in to objects, you’ll need to instantiate your objects, then copy the fields from the class: fields over to the object in question. Using a for loop, that should be fairly straightforward.

If you want to deserialize in to tables, or if your objects don’t have child objects, there’s no extra work required.

    -- save table data to a string
    data=getString(myTable)
    -- then write the string to storage however you like

    -- loading a table requires 2 steps
    data=[load from storage]
    newTable={} 
    loadTable(newTable,data)   

    -- loading an object is easy, as long as there's only 1 level of data
    data=[load from storage]
    newObject=MyClass()
    loadTable(newObject,data)

Have fun!

@Zoyt, I am getting an error when using this with a table of images. Is that normal?

@JakAttak - Yes. You need to find a way to encode the images in a string. So read the image and use image:set() to encode it intor easble code.

Thanks.

@tomxp411, your serializer works great, thanks for it! One thing: in making a string from an indexed table, it doesn’t separate the values with commas, so it can’t, for instance, write usable Lua tables to a Codea tab.