16.1 Why do we need this?
In our last tutorial we developed a simple level editor. This is not much use unless you can save and then load the level data produced. Codea provides a function to saveProjectData and one to saveGlobalData(). Since we would like to save our level data from dGenerator and then read it into our game program, saveGlobalData() is the function we need, as saveProjectData() saves data which is only accessible by the program which saved it.
Unfortunately it is not as simple as saying saveGlobalData("Level1", ourTableData) as you can only use this function to save a number, string or nil. Consequently, we need to convert our table to a string in order to save it. To load the data we need to reverse this process and reconstitute our table from the saved string.
We used a similar technique in MineSweeper to save and load high scores, albeit we were only concatenating three strings rather than converting a table, but the theory is the same.
16.2 Codea Data Persistence Under the Hood
There are four primary ways to save and load data in Codea (excluding image data, which we wont discuss in this tutorial). Namely:
- clearLocalData(), saveLocalData() and readLocalData() - used for storing information which is unique to the program and the device. Other programs and devices running a copy of the same program don't have access to this data. It is useful for things like high scores and perhaps user preferences. For each project, a key of the form Project Name_DATA is used to store local data in the com.twolivesleft.Codify.plist which may be found in the Library -> Preferences sub folder (i.e. the same location as global data).
- clearProjectData(), saveProjectData and readProjectData() - gets bundled with your program and is not device specific. It can be used for level data, maps, and other program specific data which doesn't change with different users. Project Data is stored in the Data pList in the Documents directory of your App.
- saveProjectInfo() and readProjectInfo - has similar access to Project Data (bundled with program and is not device specific) but is used for saving metadata about your App. There are two data keys which are natively supported by Codea, "Description" and "Author". The data associated with the "Description" key will show in the Codea Project Browser when this App is selected. If you are planning on using the Codea runtime, be aware that this uses the "Version" key and expects a string. If you use a number like we do then you will get an error and the runtime wont compile. The fix for this is described in our earlier tutorial on submitting an application to App store. If you whip out iExplorer you will see that all of the project information is stored in Info.plist which is part of your application bundle. This pList also includes a key called "Buffer Order" which tells the runtime which order your tabs should be loaded in.
- saveGlobalData() and readGlobalData() - is data available between all projects on a device. The subfolder Preferences of folder Library contains files including com.twolivesleft.Codify.plist which contains any global data that you have saved (under the key "CODEA_GLOBAL_DATA_STORE"). If you have used the sprite generator Spritely which comes with Codea, you will see that it stores some data in the global data store.
16.3 A Solution for Single Dimension Arrays & Tables
The simplest version of a Lua table is an array so lets work out a solution for that first. It is an easy matter to concatenate the elements of a table together to form a string, as long as the elements of that table are something that can be converted to a string (i.e. this wont work on a table of tables). It is easy because Lua provides a function to do this very thing. Even better you can specify a delimiter to be placed between each element in the resulting string.
The function looks like table.concat(yourArray, "delimiter") so:
table.concat({ "one", "two", "three", "four", "five" }, ",") will produce a string "one,two,three,four,five"
Assuming we pass in the array to our saveData() function, it will look like:
function saveData(mArray)
local saveString = table.concat(mArray, ",")
saveGlobalData("dataKey", saveString)
end
Obviously you could just as easily use a global array, in which case you wont need to pass the array to the function. Loading the data is a bit more work, but as with a lot of common problems, it is one which has already been solved (credit to http://richard.warburton.it for writing the explode function which does all the heavy lifting).
function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
Using the above function the loadData() function is trivial, and will return your original array:
function loadData()
local stringArray = readGlobalData("dataKey")
return explode(",", stringArray)
end
16.4 A Solution for Multidimension Arrays & Tables
Once you get past the simple array or non-nested table things get complicated quickly. As you can stick just about anything in a table (e.g. another table, closures / functions, Userdata and Metatables) a comprehensive conversion function would need to take all of the possible cases into account. These table serialisation libraries exist but they are overkill for our purposes.
Instead we will use the charmingly named Pickle and UnPickle functions written by Steve Dekorte.
To be continued...
Another option would be to convert the table into a JSON string and save that out - it would work just as well for a multi-dimensional table and you'd not have to worry about the size or shape of the individual entries.
ReplyDeleteLoading it back in is just as simple, load the string, decode the JSON data and bang your done.
I have a simple well commented JSON encoder / decoder that ships in at around 500 lines of lua - hit me up at techdojo [at] gmail [dot] com if you want me to send it to you. :)
Keep up the posts - codea ROCKS! :)
Hi Jon,
DeleteThanks for the comment.
I don't know much (or anything really) about JSON but would love to have a look at your code.
It turns out that I can't use Pickle or even Data Dumper because my 2D array contains a class which incorporates userdata. My current approach is to write an export/import function in the class to get out the data I need since I don't really have to save the class structure and its associated functions.
I agree Codea is fantastic. Email is on its way!
Cheers,
David
Hi David! Thanks for this awesome tutorial!
ReplyDeleteContinue to write part two!
I wait Solution for Multidimensional Arrays & Tables. :)
Thanks Georgian - it turns out that saving complicated tables is actually very tricky (see my comment above). Anyway I have solved the problem by by getting the class to export the data into the 2D array.
DeleteNext tute will be out this weekend.
Stay tuned...