Persistent Table - Create a table that automatically saves itself to documents.

Persistent Tables!

Supports types:

  • bool
  • string
  • number
  • table
  • vec2
  • vec3
  • vec4
  • matrix
  • color
  • function

Add support for:

  • classes
  • mesh

Inspired by @Mark 's idea.

This will allow you to create a table that will automatically backup to your documents. When you load the table again, all your past data will still be there.

To create a table:

tbl = pTable("filename") -- passing a second param of false will disable auto backup

Using table.insert() and table.remove will bypass saving and require a manual save. Instead use:


The table variable will behave like any normal table. If autosave is enable (default) then it will backup with each change.

To manually save a table:


To clear the entire table use:


Persistent Table Code

Very nice, thanks for sharing.
Will probably need some extension to manage colors, vectors, booleans, etc…

@jmv38 I had not considered that. Back to work!

@Jmv38 - I’m not sure I can serialize the userdata. Any pointers?

You could look at those ‘save evrything to an image’ programs that we made 1 year ago. They all have way to decode userdata types via their metatble adress. I’ll look if i find any.

Here is a gist with my last version

@Jmv38 I believe they were dumping the whole project into byte string. The userdata was created when the byte string was loaded with loadstring. I don’t believe any user data was saved.

@Jmv38 That is very helpful. Thank you.

@Briarfox i dont dont seem understand what you mean here? What is the problem with coverting to ascii and reading via loadstring? Or by userdata do you mean any userdata? Each userdata sub-type will need its own convertor.

Ok, simultaneous posts!

@Jmv38 the problem is I’m unsure how to get the user data type. I’m accessing the table assignment with __newindex. The value passed is type userdata. I’ll dig into your project some more. Thanks again.

Compare metatables:

The function "is_a" extends the capabilities of the method "is_a" which is automatically defined by Codea for classes.
  a: object to be tested
  b: test

The tests work as follows.
  1. If the type of b is a string, it is taken as the name of a type to test a against.
  2. If the type of b is a table, it is assumed to be a class to test if a is an instance thereof.
  3. If the type of b is a userdata, the test is to see if a is the same type of object.
  4. If b is a function, then it is replaced by the value of that function.
local function is_a(a,b)
    if type(b) == "function" then
        b = b()
    if type(b) == "string" then
        return type(a) == b
    if type(b) == "table" then
        if type(a) == "table"
            and a.is_a
            and a:is_a(b) then
                return true
            return false
    if type(b) == "userdata" then
        if type(a) == "userdata" then
            a = getmetatable(a)
            b = getmetatable(b)
            return a == b
            return false
    return false

@Andrew_Stacey Thank you very much. Works like a charm.

@Briarfox - Ah. Cool.

@Briarfox i think that is the method i used in the gist. Actually, it pbly came from you, Andrew!

Hmm. You do it differently than I do in StackIt, and probably faster. Thanks for the code!

Updated to support:

  • bool
  • vec2
  • vec3
  • color

@Zoyt Thanks! If auto saving with each update creates a speed problem you can always choose to turn off autosave and call the method :save() when needed.

Any ideas on how to save an image type? I’m not sure if I can get the sprite string from an image.

@Briarfox - Sorry, my method is probably slower. I just turn everything into a string that can be executed with load string. So I have to process the whole table every time. So save images, I just use ing:set, but IDK how your code works. I’ll look at when I have extra time.

Not sure where I got that code from, although the exact form is mine then I don’t think that the idea of comparing metatables originated from me. I learnt about metatables from others on these forums, probably toadkick mainly. Anyway, as they say: it’s good to share!

Don’t forget vec4 and matrix types as well.

@Zoyt I’m doing the same thing. Turning the table into a string and loading it with loadstring.