JSON bug

hi,

I just satrted this app to learn JSON and I cant get it to work.

heres the code :

local affichageNiveau local numNiveau = 1 local W\_ = display.contentWidth /2 local H\_ = display.contentHeight /2 local nombreDeNiveau = 300 --code function main() local numNiveauFilename = "numniveau.data" numNiveau = loadValue(numNiveauFilename) numNiveau = tonumber(numNiveau) for i = 1, nombreDeNiveau do if numNiveau ~= i then print("in line 43") numNiveau = 1 saveValue(numNiveauFilename, tostring(numNiveau)) break end end addLevelScreen() end function addLevelScreen() affichageNiveau = display.newText(tostring(numNiveau), W\_, H\_, native.systemFont, 56) affichageNiveau:setFillColor(1,0,1) affichageNiveau:addEventListener("tap", addValue) end function addValue() print("addValue") print (numNiveau) numNiveau = tonumber(numNiveau) numNiveau = numNiveau + 1 affichageNiveau:removeEventListener("tap", addValue) affichageNiveau:removeSelf() addLevelScreen() local numNiveauFilename = "numniveau.data" saveValue(numNiveauFilename, tostring(numNiveau)) end --saving function using json function saveValue(strFilename, strValue) print("in saveValue") local theFile = strFilename local theValue = strValue local path = system.pathForFile( theFile, system.DocumentsDirectory ) local file = io.open( path, "w+" ) if file then -- If the file exists, then continue. Another way to read this line is 'if file == true then'. file:write(theValue) -- This line will write the contents of the table to the .json file. io.close(file) -- After we are done with the file, we close the file. return true -- If everything was successful, then return true end end --loading function using json -- Load specified encrypted file, or create new file if it does not exist function loadValue(strFilename) print("in loadValue") local theFile = strFilename local path = system.pathForFile( theFile, system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then -- If file exists, continue. Another way to read this line is 'if file == true then'. local contents = file:read( "\*a" ) -- read all contents of file into a string io.close( file ) -- Since we are done with the file, close it. return contents -- Return the table with the JSON contents else return '' -- Return nothing end end main()

please help

What is the issue?  Can you describe the bug better? 

Sorry

When you restart the app the counter will reset where as I want it to stay the same as before.

Tell me if you need anything else.

First of all, you’re not using JSON at all.  You are simply reading/writing a number value.  JSON is for saving and reading table data in a simple safe manner.

But still, you have the basic right idea for how to save and load data.  However, I think you’re logic may need some changes.  I would suggest putting in some print statements to test the values you get back from your various functions.  For instance in the main() function, you loop from 1 to 300 and if the index of that loop is not equal to the value you read you set it to one, save it and abort the loop.   Then it adds the addLevelScreen().  Later your touch handler seems to add the addlevelScreen on every touch. 

I would load up the code with print statements printing out the value of numNiveau and various places and other variables and see what’s really going on inside the program.

Rob

Thanks for the help.

I didn’t write the save/load functions myself, I got that part in my book on how to use it.

Honestly I don’t really understand the function. Could you please explain the function and point out the mistakes I did.

I’m 13 and I don’t know anybodie who codes so it’s kinda hard to get help.

leon

I can try and explain what’s going on in the two functions, but as far as whats going on with your app, it’s part of your learning and discovery to help figure that out. Learning to trace what’s going on is an important skill to develop.

Let’s start with this first function:

function saveValue(strFilename, strValue)     print("in saveValue")     local theFile = strFilename     local theValue = strValue     local path = system.pathForFile( theFile, system.DocumentsDirectory )     local file = io.open( path, "w+" )     if file then -- If the file exists, then continue. Another way to read this line is 'if file == true then'.         file:write(theValue) -- This line will write the contents of the table to the .json file.         io.close(file) -- After we are done with the file, we close the file.         return true -- If everything was successful, then return true     end end

This starts by declaring a function, a block of reusable code named “saveValue”.  Because this code is designed to be used multiple times, it’s setup so you the programmer can tell it various values to use.  In this case it needs two values:  The filename to save the data to, and the value to save to the file.  The developer who created it prefixed the variable names with “str” to indicate that it’s expecting “string” values, not number, or tables or other Lua data types.  When you call this function, you pass it the file name and the value to save.  For instance:

saveValue(“score.txt”, tostring(100))

Lua is smart, it would convert a number 100 to a string “100” for you for this purpose.  Once inside the function, it makes local copies of the parameters you passed in (not really necessary in this case).  Then it generates a filename that’s a combination of the filename you passed in and the app sandbox location system.DocumentsDirectory.  This is a place in your app where you can write files too.

Next, it opens the file for writing.  Next it checks to make sure it could really open the file.  If it was successful, it then writes out the value you passed as the second parameter.  Finally it closes the file and returns to you.

The other function does the opposite.  It pretty much does the same thing, except that it reads the file if it exists, and returns whatever it read to your program.  If it failed to read the file, it returns an empty string.

In the case of the reading function, it only needs to be told the name of the file to read.  When you call the function, you assign the value to a variable to get the results:

local myVariable = loadValue(“someFilenameOfYourChoice.txt”)

If myVariable == “” then

    – it didn’t read anything

else

   print(myVariable) – myVariable holds the contents of the file someFilenameOfYourChoice.txt

end

Hope that helps

Rob

Ok I see. So are the functions correct, is there anything I’m doing wrong when calling the functions ? I tried putting print statements printing out the values and I still don’t where I messed up.

Thanks for your help I just figured it out and my code is working perfectly.

What is the issue?  Can you describe the bug better? 

Sorry

When you restart the app the counter will reset where as I want it to stay the same as before.

Tell me if you need anything else.

First of all, you’re not using JSON at all.  You are simply reading/writing a number value.  JSON is for saving and reading table data in a simple safe manner.

But still, you have the basic right idea for how to save and load data.  However, I think you’re logic may need some changes.  I would suggest putting in some print statements to test the values you get back from your various functions.  For instance in the main() function, you loop from 1 to 300 and if the index of that loop is not equal to the value you read you set it to one, save it and abort the loop.   Then it adds the addLevelScreen().  Later your touch handler seems to add the addlevelScreen on every touch. 

I would load up the code with print statements printing out the value of numNiveau and various places and other variables and see what’s really going on inside the program.

Rob

Thanks for the help.

I didn’t write the save/load functions myself, I got that part in my book on how to use it.

Honestly I don’t really understand the function. Could you please explain the function and point out the mistakes I did.

I’m 13 and I don’t know anybodie who codes so it’s kinda hard to get help.

leon

I can try and explain what’s going on in the two functions, but as far as whats going on with your app, it’s part of your learning and discovery to help figure that out. Learning to trace what’s going on is an important skill to develop.

Let’s start with this first function:

function saveValue(strFilename, strValue)     print("in saveValue")     local theFile = strFilename     local theValue = strValue     local path = system.pathForFile( theFile, system.DocumentsDirectory )     local file = io.open( path, "w+" )     if file then -- If the file exists, then continue. Another way to read this line is 'if file == true then'.         file:write(theValue) -- This line will write the contents of the table to the .json file.         io.close(file) -- After we are done with the file, we close the file.         return true -- If everything was successful, then return true     end end

This starts by declaring a function, a block of reusable code named “saveValue”.  Because this code is designed to be used multiple times, it’s setup so you the programmer can tell it various values to use.  In this case it needs two values:  The filename to save the data to, and the value to save to the file.  The developer who created it prefixed the variable names with “str” to indicate that it’s expecting “string” values, not number, or tables or other Lua data types.  When you call this function, you pass it the file name and the value to save.  For instance:

saveValue(“score.txt”, tostring(100))

Lua is smart, it would convert a number 100 to a string “100” for you for this purpose.  Once inside the function, it makes local copies of the parameters you passed in (not really necessary in this case).  Then it generates a filename that’s a combination of the filename you passed in and the app sandbox location system.DocumentsDirectory.  This is a place in your app where you can write files too.

Next, it opens the file for writing.  Next it checks to make sure it could really open the file.  If it was successful, it then writes out the value you passed as the second parameter.  Finally it closes the file and returns to you.

The other function does the opposite.  It pretty much does the same thing, except that it reads the file if it exists, and returns whatever it read to your program.  If it failed to read the file, it returns an empty string.

In the case of the reading function, it only needs to be told the name of the file to read.  When you call the function, you assign the value to a variable to get the results:

local myVariable = loadValue(“someFilenameOfYourChoice.txt”)

If myVariable == “” then

    – it didn’t read anything

else

   print(myVariable) – myVariable holds the contents of the file someFilenameOfYourChoice.txt

end

Hope that helps

Rob

Ok I see. So are the functions correct, is there anything I’m doing wrong when calling the functions ? I tried putting print statements printing out the values and I still don’t where I messed up.

Thanks for your help I just figured it out and my code is working perfectly.