[Resolved] Updating score in multiple files

So I have a question that seems to be eluding me. I am trying to pass variables between scenes, more specifically a score. Here’s some pseudocode:

On the levelSelection.lua scene, the list of levels are here to choose from. There is a highScore variable set for each one of these levels, set to 0. In level1.lua a variable “score” is created. Once the level objective is completed, I would like to pass “score” from level1.lua, back to levelSelection.lua and update the level1 highScore variable there.

Here’s what I was thinking:

[lua]–levelSelection.lua
_G.level1HighScore = 0;
testText = display.newText (level1HighScore, 140, 300, “Arial”, 20)

–level1.lua
score = 100;
levelSelection.level1HighScore = score;[/lua]

Something like that. (I hope this is not too broad). This does not work though and it does not update. I think it is because every time I run the levelSelection.lua scene, it resets level1HighScore to 0. Can anyone help me out? Thanks in advance! [import]uid: 50511 topic_id: 24879 reply_id: 324879[/import]

Reloading the scene that sets the highscore to 0 will, yes, set it to 0. (Naturally.)

Have you considered saving the score then loading it in levelselection rather than setting it at 0 each time it reloads the scene, maybe?

Or perhaps only setting it as 0 if level1 highscore is nil? [import]uid: 52491 topic_id: 24879 reply_id: 100953[/import]

Thanks Peach. It’s funny you would respond because I recently found a way to update a score variable in an easy way, on your website! I actually changed my coding to implement your ego system so I can save variables in a text file. It took me a little bit to implement it into my code but I can explain why it worked for me.

When you create and save a .txt file, each scene can call that .txt and make and modify changes to it. So my solution was to create a “lvl1highscore.txt” on my levelSelection.lua scene, and then in level1.lua I can change and modify the value in lvl1highscore.txt.

I would like to post a link to your page on your site so if anyone looking for a similar solution comes here, they can be directed to your website where I was able to find my answer. I hope its okay!

Scoring solution can be found here:
http://techority.com/2012/01/13/easy-saving-of-highscore-in-corona-sdk-apps/ [import]uid: 50511 topic_id: 24879 reply_id: 101023[/import]

Hey, that’s great - very pleased to hear that :slight_smile:

Thanks for the link too, hopefully it will indeed help others.

Marking as resolved! [import]uid: 52491 topic_id: 24879 reply_id: 101176[/import]