Table saving problem

Hello guys !

So I have this problem which I can’t find the way how to fix it.

myTable = {} myTable.musicOn = false myTable.soundOn = true myTable.lv1 = false loadsave.saveTable(myTable, "myTable.json"

You can see that I make and save the table on levelselect.lua.

Here

myTable = loadsave.loadTable("myTable.json") local button1Press = function( event ) if (myTable.lv1 == false) then storyboard.gotoScene("story","fade",200) myTable.lv1 = true loadsave.saveTable(myTable, "myTable.json") elseif (myTable.lv1== true) then storyboard.purgeScene("level1") storyboard.gotoScene("level1","fade",200) end end

I’m telling the app if that myTable.lv1 is = false it will go to story screen(I want that story screen happens only once when player play lv1 for the first time not the second too. So that works but only if I don’t close the app.

If I close it and open it again it will load this code again 

myTable = {} myTable.musicOn = false myTable.soundOn = true myTable.lv1 = false loadsave.saveTable(myTable, "myTable.json"

and that will overwrite the myTable.lv1 = true. Basicly same thing will happen as first time.

So my question is - How to avoid that overwriting ?

Thanks!

You need to change the order you do things:

[lua]

local button1Press = function( event )
    
    if (myTable.lv1 == false) then
        myTable.lv1 = true
        loadsave.saveTable(myTable, “myTable.json”)

        storyboard.gotoScene(“story”,“fade”,200)
    elseif (myTable.lv1== true) then
        storyboard.purgeScene(“level1”)
        storyboard.gotoScene(“level1”,“fade”,200)
    end
    
end

[/lua]

Once you call storyboard.gotoScene() any lines of code after that will probably not execute.  Do the save before you goto the scene and see if that helps.   I also trust you have a closing ) on your saveTable calls.

Hi,

Thanks for replying.

Still no luck :frowning: When I close the app everything resets.

myTable = {} myTable.musicOn = false myTable.soundOn = true myTable.lv1 = false loadsave.saveTable(myTable, "myTable.json") - this thing resets everything

If I remove that loadsave.saveTable(myTable, “myTable.json”) 

table won’t exist and I won’t be able to use it! So the question is still - How to avoid that overwriting.

Thanks!

Okay I think I see your problem.  You need to do something like this in main.lua:

[lua]

local myTable = loadsave(“myTable.json”)

if myTable == nil then

myTable = {}
myTable.musicOn = false
myTable.soundOn = true
myTable.lv1 = false

loadsave.saveTable(myTable, “myTable.json”)

end

[/lua]

You basically need to check and see if there is a saved settings file, if so read it in, if you can’t read it in, assume it doesn’t exist and initialize a new one.

Thanks I will try it! But how I can call that variables(stored at main.lua) from levelselect.lua? I want to use it at this event on levelselect.lua.

local button1Press = function( event ) if (myTable.lv1 == false) then -- Here I want to use variable myTable.lv1 which I made in main.lua end

Thanks!

The easy thing is to make it global, but that isn’t a best practice.  Just leave the “local” off of in front of myTable when you create it.

The next easy thing to do is to add your table to an existing object that lives from scene to scene like the storyboard object.  You can do something like this:

storyboard.myTable = loadsave.loadTable(“myTable.json”)

 

From that point on, in any storyboard scene you can access:  storyboard.myTable.soundOn

I’m doing it by leaving local so table is global now. However it works but still losing the memory when closing the app. I think that 

code in main is not working as it should. I know saving works because it works if I don’t close the game that means something is wrong on loading the table when opening the game again. I will try to mess up with this and see if I can get it working.

  1. if myTable == nil then
  2. myTable = {}myTable.musicOn = falsemyTable.soundOn = truemyTable.lv1 = falseloadsave.saveTable(myTable, “myTable.json”)
  3. end

I got this working! I added loading of table before that if myTable == nil…

and I needed to delete all my game data from phone in settings otherwise there will already be myTable.lv1 = true.

Thank you for help!

You need to change the order you do things:

[lua]

local button1Press = function( event )
    
    if (myTable.lv1 == false) then
        myTable.lv1 = true
        loadsave.saveTable(myTable, “myTable.json”)

        storyboard.gotoScene(“story”,“fade”,200)
    elseif (myTable.lv1== true) then
        storyboard.purgeScene(“level1”)
        storyboard.gotoScene(“level1”,“fade”,200)
    end
    
end

[/lua]

Once you call storyboard.gotoScene() any lines of code after that will probably not execute.  Do the save before you goto the scene and see if that helps.   I also trust you have a closing ) on your saveTable calls.

Hi,

Thanks for replying.

Still no luck :frowning: When I close the app everything resets.

myTable = {} myTable.musicOn = false myTable.soundOn = true myTable.lv1 = false loadsave.saveTable(myTable, "myTable.json") - this thing resets everything

If I remove that loadsave.saveTable(myTable, “myTable.json”) 

table won’t exist and I won’t be able to use it! So the question is still - How to avoid that overwriting.

Thanks!

Okay I think I see your problem.  You need to do something like this in main.lua:

[lua]

local myTable = loadsave(“myTable.json”)

if myTable == nil then

myTable = {}
myTable.musicOn = false
myTable.soundOn = true
myTable.lv1 = false

loadsave.saveTable(myTable, “myTable.json”)

end

[/lua]

You basically need to check and see if there is a saved settings file, if so read it in, if you can’t read it in, assume it doesn’t exist and initialize a new one.

Thanks I will try it! But how I can call that variables(stored at main.lua) from levelselect.lua? I want to use it at this event on levelselect.lua.

local button1Press = function( event ) if (myTable.lv1 == false) then -- Here I want to use variable myTable.lv1 which I made in main.lua end

Thanks!

The easy thing is to make it global, but that isn’t a best practice.  Just leave the “local” off of in front of myTable when you create it.

The next easy thing to do is to add your table to an existing object that lives from scene to scene like the storyboard object.  You can do something like this:

storyboard.myTable = loadsave.loadTable(“myTable.json”)

 

From that point on, in any storyboard scene you can access:  storyboard.myTable.soundOn

I’m doing it by leaving local so table is global now. However it works but still losing the memory when closing the app. I think that 

code in main is not working as it should. I know saving works because it works if I don’t close the game that means something is wrong on loading the table when opening the game again. I will try to mess up with this and see if I can get it working.

  1. if myTable == nil then
  2. myTable = {}myTable.musicOn = falsemyTable.soundOn = truemyTable.lv1 = falseloadsave.saveTable(myTable, “myTable.json”)
  3. end

I got this working! I added loading of table before that if myTable == nil…

and I needed to delete all my game data from phone in settings otherwise there will already be myTable.lv1 = true.

Thank you for help!