How to use GGData?

I am trying to learn how to use GGData.

I just want to save the information in my own App. (NO iCloud, Facebook, on Line, etc)

only my app. (I think it will be easier to start like that)

I just don’t understand the main concept and how to do it.

I have read a lot, they say it’s easy, but I would like to really know how.

Like “tap” on a button and “Mute” the music…

like tell the app in one scene, the background is “blue”

and when you go to another scene, and comeback to the previews scene

the app will remember that the background IS blue

like “tap” one button and adds points (1, 2, 3, …)

and then after going to many other scenes you comeback and you see your

score = 3

things like that, but I would like to “really” understand the concept so I can apply it in many

circumstances…

Thanks for your help


I just watched the video ( I bought it )

Beginning Game Dev, Section 07: Saving and Loading Files by Jay…

the froggy One…

I copy the GGData.lua into my folder! – Done

I put this line on top of my file – settings.lua

local GGData = require ("GGData")

Before the createScene I create a new GGData Box

local scores = GGData.new("gamescores")

in the enterScene I just save it

scores:save()

And I got this ERROR


File: GGData.lua
Line: 184
Attempt to concatenate field ‘id’ (a nil value)


You see… As much as they say it’s easy…Please I really need someone’s help


–**-- [39 Views and 0 reply] --**-- ( on Friday July 11 at 11:59 ) --**–

either nobody knows the answer…

or I am not asking the right question…

or this is just too difficult to explain…

Let’s see in a few days…I have faith that I will understand this concept…



–**-- [55 Views and 0 reply] --**-- ( on Monday July 14 at 5:08 pm ) --**–

Let’s see in a few days…I have faith that someone will help me with this problem…


I think you are not setting any value before calling box:save()

local box = GGData:new( “sample” )
Set some values
box:set( “message”, “hello, world” )
box.anotherValue = 10
Get some values
print( box:get( “anotherValue” ) ) – prints 10
print( box.message ) – prints ‘hello, world’
Save the box
box:save()

Thanks for that… I saw that in some web site… but I still don’t get it

I have the storyboard going on, and I don’t know where to put

box.anotherValue = 10

or where should I write

box:save()

 

I wish you could help me a little bit more, I have been wanting to really understand this for almost 2 years

 

Thanks for your time and patience

I think you are not setting any value before calling box:save()

local box = GGData:new( “sample” )
Set some values
box:set( “message”, “hello, world” )
box.anotherValue = 10
Get some values
print( box:get( “anotherValue” ) ) – prints 10
print( box.message ) – prints ‘hello, world’
Save the box
box:save()

2 Years and 2 months! and nothing…

HOPE THIS WILL HELP!

  -- ========================== -- MAIN -- =========================== local game = require("Game")   -- EARLY IN THE MAIN CODE, LOAD IN DATA.. -- THE DATA IS/WILL BE STORED ON USER'S DEVICE -- IF THIS IS FIRST RUN OF THE APP, THE LOADDATA FUNCTION WILL -- CREATE DEAULT VALUES AND STORE THEM ONTO THE USER'S DEVICE -- \*\*\* YOU CAN CHECK THIS ON THE SIMULATOR, AFTER RUNNING THE APP,  -- FORM THE SIMULATOR 'FILE' MENU,CHOOSE 'SHOW PROJECT SANDBOX' --- DOCUMENTS/BOX game:loadData() print(game.data.gameState)   -- IN WHAT EVER MODULE(FILE) COMPOSER SCENE, IN WHICH YOU INCLUDE -- local game = require("Game") -- UPDATE THE DATA TO THE DATA TABLE LIKE SO... THEN SAVE IT TO THE DEVICE game.data.gameState = "NEW LEVEL" game.data.level = 2 game:saveData()   print(game.data.gameState)     -- ========================== -- GAME MODULE  -- =========================== local GGData = require("ggData") local M = {}   M.data = nil   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" )   M.data   = { gameState = "PLAY", hasLiked = false, hasRated = false,                level = 1, score = 0                                                                 }     box1.gameData = {} box1.gameData = M.data box1:save() end   local box1 = GGData:new( "gameData" ) if box1.gameData == nil then createDataFile() else M.data = box1.gameData end end   function M:saveData() local box1 = GGData:new( "gameData" ) box1.gameData = M.data box1:save() end   return M    
  1.  test this by creating new project, and paste first part of this code into main.lua  and the second part into game.lua

  2.  be sure that ggData is part of the files in this new test project/

  3.  add/change fields in the M.data table to what you would want to track for your particular game.

  4. after you run the app, and after you change some game data fields to new values, check the ‘documents’ directory on the simulator and you will see how that data gets updated.

good luck!

Bob

Whooow!, thanks Bob…

I know now why I have 2 years and almost 3 months and still I don’t get it.

This level of programming is way too advanced for me… for now…

I have never seen the “sandbox” I opened and it has like 5 thousand things.

I have seen the – return M – before, I don’t know what is that for, and what do I do with it.

I never went to school to learn programing. I just started using Corona 2 years ago.

I will learn for sure, thanks to all of you out there.

-------------- Just a couple of questions ------------

the

local game = require(“Game”)

is a lua file that I have to create? like Game.lua? and can I use capital letters for the first letter?

if so…

I guess, everything down from here:

– ==========================
– GAME MODULE 
– ===========================

Goes in the Game.lua file… correct?

And this goes in main.lua

– ==========================
– MAIN
– ===========================

Now… the code in main.lua is it before I go to the home.lua? home.lua is my main view, or the first view screen.

I will start to experiment with this…

but YES! for sure this information will help me a lot, in the learning curve for this topic.

specially the “sandbox” information, thanks

--------- quick note -------

I just put everything in there…

I “print” this

print(game.data.gameState)
print(game.data.level)

and is printing

NEW LEVEL

2

I can not be in a new level, and the level is 2 already…

it is very confusing…

sorry!

Victor

Victor,

A good way to learn coding, is checking out some of the many good books out there, and mostly reviewing sample code over and over and over.  If you want to learn without going to an official school, you need to spend a lot of time looking at code, then retyping code, changing code and experimenting, and then you can start to get a understanding on what is going on.  There are many good developers that have posted very helpful tutorials on lua and corona.  Search the internet and corona forums and you will find a lot of them. Lua is one of the easiest programming languages to learn.  And Corona is a great tool for making good 2d mobile apps. You might be best starting with some of the sample code included int he corona sdk.

Q1 & Q2.  Yes, create a separate file and paste that code sample include everything below 

– ==========================

– GAME MODULE 

– =========================== 

into that file.

 You can name that file anything you want. Just be careful to avoid naming it something that might be a reserved lua-corona word, like ‘timer’ or such.  This can cause issues and can also get confusing.

I used ‘Game.lua’ my example, but ‘game.lua’ works as well.  Actually, I typed that example fairly quickly, and the normal style for me is to name all my lua files with lower case. So I normally would have named that file game.lua, not Game.lua … but either is fine.

In my opinion it is important to try and have some consistent style with how you name things … it helps keep things clean and neat and much much easier to debug when there are issues.

So in main, I could of, and normally do it this way:

local Game = require(“game”)  

– that is the reverse of what I showed in the example. But again I was typing it out quickly and made a typo error. 

– you can upper or lower case any of it.  Just be consistent on how you do it.

– I like to upper case first letter of the ‘handle’  to a required file 

local Game = require(“game”)

‘Game’ is the handle to that file I am requiring in to main, in this example.  I do this, so when I see that ‘uppercase’ word later in the code I know it is a handle to a file I required in.

So, if you decide to name the ‘handle’ ‘Game’ instead of ‘game’ as I initially had it, you will need to uppercase the ‘g’ of game, everywhere in the example where I referenced it  as such: 

game.data.gameState   would need to be Game.data.gameState

Q3.  You can put all that sample code ‘above’ the

– ==========================

– GAME MODULE 

– ===========================

into home.lua,  but you can also split it up like so…

In main.lua

  1. require the game.lua file

local Game = require(“game”)

  1. load the data in

Game:loadData()

  1. print it just so you can see it is working…  take out the print when you get ready to deploy your app to the market

print(Game.data.gameState)

Then in home.lua

  1. require the game.lua file

local Game = require(“game”)   – do this above all the composer functions

  1. I am not that verse with Composer, but I would probably in the create or show function put the rest of the code example

print(Game.data.gameState)

– change the game state

Game.data.gameState = “READY” – or whatever names you give the different game states.

You can do it as I have it, and paste that code into main.lua…  because that is still the first code that will execute in your app. Then in your home.lua (at the top of that file) you just need to require the game.lua file just as you did in main.

local Game = require(“game”)

This way, you loaded your data files in right when main is called, and you are accessing the game date, in home.lua where apparently the game action occurs.

Regarding your ‘quick note’.  It appears confusing, but the reason you see it print/show ‘NEW LEVEL’ and ‘2’ is because I (just as an example) set those values in the sample code, then saved that info.

game.data.gameState = “NEW LEVEL”
game.data.level = 2

game:saveData()

This was just to show you how to update that data table.  In your actual code, you would set those values when you decide they need to be set… clearly after a level or wave of your game is done, you would set those values to the data table to be saved.

You will also, notice, if you run the app again those values are still there. If you want to start back at the default values, that are set in the game.lua file…  you need to get to the sandbox, documents/boxes directory and delete that ggData file ‘gameData.box’.

When you start the app again, it will re-create that file with the default data, and you are starting fresh.

Good luck!!

Bob

Bob…

Thank you very much. You have taken the time to help me, and that…

is something really good, it means a lot to me… thanks.

I have been doing some work(it’s been only a few days)

but I got the sandbox and I see the changes…

I think that I am at 92% of understanding this…

I just need some time to work on my new app using this GGData…

Thank you for all your help one more time… I will probably have another question soon.

but for now I have a lot to work with. You helped me a lot.

Thanks

Victor

Thanks for that… I saw that in some web site… but I still don’t get it

I have the storyboard going on, and I don’t know where to put

box.anotherValue = 10

or where should I write

box:save()

 

I wish you could help me a little bit more, I have been wanting to really understand this for almost 2 years

 

Thanks for your time and patience

2 Years and 2 months! and nothing…

HOPE THIS WILL HELP!

  -- ========================== -- MAIN -- =========================== local game = require("Game")   -- EARLY IN THE MAIN CODE, LOAD IN DATA.. -- THE DATA IS/WILL BE STORED ON USER'S DEVICE -- IF THIS IS FIRST RUN OF THE APP, THE LOADDATA FUNCTION WILL -- CREATE DEAULT VALUES AND STORE THEM ONTO THE USER'S DEVICE -- \*\*\* YOU CAN CHECK THIS ON THE SIMULATOR, AFTER RUNNING THE APP,  -- FORM THE SIMULATOR 'FILE' MENU,CHOOSE 'SHOW PROJECT SANDBOX' --- DOCUMENTS/BOX game:loadData() print(game.data.gameState)   -- IN WHAT EVER MODULE(FILE) COMPOSER SCENE, IN WHICH YOU INCLUDE -- local game = require("Game") -- UPDATE THE DATA TO THE DATA TABLE LIKE SO... THEN SAVE IT TO THE DEVICE game.data.gameState = "NEW LEVEL" game.data.level = 2 game:saveData()   print(game.data.gameState)     -- ========================== -- GAME MODULE  -- =========================== local GGData = require("ggData") local M = {}   M.data = nil   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" )   M.data   = { gameState = "PLAY", hasLiked = false, hasRated = false,                level = 1, score = 0                                                                 }     box1.gameData = {} box1.gameData = M.data box1:save() end   local box1 = GGData:new( "gameData" ) if box1.gameData == nil then createDataFile() else M.data = box1.gameData end end   function M:saveData() local box1 = GGData:new( "gameData" ) box1.gameData = M.data box1:save() end   return M    
  1.  test this by creating new project, and paste first part of this code into main.lua  and the second part into game.lua

  2.  be sure that ggData is part of the files in this new test project/

  3.  add/change fields in the M.data table to what you would want to track for your particular game.

  4. after you run the app, and after you change some game data fields to new values, check the ‘documents’ directory on the simulator and you will see how that data gets updated.

good luck!

Bob

Whooow!, thanks Bob…

I know now why I have 2 years and almost 3 months and still I don’t get it.

This level of programming is way too advanced for me… for now…

I have never seen the “sandbox” I opened and it has like 5 thousand things.

I have seen the – return M – before, I don’t know what is that for, and what do I do with it.

I never went to school to learn programing. I just started using Corona 2 years ago.

I will learn for sure, thanks to all of you out there.

-------------- Just a couple of questions ------------

the

local game = require(“Game”)

is a lua file that I have to create? like Game.lua? and can I use capital letters for the first letter?

if so…

I guess, everything down from here:

– ==========================
– GAME MODULE 
– ===========================

Goes in the Game.lua file… correct?

And this goes in main.lua

– ==========================
– MAIN
– ===========================

Now… the code in main.lua is it before I go to the home.lua? home.lua is my main view, or the first view screen.

I will start to experiment with this…

but YES! for sure this information will help me a lot, in the learning curve for this topic.

specially the “sandbox” information, thanks

--------- quick note -------

I just put everything in there…

I “print” this

print(game.data.gameState)
print(game.data.level)

and is printing

NEW LEVEL

2

I can not be in a new level, and the level is 2 already…

it is very confusing…

sorry!

Victor

Victor,

A good way to learn coding, is checking out some of the many good books out there, and mostly reviewing sample code over and over and over.  If you want to learn without going to an official school, you need to spend a lot of time looking at code, then retyping code, changing code and experimenting, and then you can start to get a understanding on what is going on.  There are many good developers that have posted very helpful tutorials on lua and corona.  Search the internet and corona forums and you will find a lot of them. Lua is one of the easiest programming languages to learn.  And Corona is a great tool for making good 2d mobile apps. You might be best starting with some of the sample code included int he corona sdk.

Q1 & Q2.  Yes, create a separate file and paste that code sample include everything below 

– ==========================

– GAME MODULE 

– =========================== 

into that file.

 You can name that file anything you want. Just be careful to avoid naming it something that might be a reserved lua-corona word, like ‘timer’ or such.  This can cause issues and can also get confusing.

I used ‘Game.lua’ my example, but ‘game.lua’ works as well.  Actually, I typed that example fairly quickly, and the normal style for me is to name all my lua files with lower case. So I normally would have named that file game.lua, not Game.lua … but either is fine.

In my opinion it is important to try and have some consistent style with how you name things … it helps keep things clean and neat and much much easier to debug when there are issues.

So in main, I could of, and normally do it this way:

local Game = require(“game”)  

– that is the reverse of what I showed in the example. But again I was typing it out quickly and made a typo error. 

– you can upper or lower case any of it.  Just be consistent on how you do it.

– I like to upper case first letter of the ‘handle’  to a required file 

local Game = require(“game”)

‘Game’ is the handle to that file I am requiring in to main, in this example.  I do this, so when I see that ‘uppercase’ word later in the code I know it is a handle to a file I required in.

So, if you decide to name the ‘handle’ ‘Game’ instead of ‘game’ as I initially had it, you will need to uppercase the ‘g’ of game, everywhere in the example where I referenced it  as such: 

game.data.gameState   would need to be Game.data.gameState

Q3.  You can put all that sample code ‘above’ the

– ==========================

– GAME MODULE 

– ===========================

into home.lua,  but you can also split it up like so…

In main.lua

  1. require the game.lua file

local Game = require(“game”)

  1. load the data in

Game:loadData()

  1. print it just so you can see it is working…  take out the print when you get ready to deploy your app to the market

print(Game.data.gameState)

Then in home.lua

  1. require the game.lua file

local Game = require(“game”)   – do this above all the composer functions

  1. I am not that verse with Composer, but I would probably in the create or show function put the rest of the code example

print(Game.data.gameState)

– change the game state

Game.data.gameState = “READY” – or whatever names you give the different game states.

You can do it as I have it, and paste that code into main.lua…  because that is still the first code that will execute in your app. Then in your home.lua (at the top of that file) you just need to require the game.lua file just as you did in main.

local Game = require(“game”)

This way, you loaded your data files in right when main is called, and you are accessing the game date, in home.lua where apparently the game action occurs.

Regarding your ‘quick note’.  It appears confusing, but the reason you see it print/show ‘NEW LEVEL’ and ‘2’ is because I (just as an example) set those values in the sample code, then saved that info.

game.data.gameState = “NEW LEVEL”
game.data.level = 2

game:saveData()

This was just to show you how to update that data table.  In your actual code, you would set those values when you decide they need to be set… clearly after a level or wave of your game is done, you would set those values to the data table to be saved.

You will also, notice, if you run the app again those values are still there. If you want to start back at the default values, that are set in the game.lua file…  you need to get to the sandbox, documents/boxes directory and delete that ggData file ‘gameData.box’.

When you start the app again, it will re-create that file with the default data, and you are starting fresh.

Good luck!!

Bob

Bob…

Thank you very much. You have taken the time to help me, and that…

is something really good, it means a lot to me… thanks.

I have been doing some work(it’s been only a few days)

but I got the sandbox and I see the changes…

I think that I am at 92% of understanding this…

I just need some time to work on my new app using this GGData…

Thank you for all your help one more time… I will probably have another question soon.

but for now I have a lot to work with. You helped me a lot.

Thanks

Victor

Hi, Bob, 

I have found your input on this thread to be extremely useful and, as a direct result, have just started using GGData in my development.

I’m curious to know if there are any performance considerations to be taken in to account or best practices for using GGData.
I’ve had a trawl around, but haven’t found anything on the subject.

Thanks,

Strange Flight,

In main I have a section of code that handles loading and saving game data.  Best practice for me is, when the game starts I call loadData and puts the data from the saved file into the gameData table for use during the game.  That table gets updated when ever anything changes the values of the fields in that table.  At any time, such as a level is completed or as often as needed I will call saveData which then takes the gameData table and saves it back the the file.

I don’t do a lot of performance testing.  So I can not answer how much performance hit there is … and have not used anything else to compare it to.

 But I think for most mobile apps it would be negligible.  If have a tower defense game(Tanks and Towers) where it gets intense in later waves with a lot of towers blasting away at tanks and projectiles and explosions everywhere.  I do not call saveData till the end of the wave, even though the gameData table is updated during the action. If I were to call it repeatedly during the wave it could have a performance impact that could effect the game loop(enterFrame event). I figure in the case of a crash in middle of a wave, user will just start over at the beginning of the wave, which is the last saved data.

So in this sample below, in my main.lua I will call loadData at the start of the app.  During the game as needed, just call saveData().

In the gameData table you can create what ever fields you want, including tables. Just have 4 listed in my sample, but you could have many more. 

  local gameData = {}   ------------------------------------------------------- -- DATA FILE MANAGEMENT -------------------------------------------------------   local function saveData() local box1 = GGData:new( "gameData" ) box1.gameData = gameData box1:save() end   local function createDataFile() local box1 = GGData:new( "gameData" )   gameData   = { gameState = "PLAY", points = 0, username = "USER",  hasRated = false  }   box1.gameData = {} box1.gameData = gameData box1:save() end   local box1 = GGData:new( "gameData" ) if box1.gameData == nil then createDataFile() else gameData = box1.gameData end   end

There is also some good info on the Corona Geek show, maybe 2 weeks ago about persistent data and a good tip about adding new fields to the data table with updated releases of your app. Roaming Gamer who posted the info about that on the Corona Geek Show apparently has some tutorials (sample code for different type apps) on his site.  I would imagine they could be very helpful.

Good Luck

Thanks, Bob, 

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

Jules.

Hi, Bob, 

I have found your input on this thread to be extremely useful and, as a direct result, have just started using GGData in my development.

I’m curious to know if there are any performance considerations to be taken in to account or best practices for using GGData.
I’ve had a trawl around, but haven’t found anything on the subject.

Thanks,

Strange Flight,

In main I have a section of code that handles loading and saving game data.  Best practice for me is, when the game starts I call loadData and puts the data from the saved file into the gameData table for use during the game.  That table gets updated when ever anything changes the values of the fields in that table.  At any time, such as a level is completed or as often as needed I will call saveData which then takes the gameData table and saves it back the the file.

I don’t do a lot of performance testing.  So I can not answer how much performance hit there is … and have not used anything else to compare it to.

 But I think for most mobile apps it would be negligible.  If have a tower defense game(Tanks and Towers) where it gets intense in later waves with a lot of towers blasting away at tanks and projectiles and explosions everywhere.  I do not call saveData till the end of the wave, even though the gameData table is updated during the action. If I were to call it repeatedly during the wave it could have a performance impact that could effect the game loop(enterFrame event). I figure in the case of a crash in middle of a wave, user will just start over at the beginning of the wave, which is the last saved data.

So in this sample below, in my main.lua I will call loadData at the start of the app.  During the game as needed, just call saveData().

In the gameData table you can create what ever fields you want, including tables. Just have 4 listed in my sample, but you could have many more. 

  local gameData = {}   ------------------------------------------------------- -- DATA FILE MANAGEMENT -------------------------------------------------------   local function saveData() local box1 = GGData:new( "gameData" ) box1.gameData = gameData box1:save() end   local function createDataFile() local box1 = GGData:new( "gameData" )   gameData   = { gameState = "PLAY", points = 0, username = "USER",  hasRated = false  }   box1.gameData = {} box1.gameData = gameData box1:save() end   local box1 = GGData:new( "gameData" ) if box1.gameData == nil then createDataFile() else gameData = box1.gameData end   end

There is also some good info on the Corona Geek show, maybe 2 weeks ago about persistent data and a good tip about adding new fields to the data table with updated releases of your app. Roaming Gamer who posted the info about that on the Corona Geek Show apparently has some tutorials (sample code for different type apps) on his site.  I would imagine they could be very helpful.

Good Luck