Loadsave problems

I have a game that uses the loadsave library. There is a .json file in main.lua, which has all the variables I want to save. Most of these variables are booleans, which are for telling whether or not a costume is bought. I also use it to save the money, and high score. 

The problem is, I want to add new costumes. To do so I need to add it to the .json file. But I have this if statement 

[lua] if (user == nil) then --[[insert variables]] end[/lua]

So if I were to add a new costume using the if statement all the variables would be set to their starting default, due to the fact that I would have to set ‘user’ to nil.

I could set it outside the if statement, but then it would set the new costume to false when ever the player exits the game. So when the player buys the new costume (which would set the costume’s boolean to true), the moment he exits the game the costume boolean will get set to false, and the player will have to buy it again, and again, and again, etc. 

I tried setting up a new .json file to add the new costumes, but I can’t figure out how to do it only once every time I want add something new. 

Here’s the code.

[lua]

–this is the test code

local new = loadsave.loadTable(“new.json”)

–new = nil

if (new == nil) then

    new = {}

    new.loaded = false

    new.newItems = false

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

end

if (new.loaded == true) then

    new.newItems = true

end

– working code

–setup save file for user

user = loadsave.loadTable(“user.json”)

–user = nil

if(user == nil) then

–loads variables if user == nil

user = {}

user.money = 0

user.highscore = 0

user.playsound = true

–sets the skin the player is currently using (ninja as default)

user.skin = “ninja”

–boolings telling which skins the player has bought

user.whiteNinja = false

user.girlNinja = false

user.lizardMan = false

user.robot = false

user.worker = false

user.chef = false

user.fighter = false

user.cheese = false

user.pumkin = false

    --will not load new items if ‘user’ equals ‘nil’

    user.newItems = false

    --saves everything

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

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

end

–load new objects 

if (new.newItems == true) then

    print(“hi”)

    user.pumkin = false

    new.newItems = false

    new.loaded = false

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

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

end

– Go to the menu screen

composer.gotoScene( “menu” )

[/lua]

Hi @ec0,

You can store only information about items bought by user. That is why: 

Example:

local user = {} user.fighter = false if user.fighter then     print( 'user.fighter is true' ) else     print( 'user.fighter is false' ) end -- Note that user.chief is not defined if user.chef then     print( 'user.chef is true' ) else     print( 'user.chef is false' ) end

user.fighter is equal false but user.chef is nil. In Lua anything that is not false (either nil or false) is true.

Have a nice day:)

ldurniat

I’m confused. I think I understand what your saying, but I don’t exactly know how it works with my code. 

Anyways, I think I fixed my problem. I created another new.json file, called it new2, and I’m going to swap it and new1.json between updates, setting the previous new.json to nil.    

Hi @ec0,

You can store only information about items bought by user. That is why: 

Example:

local user = {} user.fighter = false if user.fighter then     print( 'user.fighter is true' ) else     print( 'user.fighter is false' ) end -- Note that user.chief is not defined if user.chef then     print( 'user.chef is true' ) else     print( 'user.chef is false' ) end

user.fighter is equal false but user.chef is nil. In Lua anything that is not false (either nil or false) is true.

Have a nice day:)

ldurniat

I’m confused. I think I understand what your saying, but I don’t exactly know how it works with my code. 

Anyways, I think I fixed my problem. I created another new.json file, called it new2, and I’m going to swap it and new1.json between updates, setting the previous new.json to nil.