0
\$\begingroup\$

I have 5 scripts to make the scoreboard. But I don't know how to order the list (the scores which are stores in a list) by newest added. Can someone please help me? Thanks :)

First Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using TMPro;

public class RowUi : MonoBehaviour
{
    public TextMeshProUGUI score;
}

Second Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;

public class ScoreManager : MonoBehaviour
{
    private ScoreData sd;

    private void Awake()
    {
        var json = PlayerPrefs.GetString("scores", "{}");
        sd = JsonUtility.FromJson<ScoreData>(json);
    }

    public IEnumerable<Score> GetHighScore()
    {
        // **THIS IS WHERE THE SCORE IS ORDERD BY DESCENDING** //
        return sd.scores.OrderByDescending(x => x.score);
    }

    public void AddScore(Score score)
    {
        sd.scores.Add(score);
    }

    private void OnDestroy()
    {
        SaveScore();
    }

    public void SaveScore()
    {
        var json = JsonUtility.ToJson(sd);
        PlayerPrefs.SetString("scores", json);
    }
}

Third Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ScoreUi : MonoBehaviour
{
    public static ScoreUi instance;

    public RowUi rowUi;
    public ScoreManager scoreManager;

    private void Start()
    {
        instance = this;

        if(gameObject.activeInHierarchy == true)
        {
            ScoreUi.instance.AddScoreToBoard();
        }

        var scores = scoreManager.GetHighScore().ToArray();
        for(int i = 0; i < scores.Length; i++)
        {
            var row = Instantiate(rowUi, transform).GetComponent<RowUi>();
            row.score.text = scores[i].score.ToString();
        }
    }

    public void AddScoreToBoard()
    {
        scoreManager.AddScore(new Score(UserInput.instance.score));
    }
}

Fourth Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class ScoreData
{
    public List<Score> scores;

    public ScoreData()
    {
        scores = new List<Score>();
    }
}

Fifth Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class Score
{
    public int score;

    public Score(int score)
    {
        this.score = score;
    }
}

I hope I explained this well enough and if you need extra info, just tell me :)

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

First, we can add a date field to the score type. We'll make it a long so it's easy to serialize, but also add a getter to access it as a DateTime object for when that's more convenient to read.:

[Serializable]
public class Score
{
    public int score;
    public long timestamp;
    public System.DateTime dateAdded { 
        get { 
            return System.DateTime.FromFileTimeUtc(timeStamp);
        }
    }

    public Score(int score)
    {
        this.score = score;
        // Assign our time stamp whenever logging a new score.
        timestamp = System.DateTime.Now.ToFileTimeUtc();
    }
}

Then we can order our scores by date the same way we order them by value:

public IEnumerable<Score> GetHighScore()
{
    // **THIS IS WHERE THE SCORE IS ORDERED BY DESCENDING** //
    return sd.scores.OrderByDescending(x => x.score);
}


public IEnumerable<Score> GetLatestScore()
{
    // **THIS IS WHERE THE SCORE IS ORDERED FROM NEWEST TO OLDEST** //
    return sd.scores.OrderByDescending(x => x.timeStamp);
}

To erase the saved scores, just reset your score data before saving:

public void ClearAllSavedScores() {
    // Replace our score data with a new empty list of scores.
    sd = new ScoreData();
    // Save this empty collection to replace what's in PlayerPrefs.
    SaveScore();
}

As you can see, all of this is a pretty trivial modification of what you already had, plus use of the standard DateTime type you can find fully explained in the Microsoft .NET documentation. If you struggle with this, then you may want to work your way through some more beginner programming tutorials to learn the ins and outs of the language.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .