How to have app to remember user's data

So for the users’ ease, I want to store the text entered the previous time the the app was used. Is there a simple way to do this? I’m new to doing this. Thanks!

You have to write the data to the device. Usually a json encoded text file. Here is some info on json: http://coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

You can use a library to help with this. A popular one is GGdata, makes it very easy: https://github.com/GlitchGames/GGData

I’m rather fond of this method myself:
 

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Rob

Thanks for the responses! So just to make sure I’m understanding this correctly, I should just copy/ paste the following set of code in the beginning of my main.lua to load the JSON library and to tell it to use the documents directory (I also noticed it loaded the local json twice, I’m assuming that it is necessary?). Then after this, I make a table with what I want to save by using SaveTable, and now I can recall things like “box1.text” from the saved table? 

Thanks

[lua]

– load the JSON library.

local json = require(“json”)

– Function to save a table.  Since game settings need to be saved from session to session, we will

– use the Documents Directory

local json = require(“json”)

function saveTable(t, filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    if file then

        local contents = json.encode(t)

        file:write( contents )

        io.close( file )

        return true

    else

        return false

    end

end

function loadTable(filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local contents = “”

    local myTable = {}

    local file = io.open( path, “r” )

    if file then

         – read all contents of file into a string

         local contents = file:read( “*a” )

         myTable = json.decode(contents);

         io.close( file )

         return myTable 

    end

    return nil

end

[/lua]

If you are assigning json to a local variable (reccomended) then you only need to require it once per lua file.

Requiring it twice really has no effect.

To elaborate on this, the second time you require the same file, it just loads it from the loaded packages table.

So Rob is right, but there is still no logical reason to require it twice in the same file i guess :slight_smile:

You have to write the data to the device. Usually a json encoded text file. Here is some info on json: http://coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

You can use a library to help with this. A popular one is GGdata, makes it very easy: https://github.com/GlitchGames/GGData

I’m rather fond of this method myself:
 

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Rob

Thanks for the responses! So just to make sure I’m understanding this correctly, I should just copy/ paste the following set of code in the beginning of my main.lua to load the JSON library and to tell it to use the documents directory (I also noticed it loaded the local json twice, I’m assuming that it is necessary?). Then after this, I make a table with what I want to save by using SaveTable, and now I can recall things like “box1.text” from the saved table? 

Thanks

[lua]

– load the JSON library.

local json = require(“json”)

– Function to save a table.  Since game settings need to be saved from session to session, we will

– use the Documents Directory

local json = require(“json”)

function saveTable(t, filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    if file then

        local contents = json.encode(t)

        file:write( contents )

        io.close( file )

        return true

    else

        return false

    end

end

function loadTable(filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local contents = “”

    local myTable = {}

    local file = io.open( path, “r” )

    if file then

         – read all contents of file into a string

         local contents = file:read( “*a” )

         myTable = json.decode(contents);

         io.close( file )

         return myTable 

    end

    return nil

end

[/lua]

If you are assigning json to a local variable (reccomended) then you only need to require it once per lua file.

Requiring it twice really has no effect.

To elaborate on this, the second time you require the same file, it just loads it from the loaded packages table.

So Rob is right, but there is still no logical reason to require it twice in the same file i guess :slight_smile: