How to use GGData?

Thanks, Bob, 

That’s exactly the kind of information I was after.

Jules.

Hopefully Bob is still here and can give me a hand here. I am using the method you described above and had no troubles getting it to work as it is. However, I am trying to get M.data to hold individual tables for each of my game levels. So basically a multi level array. Below is the code I have so far. My issue is with one line. 

table.insert(M.data, {lvl[i] = level[i]})

This line throws an error: ‘}’ expected near ‘=’ stack traceback.

I have used table.insert(M.data, {lvl = level[i]}) and it works fine, except I end up with 100 levels with the key of ‘lvl’. I have tried making a local variable called lvl that increments each time through the loop and printed out that it works(as in lvl1 all the way to lvl100). But when I tried to use that in the insert line like this

table.insert(M.data, {tostring(lvl) = level[i]}) - this failed with the same error as above

or table.insert(M.data, {lvl = level[i]})  -this produced 100 tables named ‘lvl’ instead of the value of the lvl varaible.

All I want to do is add the level number to ‘lvl’ each time it loops. What am I doing wrong and how can I solve this. This code only executes the first time the game is run but I need it to happen correctly. I just want each level to be its own table within M.data. It will simplify things greatly. I am sure my error will be something simple or a syntax issue. I have only been playing with lua for the last month. Please point me in the right direction.

function M:loadData() -- IF GAME DATA FILE DOES NOT EXIST : CREATE THE DATA BOX TO HOLD GAME DATA local function createDataFile() local box1 = GGData:new( "gameData" ) local level = {} M.data = {} local i = 1 while i \< 101 do local lvl print(lvl) level[i] = { level = i, unlocked = true, hasRated = false, highScore = 0, stars = 0 } print("level ".. i .. " = " .. (tostring(level[i]))) this prints perfect table.insert(M.data, {lvl[i] = level[i]}) --box1:set( "lvl"..i , level[i]) -- works inserting directly into the box i = i + 1 end box1.gameData = {} box1.gameData = M.data box1:save() end

Never mind. I got it sorted whilst waiting for my first post to be approved. I am persistent if nothing else. The following code gives me exactly what I was after. Using M.data[tostring(lvl)] = level[i] worked a treat creating a new key and table value.  :slight_smile:

local i = 1 local lvl = "lvl" .. i while i \< 101 do level[i] = { gameState = "PLAY", hasLiked = false, hasRated = false, level = i, highScore = 0} lvl = "lvl" .. i --converts lvl to a string and inserts it into the M.data table M.data[tostring(lvl)] = level[i] i = i + 1 end

Scott,

Sorry I missed your post yesterday, but as it turns out your ‘persistence’ paid off.  If you are truly persistent, you will do well coding (IMHO).  

Good luck!

Bob

Hopefully Bob is still here and can give me a hand here. I am using the method you described above and had no troubles getting it to work as it is. However, I am trying to get M.data to hold individual tables for each of my game levels. So basically a multi level array. Below is the code I have so far. My issue is with one line. 

table.insert(M.data, {lvl[i] = level[i]})

This line throws an error: ‘}’ expected near ‘=’ stack traceback.

I have used table.insert(M.data, {lvl = level[i]}) and it works fine, except I end up with 100 levels with the key of ‘lvl’. I have tried making a local variable called lvl that increments each time through the loop and printed out that it works(as in lvl1 all the way to lvl100). But when I tried to use that in the insert line like this

table.insert(M.data, {tostring(lvl) = level[i]}) - this failed with the same error as above

or table.insert(M.data, {lvl = level[i]})  -this produced 100 tables named ‘lvl’ instead of the value of the lvl varaible.

All I want to do is add the level number to ‘lvl’ each time it loops. What am I doing wrong and how can I solve this. This code only executes the first time the game is run but I need it to happen correctly. I just want each level to be its own table within M.data. It will simplify things greatly. I am sure my error will be something simple or a syntax issue. I have only been playing with lua for the last month. Please point me in the right direction.

function M:loadData() -- IF GAME DATA FILE DOES NOT EXIST : CREATE THE DATA BOX TO HOLD GAME DATA local function createDataFile() local box1 = GGData:new( "gameData" ) local level = {} M.data = {} local i = 1 while i \< 101 do local lvl print(lvl) level[i] = { level = i, unlocked = true, hasRated = false, highScore = 0, stars = 0 } print("level ".. i .. " = " .. (tostring(level[i]))) this prints perfect table.insert(M.data, {lvl[i] = level[i]}) --box1:set( "lvl"..i , level[i]) -- works inserting directly into the box i = i + 1 end box1.gameData = {} box1.gameData = M.data box1:save() end

Never mind. I got it sorted whilst waiting for my first post to be approved. I am persistent if nothing else. The following code gives me exactly what I was after. Using M.data[tostring(lvl)] = level[i] worked a treat creating a new key and table value.  :slight_smile:

local i = 1 local lvl = "lvl" .. i while i \< 101 do level[i] = { gameState = "PLAY", hasLiked = false, hasRated = false, level = i, highScore = 0} lvl = "lvl" .. i --converts lvl to a string and inserts it into the M.data table M.data[tostring(lvl)] = level[i] i = i + 1 end

Scott,

Sorry I missed your post yesterday, but as it turns out your ‘persistence’ paid off.  If you are truly persistent, you will do well coding (IMHO).  

Good luck!

Bob