compare two tables to record the highest score

Hi,

I’m trying to compare the score [1] and highscore [1] tables to get the highest score and record it in highscore.json but it does not work for me.

Can somebody help me?

thank.

local function onCollision\_1(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 if agro.type == "ball" and hit.type == "exit" then elseif agro.type == "exit" and hit.type == "ball" then initialtime.text = initialtime.text + clockText.text score = {} highscore = {} highscore[1] = initialtime.text score[1] = initialtime.text loadsave.saveTable(score, "score.json", system.DocumentsDirectory) score = loadsave.loadTable("score.json", system.DocumentsDirectory) if score[1] \> highscore[1] then loadsave.saveTable(highscore, "highscore.json", system.DocumentsDirectory) highscore = loadsave.loadTable("highscore.json", system.DocumentsDirectory) else end showpoints = function() storyboard.gotoScene("play", "fade", 800) end timer.performWithDelay(2000, showpoints) end end end
highscore[1] = initialtime.text score[1] = initialtime.text

score[1] and highscore[1] have the same value in your code

Don’t forgot that when collision function is used as a parameter, there are two variables : the self element and the event in second.

If so, write onCollision_1(self,event)

You probably should not be initializing your high score or current score in the middle of your collision.

I would at the very beginning of your program if you detect it’s the first time you’ve run, set highScore to 0 and save it.  Set your current score to 0 at the beginning of the game.

Then in your collision loop, test to see if “score” is greater than “highScore”, if so, update “highScore” and save it. Then display the current score and move on.

Rob

Forgive that I have not answered, I have been moving. I have already solved, I explain (Sorry that my English is from Google, I hope you understand me):

I have a display.newText “initialtime” with which I show the score given by the clockText counter and save it in “score.json” when there is a collision, at the same time I want to save the highest score in “highscore.json” if score [1]> highscore [1]. The error was motivated by comparing text with numbers, to solve it I had to convert the text from “initialtime” to number with string tonumber, I hope you understood me, this is how it fits and works perfectly, only in “highscore” .json “the table would look like this: {” score “: 56} and not like this: {” highscore ": 56}, I do not know how to do it in a different way but it helps. thanks.

 initialtime.text = initialtime.text + clockText.text local s = {} s = loadsave.loadTable("score.json", system.DocumentsDirectory) stringnumber1 = initialtime.text s.score = tonumber(stringnumber1) loadsave.saveTable(s, "score.json", system.DocumentsDirectory) local h = {} h = loadsave.loadTable("highscore.json", system.DocumentsDirectory) if s.score \> h.score then loadsave.saveTable(s, "highscore.json", system.DocumentsDirectory) end

I got it.

 initialtime.text = initialtime.text + clockText.text local s = {} s = loadsave.loadTable("score.json", system.DocumentsDirectory) stringnumber1 = initialtime.text s.score = tonumber(stringnumber1) loadsave.saveTable(s, "score.json", system.DocumentsDirectory) local h = {} h = loadsave.loadTable("highscore.json", system.DocumentsDirectory) if s.score \> h.highscore then stringnumber2 = initialtime.text h.highscore = tonumber(stringnumber2) loadsave.saveTable(h, "highscore.json", system.DocumentsDirectory) end
highscore[1] = initialtime.text score[1] = initialtime.text

score[1] and highscore[1] have the same value in your code

Don’t forgot that when collision function is used as a parameter, there are two variables : the self element and the event in second.

If so, write onCollision_1(self,event)

You probably should not be initializing your high score or current score in the middle of your collision.

I would at the very beginning of your program if you detect it’s the first time you’ve run, set highScore to 0 and save it.  Set your current score to 0 at the beginning of the game.

Then in your collision loop, test to see if “score” is greater than “highScore”, if so, update “highScore” and save it. Then display the current score and move on.

Rob

Forgive that I have not answered, I have been moving. I have already solved, I explain (Sorry that my English is from Google, I hope you understand me):

I have a display.newText “initialtime” with which I show the score given by the clockText counter and save it in “score.json” when there is a collision, at the same time I want to save the highest score in “highscore.json” if score [1]> highscore [1]. The error was motivated by comparing text with numbers, to solve it I had to convert the text from “initialtime” to number with string tonumber, I hope you understood me, this is how it fits and works perfectly, only in “highscore” .json “the table would look like this: {” score “: 56} and not like this: {” highscore ": 56}, I do not know how to do it in a different way but it helps. thanks.

 initialtime.text = initialtime.text + clockText.text local s = {} s = loadsave.loadTable("score.json", system.DocumentsDirectory) stringnumber1 = initialtime.text s.score = tonumber(stringnumber1) loadsave.saveTable(s, "score.json", system.DocumentsDirectory) local h = {} h = loadsave.loadTable("highscore.json", system.DocumentsDirectory) if s.score \> h.score then loadsave.saveTable(s, "highscore.json", system.DocumentsDirectory) end

I got it.

 initialtime.text = initialtime.text + clockText.text local s = {} s = loadsave.loadTable("score.json", system.DocumentsDirectory) stringnumber1 = initialtime.text s.score = tonumber(stringnumber1) loadsave.saveTable(s, "score.json", system.DocumentsDirectory) local h = {} h = loadsave.loadTable("highscore.json", system.DocumentsDirectory) if s.score \> h.highscore then stringnumber2 = initialtime.text h.highscore = tonumber(stringnumber2) loadsave.saveTable(h, "highscore.json", system.DocumentsDirectory) end