Create a scoreboard class and load previous records and save new one
Thu Nov 04 2021 23:30:26 GMT+0000 (Coordinated Universal Time)
Saved by @miguel9177 #c#
class ScoreBoard { // Constructor that takes points and number of records, this constructor will be called if the user doesnt insert the name, and will assing the name to unknown public ScoreBoard(int points, int numberOfRecords) { playerName = "unknown"; playerPoints = points; numberOfSaves = numberOfRecords + 1; } // Constructor that takes all the arguments, this constructor will set the name, points and number of records public ScoreBoard(string name, int points, int numberOfRecords) { playerName = name; playerPoints = points; numberOfSaves = numberOfRecords +1; } //Create a read only variable called playername public string playerName { get; } //Create a read only variable called Player points public int playerPoints { get; } //Create a read only variable called number of saves public int numberOfSaves { get; } } //create this player score ScoreBoard playerScore; [SerializeField] //save the text box that will get the name of the player Text TextNameOfPlayer; public void CallerOfSetPlayerScore(int points) { //call the function SetPlayerScore(points, TextNameOfPlayer.text); } //this function add the points, name, to the player private void SetPlayerScore(int playerPoints_, string playerName_ = "unknown") { //give the currect values of this player score playerScore = new ScoreBoard(playerName_, 50, PlayerPrefs.GetInt("NumberOfItems")); } //this function will load all the points stored public void LoadPlayerScores() { //do a loop trought all the scores for (int i = 0; i < PlayerPrefs.GetInt("NumberOfItems") + 1; i++) { Debug.Log(PlayerPrefs.GetString("Name" + i)); Debug.Log(PlayerPrefs.GetInt("Points" + i)); } } //this function will save all the points public void SavePlayerScore() { //this will set all the variables with nameN (the n is the number of all the saves +1) so it will create a new player pref because it has a number at the end making is name different from the older prefs PlayerPrefs.SetString("Name" + playerScore.numberOfSaves, playerScore.playerName); PlayerPrefs.SetInt("Points" + playerScore.numberOfSaves, playerScore.playerPoints); PlayerPrefs.SetInt("NumberOfItems", playerScore.numberOfSaves); PlayerPrefs.Save(); }
Comments