Rob Miracle's json saving method

Hi,

I wanted to use Rob Miracle’s json saving method, but I have a question. Is there a way to check if a json file exists? Because I want to make a high score, but I need to create it. If I create it and set it to 0 but there already is a high score, I want to import that high score. So I need to be able to check if a json file exists.

Thanks!

Hi @cozymonster,

Have you considered using LFS (Lua File System) to scan the directory?

http://docs.coronalabs.com/api/library/lfs/index.html

Brent

P.S. - I also got your name change done. :slight_smile:

The loadTable function checks to see if it exists.  If it does, it opens it, reads it, parses it and returns the table.  If the file does not exist, it returns nil.  In your code you can do something like:

local mySettings = loadTable(“somefile.json”)

if mySettings == nil then

   – create the table

   mySettings = {}

   – populate it with values as necessary

   saveTable(mySettings, “somefile.json”)

end

Changing the variables to your needs of course.

Hi, I got the high score to work using this:

gameSettings = {} gameSettings = loadTable("gameSettings.json") if gameSettings.highScore == nil then     gameSettings.highScore = 0 end  

It works fine on the simulator. But when I built the .apk and sent it to my device, it didn’t work, like it usually does. All I got was a black screen and this in the adb: attempt to index global ‘gameSettings’ (a nil value).

Does anyone know what this would mean?

In the first line you make gameSettings a table.

The next line completely overwrites it regardless.  It will either be nil or the table returned from the function.

If it returns nil because the gameSettings.json file doesn’t exist yet, then the variable gameSettings will be nil.

if gameSettings.highscore == nil then  returns an error because gameSettings is nil and doesn’t have a highscore member.  Try this instead:

if gameSettings == nil or (gameSettings and gameSettings.highscore == nil) then

     gameSettings.highscore = 0

end

or something similar.

Hi, 

I changed my code around a bit so it works.

gameSettings = loadTable("gameSettings.json") if gameSettings == nil then gameSettings = {} gameSettings.highScore = 0 end

Hi @cozymonster,

Have you considered using LFS (Lua File System) to scan the directory?

http://docs.coronalabs.com/api/library/lfs/index.html

Brent

P.S. - I also got your name change done. :slight_smile:

The loadTable function checks to see if it exists.  If it does, it opens it, reads it, parses it and returns the table.  If the file does not exist, it returns nil.  In your code you can do something like:

local mySettings = loadTable(“somefile.json”)

if mySettings == nil then

   – create the table

   mySettings = {}

   – populate it with values as necessary

   saveTable(mySettings, “somefile.json”)

end

Changing the variables to your needs of course.

Hi, I got the high score to work using this:

gameSettings = {} gameSettings = loadTable("gameSettings.json") if gameSettings.highScore == nil then     gameSettings.highScore = 0 end  

It works fine on the simulator. But when I built the .apk and sent it to my device, it didn’t work, like it usually does. All I got was a black screen and this in the adb: attempt to index global ‘gameSettings’ (a nil value).

Does anyone know what this would mean?

In the first line you make gameSettings a table.

The next line completely overwrites it regardless.  It will either be nil or the table returned from the function.

If it returns nil because the gameSettings.json file doesn’t exist yet, then the variable gameSettings will be nil.

if gameSettings.highscore == nil then  returns an error because gameSettings is nil and doesn’t have a highscore member.  Try this instead:

if gameSettings == nil or (gameSettings and gameSettings.highscore == nil) then

     gameSettings.highscore = 0

end

or something similar.

Hi, 

I changed my code around a bit so it works.

gameSettings = loadTable("gameSettings.json") if gameSettings == nil then gameSettings = {} gameSettings.highScore = 0 end

Hi Guys, please, I’m getting crazy about this…
 
I just copy and paste what Quxrocket pasted above and It does doesn’t works in my emulator.

gameSettings = loadTable(“gameSettings.json”)
if gameSettings == nil then
gameSettings = {}
gameSettings.highScore = 0
end

I got this error:

<

attempt to call global ‘loadTable’(a nil value)

>

The error raises for the line gameSettings = loadTable(“gameSettings.json”)

I’m new in Corona and I have the free acount yet…  Any idea about what I’m not doing? Is there some extra thing to download? I don’t know…

Hi t.alexandre,

Have you copied - pasted the code in http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/ into your project? I think that’s what’s happening - the simulator doesn’t know what you’re talking about when you refer to loadTable, which is not in the Corona API, but Rob Miracle’s json saving method.

Hope this helps!

Thank you SO MUCH cozymonster29.

I’ll try to isolate this function in some utility.lua file now. thank you so much!

Hi Guys. In order to organize my functions, I created a utlility.lua file and I put the following code:

local M = {} local json = require("json") function M.saveTable(t, filename) &nbsp; &nbsp; local path = system.pathForFile( filename, system.DocumentsDirectory) &nbsp; &nbsp; local file = io.open(path, "w") &nbsp; &nbsp; if file then &nbsp; &nbsp; &nbsp; &nbsp; local contents = json.encode(t) &nbsp; &nbsp; &nbsp; &nbsp; file:write( contents ) &nbsp; &nbsp; &nbsp; &nbsp; io.close( file ) &nbsp; &nbsp; &nbsp; &nbsp; return true &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; return false &nbsp; &nbsp; end end function M.loadTable(filename) &nbsp; &nbsp; local path = system.pathForFile( filename, system.DocumentsDirectory) &nbsp; &nbsp; local contents = "" &nbsp; &nbsp; local myTable = {} &nbsp; &nbsp; local file = io.open( path, "r" ) &nbsp; &nbsp; if file then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- read all contents of file into a string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;local contents = file:read( "\*a" ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myTable = json.decode(contents); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;io.close( file ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return myTable&nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; return nil end &nbsp;

And, In my main code, I put:

-- Load Configurations &nbsp;&nbsp;&nbsp;&nbsp;local utility = require("utility") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local mySettings = utility.loadTable("settings.json") &nbsp; &nbsp;\<\<-- Error HERE &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if mySettings == nil then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings = {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.soundOn = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.musicOn = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.score = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utility.saveTable(mySettings, "settings.json") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local mySettings = utility.loadTable("settings.json") &nbsp;

But I got this error at the first line:

<

attempt to index local ‘utility’(a boolean value)

>

I dont understand this error because since I didn’t previusly declared mySettings, it should be converted to boolean false or to a table regarding the result isn’t it?

at the end of utility.lua you need the line:

    return M

Hi Guys, please, I’m getting crazy about this…
 
I just copy and paste what Quxrocket pasted above and It does doesn’t works in my emulator.

gameSettings = loadTable(“gameSettings.json”)
if gameSettings == nil then
gameSettings = {}
gameSettings.highScore = 0
end

I got this error:

<

attempt to call global ‘loadTable’(a nil value)

>

The error raises for the line gameSettings = loadTable(“gameSettings.json”)

I’m new in Corona and I have the free acount yet…  Any idea about what I’m not doing? Is there some extra thing to download? I don’t know…

Hi t.alexandre,

Have you copied - pasted the code in http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/ into your project? I think that’s what’s happening - the simulator doesn’t know what you’re talking about when you refer to loadTable, which is not in the Corona API, but Rob Miracle’s json saving method.

Hope this helps!

Thank you SO MUCH cozymonster29.

I’ll try to isolate this function in some utility.lua file now. thank you so much!

Hi Guys. In order to organize my functions, I created a utlility.lua file and I put the following code:

local M = {} local json = require("json") function M.saveTable(t, filename) &nbsp; &nbsp; local path = system.pathForFile( filename, system.DocumentsDirectory) &nbsp; &nbsp; local file = io.open(path, "w") &nbsp; &nbsp; if file then &nbsp; &nbsp; &nbsp; &nbsp; local contents = json.encode(t) &nbsp; &nbsp; &nbsp; &nbsp; file:write( contents ) &nbsp; &nbsp; &nbsp; &nbsp; io.close( file ) &nbsp; &nbsp; &nbsp; &nbsp; return true &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; return false &nbsp; &nbsp; end end function M.loadTable(filename) &nbsp; &nbsp; local path = system.pathForFile( filename, system.DocumentsDirectory) &nbsp; &nbsp; local contents = "" &nbsp; &nbsp; local myTable = {} &nbsp; &nbsp; local file = io.open( path, "r" ) &nbsp; &nbsp; if file then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- read all contents of file into a string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;local contents = file:read( "\*a" ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myTable = json.decode(contents); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;io.close( file ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return myTable&nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; return nil end &nbsp;

And, In my main code, I put:

-- Load Configurations &nbsp;&nbsp;&nbsp;&nbsp;local utility = require("utility") &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local mySettings = utility.loadTable("settings.json") &nbsp; &nbsp;\<\<-- Error HERE &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if mySettings == nil then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings = {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.soundOn = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.musicOn = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySettings.score = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utility.saveTable(mySettings, "settings.json") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local mySettings = utility.loadTable("settings.json") &nbsp;

But I got this error at the first line:

<

attempt to index local ‘utility’(a boolean value)

>

I dont understand this error because since I didn’t previusly declared mySettings, it should be converted to boolean false or to a table regarding the result isn’t it?