Question about JSON

Hi…

I seem to have a problem with JSON -

i’m trying to save the highscores in my game with it…

I’m doing everything according to how it should be - and it work. In the simulator it saves the scores perfectly, even when I restart it, it still knows what the old scores were…

On the device however, the scores are refreshed every time I restart the app…

Is there something I’m missing?

Where are you saving the scores to?   system.ResourcesDirectory or system.DocumentsDirectory?  Do you by any chance have a difference in the filenames as far as case sensitivity is concerned?

I’m saving it to system.DocumentsDirectory.

and as far as the file name it’s exactly the same in all its references.

I also used the bit of code you published regarding saving with JSON, and it still didn’t work.

this is what I wrote in the main.lua when the game starts -

path= system.pathForFile(“LMData.json”, system.DocumentsDirectory)
file=io.open(path, “r”)
Data={}

this is what I wrote when I saved the scores-

    if(isArcade) then
        Data.lastscore_arcade= gameScore
       local stringified=json.encode(Data)
       file= io.open(path,“w”)
       file:write(stringified)
       io.close(file)
       newHighScore=false
            if(Data.highscore_arcade~=nil) then
                if(Data.lastscore_arcade>Data.highscore_arcade) and (Data.lastscore_arcade>0) then
                        Data.highscore_arcade = Data.lastscore_arcade
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                end    
            else
                        Data.highscore_arcade = Data.lastscore_arcade
                        if(Data.highscore_arcade>0) then
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                else
                Data.highscore_arcade=0
                newHighScore=false
            end
        end
     else
        Data.lastscore_time= gameScore
       local stringified=json.encode(Data)
       file= io.open(path,“w”)
       file:write(stringified)
       io.close(file)
       newHighScore=false
            if(Data.highscore_time~=nil) then
                if(Data.lastscore_time>Data.highscore_time) and (Data.lastscore_time>0) then
                        Data.highscore_time = Data.lastscore_time
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                end    
            else
                        Data.highscore_time = Data.lastscore_time
                        if(Data.highscore_time>0) then
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                else
                Data.highscore_time=0
                newHighScore=false
            end
        end
    end

I tried both with the save/load  functions you suggested in the article, and without them, but it made no difference

Where do you actually read the data?  I only see you saving it and initializing an empty data table at the top.

Yea figured it out just now…

been using several methods which didnt work and gave me errors, but  finally tried this one and it did the trick -

path= system.pathForFile(“LMData.json”, system.DocumentsDirectory)
local contents = “”
Data = {}
file = io.open( path, “r” )
    if file then
         local contents = file:read( “*a” )
         Data = json.decode(contents);
         io.close( file )
 end

thank you :slight_smile:

Where are you saving the scores to?   system.ResourcesDirectory or system.DocumentsDirectory?  Do you by any chance have a difference in the filenames as far as case sensitivity is concerned?

I’m saving it to system.DocumentsDirectory.

and as far as the file name it’s exactly the same in all its references.

I also used the bit of code you published regarding saving with JSON, and it still didn’t work.

this is what I wrote in the main.lua when the game starts -

path= system.pathForFile(“LMData.json”, system.DocumentsDirectory)
file=io.open(path, “r”)
Data={}

this is what I wrote when I saved the scores-

    if(isArcade) then
        Data.lastscore_arcade= gameScore
       local stringified=json.encode(Data)
       file= io.open(path,“w”)
       file:write(stringified)
       io.close(file)
       newHighScore=false
            if(Data.highscore_arcade~=nil) then
                if(Data.lastscore_arcade>Data.highscore_arcade) and (Data.lastscore_arcade>0) then
                        Data.highscore_arcade = Data.lastscore_arcade
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                end    
            else
                        Data.highscore_arcade = Data.lastscore_arcade
                        if(Data.highscore_arcade>0) then
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                else
                Data.highscore_arcade=0
                newHighScore=false
            end
        end
     else
        Data.lastscore_time= gameScore
       local stringified=json.encode(Data)
       file= io.open(path,“w”)
       file:write(stringified)
       io.close(file)
       newHighScore=false
            if(Data.highscore_time~=nil) then
                if(Data.lastscore_time>Data.highscore_time) and (Data.lastscore_time>0) then
                        Data.highscore_time = Data.lastscore_time
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                end    
            else
                        Data.highscore_time = Data.lastscore_time
                        if(Data.highscore_time>0) then
                        local stringified=json.encode(Data)
                        file= io.open(path,“w”)
                        file:write(stringified)
                        io.close(file)
                        newHighScore=true
                else
                Data.highscore_time=0
                newHighScore=false
            end
        end
    end

I tried both with the save/load  functions you suggested in the article, and without them, but it made no difference

Where do you actually read the data?  I only see you saving it and initializing an empty data table at the top.

Yea figured it out just now…

been using several methods which didnt work and gave me errors, but  finally tried this one and it did the trick -

path= system.pathForFile(“LMData.json”, system.DocumentsDirectory)
local contents = “”
Data = {}
file = io.open( path, “r” )
    if file then
         local contents = file:read( “*a” )
         Data = json.decode(contents);
         io.close( file )
 end

thank you :slight_smile: