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:
tbl.insert(value)
tbl.remove(index)
The table variable will behave like any normal table. If autosave is enable (default) then it will backup with each change.
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.
@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.
@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.
@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.
--[[
The function "is_a" extends the capabilities of the method "is_a" which is automatically defined by Codea for classes.
Parameters:
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()
end
if type(b) == "string" then
return type(a) == b
end
if type(b) == "table" then
if type(a) == "table"
and a.is_a
and a:is_a(b) then
return true
else
return false
end
end
if type(b) == "userdata" then
if type(a) == "userdata" then
a = getmetatable(a)
b = getmetatable(b)
return a == b
else
return false
end
end
return false
end
@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.
Thanks!
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!