Using JSON

I set up a load save.lua module using the code in Rob Miracles article: 

https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

I set up a data module called my data.lua as follows:

local loadsave = require(“loadsave”)

local M = {}

M.maxLevels = 500

M.settings = {}

–Data table that will hold various player data and otherwise.

M.settings.levels = {}

–Table that tracks the player’s per-level progress. For this tutorial, only .stars

– These lines are just here to pre-populate the table.

– In reality, your app would likely create a level entry when each level is unlocked and the score/stars are saved.

– Perhaps this happens at the end of your game level, or in a scene between game levels.

M.settings.levels[1] = {}

M.settings.levels[1].stars = 1

M.settings.levels[1].score = 4567


M.settings.levels[2] = {}

M.settings.levels[2].stars = 2

M.settings.levels[2].score = 4394


M.settings.levels[3] = {}

M.settings.levels[3].stars = 1

M.settings.levels[3].score = 8384


M.settings.levels[4] = {}

M.settings.levels[4].stars = 0

M.settings.levels[4].score = 10294


– levels data members:

–      .stars – Stars earned per level

–      .score – Score for the level

return M

In my level 1 code I did the following:

function star_Counter()

if gameScore >= 7001 then

   stars = 3

   levels = 1

elseif(7000>= gameScore and gameScore >=3080) then

   stars = 2

   levels = 1

elseif gameScore <= 3079 then

   stars = 1

   levels = 1

end

   savestars()

end 


function savestars()

  mydata:saveTable(levels, stars)

end

I played the level and I should receive 2 stars.  I looked in the sandbox but I didn’t have any thing for my data or for stars.  I’m trying to save using JSON but I’m having trouble.  I’m having trouble grasping tables.  I can save one data item to a table (xml) but not multiple items.  So, based on what I am reading I thought it might be easier if I used jSON.  But I’m not doing something correct.  

I would GREATLY appreciate help in grasping this.

Thanks

Lori

I think this:

(7000>= gameScore and gameScore >=3080)

should probably be:
 

(7000 < = gameScore and gameScore >=3080)

Personally I would for sanity sake, rewrite that whole condition like this:

if (gameScore >= 3080 and gameScore <= 7000) then …

I find  that easier to read. 

That makes sense - I’ll make the switch.  Any suggestion on how I am trying to use JSON?

No. You’re doing exactly what I do.  I use my loadsave function to load and save the M.settings table, not the entire M table since it tends to hold fixed and temporary values that I don’t want to save.

Rob

I think this:

(7000>= gameScore and gameScore >=3080)

should probably be:
 

(7000 < = gameScore and gameScore >=3080)

Personally I would for sanity sake, rewrite that whole condition like this:

if (gameScore >= 3080 and gameScore <= 7000) then …

I find  that easier to read. 

That makes sense - I’ll make the switch.  Any suggestion on how I am trying to use JSON?

No. You’re doing exactly what I do.  I use my loadsave function to load and save the M.settings table, not the entire M table since it tends to hold fixed and temporary values that I don’t want to save.

Rob