Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RickBeniers · Oct 24, 2019 at 05:30 PM · listhighscorebinaryformatter

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);
     }
 }
 
 






playerdataclass.png (13.9 kB)
savesystem-saveloadfunctions.png (51.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges