I have created a height score class by keep track of time. In this class top 5 score are saved in file using Preferences and than they are display on screen.

First I am setting up top5Time array by filling in all zero's. Than I am getting top 5 time from file and fill into top5Time array. Than I am saving in file and displaying the top 5 time on screen.

However there is a bug here. Top5Time gets the data from last run. here is what I mean:

1st time playing - get score of 100 - go to HeighScoreState - update score - display: old data  (*This should be 100 not oldData*)

2nd   time playing - get score of 40  - go to HeighScoreState - update score - display: score 100 (*This should be 40 not 100*)

3rd   time playing - get score of 80  - go to HeighScoreState - update score - display: score 40  (*This should be 80 not 40*)




public class HeighScoreState {

        public static Preferences settings = Gdx.app.getPreferences("settingsFile");
        public newTime;

        public HeighScoreState(int newTime) {
            this.newTime = newTime;


            // fill top 5 data with '0'
            for (int i = 0; i < top5Time.length; i++) {
                top5Time[i] = 0;
            }

            // get top 5 data from file
            for (int i = 0; i < top5Time.length; i++) {
                top5Time[i] = MyGdxGame.settings.getLong(key[i]);
            }

            // save data in file
            for (int i = 0; i < top5Time.length; i++) {
                if (top5Time[i] == 0) {
                    MyGdxGame.settings.putLong(key[i], newTime);
                    break;
                } else if (newTime <= top5Time[i]) {
                    MyGdxGame.settings.putLong(key[i], newTime);
                    break;
               }
          }
           MyGdxGame.settings.flush(); // save data
        }



           public void render() {
            sb.setProjectionMatrix(hudCamera.combined);

            sb.begin();

            // print data from file
            for (int i = 0; i < top5Time.length; i++) {
                font.draw(sb,"score: " + top5Time[i], 20, 60 * i);
            }

            sb.end();
        }
    }

Dani AI

Generated

The root cause is that the array shown in render() is never synchronized with what is written to Preferences. Both and spotted this: the code writes the new value into the Preferences store but leaves top5Time unchanged, so the display keeps showing the old in-memory values. Another possible source of confusion is using different Preferences references (the class declares one but code writes to MyGdxGame.settings); make sure the same Preferences instance is used everywhere.

A safe pattern is: (1) load into an in-memory top-5 structure once, (2) update that structure when a new score arrives (inserting and shifting down to keep order), and (3) persist the entire in-memory list back to Preferences and call flush(). That guarantees the UI reads the canonical, up-to-date array and avoids relying on reloading from disk every frame. Prefer using an explicit "empty" sentinel (for example Long.MAX_VALUE when handling times) or prefs.getLong(key, defaultValue) so empty slots are distinguishable from real zeros.

Example insertion + persist (keeps lower times as better):

// load with sentinel
for (int i = 0; i < top5.length; i++) top5[i] = prefs.getLong(key[i], Long.MAX_VALUE);

// find insert position (smaller time wins)
int pos = top5.length;
for (int i = 0; i < top5.length; i++) if (newTime <= top5[i]) { pos = i; break; }

if (pos < top5.length) {
    for (int j = top5.length - 1; j > pos; j--) top5[j] = top5[j - 1]; // shift
    top5[pos] = newTime;

    // write full list so in-memory == on-disk
    for (int i = 0; i < top5.length; i++) {
        long toSave = (top5[i] == Long.MAX_VALUE) ? 0L : top5[i];
        prefs.putLong(key[i], toSave);
    }
    prefs.flush();
}

Quick checks: confirm the same Preferences object is used for get/put; update the in-memory array before switching to the HighScore state so render() shows the new values; add a simple log of the array after update to verify the flow.

Recommended Answers

All 2 Replies

At line 35 you update the settings OK, but you do not update the top5Time array. When you print the results you print them from that array, so the printout does not include the latest value.

Could it be that when you're showing the top time from top5Time in render() that you haven't read the new data from the file yet? A part of the code seems missing so it's hard to tell, but when you are setting the high score with HeighScoreState() you are not updating the top5Time array which you are using in render() to show the score.

Perhaps if you changed the file updating part to also update the top5Time...

 // save data in file
for (int i = 0; i < top5Time.length; i++) {
    if (top5Time[i] == 0) {
        MyGdxGame.settings.putLong(key[i], newTime);

        // new
        top5Time[i]=newTime;

        break;
    } else if (newTime <= top5Time[i]) {
        MyGdxGame.settings.putLong(key[i], newTime);

        // new
        top5Time[i]=newTime;

        break;
    }
}

..it would be able to access the new time in the render() method without having to reload the preference file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.