- Home /
how to create a highscore using serialized binaryfile
I am building a game which has a high score. the data inside the high score is the number of Kills, Time and username that the player has wich are stored in a list. Currently, I got a working system using binary formatter and a serialized text file, however when I load the list and its contents from the binary file only the first created entry into the binary file shows up in the list on the high score. It looks like the binary file is only reading the first line of the file. Question 1: why can I only read the first entry from the binary file? Question 2: how do I insert all data from a binary file into a List?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class HighscoreManager : MonoBehaviour
{
Dictionary<string, Dictionary<string, int> > PlayerScore;
private int Time;
private int Kills;
private int Kills2;
private int Kills3;
private int Kills4;
private int Kills5;
private int Kills6;
private string userName = "User01";
public GameObject SL;
//public ArrayList savedDatas = new ArrayList();
public List<string> loadedstats = new List<string>();
// Start is called before the first frame update
void Start()
{
SetScore("Rick", "Time", 500);
SetScore("Rick", "Kills", 9);
SetScore("BOB", "Time", 1500);
SetScore("BOB", "Kills", 50);
SetScore("BBB", "Deaths", 3);
SetScore("CCC", "Time", 6);
//GetComponent<PlayerController>().LoadPlayer();
//SaveSystems.LoadPlayer();
//savedData.Add(data.savedData);
loadedstats = SaveSystems.LoadPlayer();
int.TryParse(loadedstats[0], out Kills);
int.TryParse(loadedstats[1], out Time);
//int.TryParse(loadedstats[2], out Kills2);
//int.TryParse(loadedstats[3], out Kills3);
//int.TryParse(loadedstats[4], out Kills4);
//int.TryParse(loadedstats[5], out Kills5);
//Kills = (int)data[0];
//Time = (int)data[1];
//userName = "" + data[2];
SetScore(userName, "Time", Time);
SetScore(userName, "Kills", Kills);
Debug.Log("data loaded" + " Time : " + Time + ", Kills : " + Kills + ", username : " + userName);
Debug.Log("List Counted objects = " + loadedstats.Count);
Debug.Log("List capacity = " + loadedstats.Capacity);
}
void Init()
{
if (PlayerScore != null)
{
return;
}
else
{
PlayerScore = new Dictionary<string, Dictionary<string, int>>();
}
}
public int GetScore(string userName, string scoreType)
{
Init();
if (PlayerScore.ContainsKey(userName) == false)
{
return 0;
}
if(PlayerScore[userName].ContainsKey(scoreType) == false)
{
return 0;
}
return PlayerScore[userName][scoreType];
}
public void SetScore(string userName, string scoreType, int value)
{
Init();
if (PlayerScore.ContainsKey(userName) == false)
{
PlayerScore[userName] = new Dictionary<string, int>();
}
PlayerScore[userName][scoreType] = value;
}
public void ChangeScore(string userName, string scoreType, int amount)
{
Init();
int currScore = GetScore(userName, scoreType);
SetScore(userName, scoreType, currScore + amount);
}
public string[] GetPlayerNames(string sortingScoreType)
{
Init();
return PlayerScore.Keys.OrderByDescending( n => GetScore(n, sortingScoreType) ).ToArray();
}
}
above is the highscoreManager class which builds the high score list. below is the saveSystem class which saves and loads data in a binary file.
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Collections;
using System.Collections.Generic;
public static class SaveSystems
{
public static ArrayList savedData = new ArrayList();
public static void SavePlayer(PlayerController player)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(Application.persistentDataPath + "/player.savedData",
FileMode.Append);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
Debug.Log(Application.persistentDataPath + "/player.savedData");
}
public static List<string> LoadPlayer()
{
if (File.Exists(Application.persistentDataPath + "/player.savedData"))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(Application.persistentDataPath + "/player.savedData", FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data.stats;
}
else
{
return null;
}
}
}
[Serializable]
public class PlayerData
{
public List<string> stats = new List<string>();
public PlayerData(PlayerController player)
{
//stats[0] = player.Kills;
stats.Add("" + player.Kills);
//stats[1] = player.Time;
stats.Add("" + player.Time);
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to save a list of MonoBehaviours in a binary file 0 Answers
BinaryFormatter - saving and loading a list containing sprites. 0 Answers
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers
How to make a highscore list? 0 Answers