Saving all data

Hello again! This has worried me for awhile, I’ve done quite a few attempts at this, but I can’t seem to save all the data in a project “mostly for tables” For example, lets say I have an inventory, randomly generated terrain (a bit of tables and physics,) and a characters health. How would I save all this?

You can only save strings and numbers, so you need to “serialise” or “flatten” your tables into strings. I explained how I did this for an undo function in a game here (look for the ‘copying tables’ heading)
http://coolcodea.wordpress.com/2013/03/31/19-lessons-from-a-simple-board-game-part-2/

Other people have also posted functions that flatten tables into strings. Search on this forum for serialise or serialize and you’ll find plenty of ideas…

See my code here: http://twolivesleft.com/Codea/Talk/discussion/3050/save-load-a-table/p1
If you have a little understanding of Codea and how table serialization works, it’s SUPER easy to extend to include things like vec2s, and even physics bodies.
Edit: If you want to save the state of your entire app, there’s a variable called “_G” which holds all your variables in a giant table, so you could attempt to extend my code to support all data types, and simply save the _G variable.

@Ignatz awesome! But it looks like that it only “saves” while in-app, and when you get out of the app, progress is lost, correct?

@Prynok, that’s right. That would need fixing.

It’ snot hard to write an Obj-C function in the he runtime to call a function when the app is left.

No, I think he means that I dont save progress if you leave the game and come back later, which of course is easy to do.

Does the Codea runtime get notified when the user backgrounds Codea? That seems like kind of an essential feature.

@tomxp411 - I agree, and I’ve requested it before. If you look at TLL’s games, they don’t pause when you come back from the background (I may be misremembering it), so it may not be important to them. @Simeon - How Garda would it be for you to add this to Codea?
Thanks!

Sorry @Zoyt, still learning with codea, how could I extend your code to add vec2’s and physic bodies?

@Zoyt If I recall correctly, code doesn’t actually execute in the background on iOS, except for specific service threads. Instead, the app is in a suspended state, where its data is still loaded, and it can fire back up quickly.

Still, an app CAN terminate in the background. On other handheld OS’s, the practice has always been to save your state when the user switches to another app… maybe Codea does some magic in the background for us.

Correct me if I’m wrong, but couldn’t I add somthing like

draw()
for i=1,#table do
if table[i] == physics body then
tostring table[i]
end
end

for Zoyts table saver?

Wait… I don’t think that would work upon looking at it again, back to square one.