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 auzzyWiz · Nov 29, 2017 at 10:51 AM · arrayssave datasave-to-filebinarywriter

Saving Player Data for Multiple Scenes/Levels

I am attempting to modify the Unity Trash Cat Walkthrough so that it will save information for multiple levels instead of just one general leaderboard for the entire game.

The issue is when I am trying to read the fastest times entries for each level from the file that was previously created. I am receiving the error: EndOfStreamException: Failed to read past end of stream.

This is happening at these lines:

 entry.name = r.ReadString();
 entry.time = r.ReadSingle();
 entry.score = r.ReadInt32();
 entry.sceneNum = r.ReadInt32();
 entry.bonusCount = r.ReadInt32();


I, however, am not sure if this is happening bc I am writing to the file incorrectly or reading... or BOTH. Is this even a good way to go about this? Am I looking at this wrong?

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using UnityEngine;
 
 public struct FastestTimeEntry : System.IComparable<FastestTimeEntry>
 {
     public int sceneNum;
     public string name;
     public float time;
     public int score;
     public int bonusCount;
 
     public int CompareTo(FastestTimeEntry other)
     {
         // We want to sort from  lowest to highest time.
         return time.CompareTo(other.time);
     }
 }
 
 

 public class PlayerProgress{
 
     static protected PlayerProgress m_Instance;
     static public PlayerProgress instance { get { return m_Instance; } }
     protected string saveFile = "";
     static int s_Version = 1; 
     public int tokens;
     public int premium;
     public int unlockedSceneNum;
      public bool licenseAccepted;
 
      public string previousName = "Game Champ";
 
     public List<FastestTimeEntry>[] fastestTimes = new List<FastestTimeEntry>[GameControllerMain.sceneNames.Length];
 
     public int ftueLevel = 0;
     public int rank = 0;
 
 
     public int totalBonusCount = 0;
     public int totalScore;
     
     // Fastest Time management
 
     public int GetTimePlace(float time, int sceneNum)
     {
         FastestTimeEntry entry = new FastestTimeEntry();
         entry.time = time;
         entry.sceneNum = sceneNum;
 
 
         int index = fastestTimes[sceneNum].FindIndex(c => c.time == time);
 
         Debug.Log("GetTimePlace Index:"+ index);
 
         return index < 0 ? (~index) : index;
     }
 
 
 
 
     public void InsertTime(float time, int score, int sceneNum, int bonusCount)
     {
         Debug.Log("Inserting time...");
         FastestTimeEntry entry = new FastestTimeEntry();
         entry.time = time;
         entry.score = score;
         entry.sceneNum = sceneNum;
         entry.bonusCount = bonusCount;
 
         fastestTimes[sceneNum].Insert(GetTimePlace(time, sceneNum), entry);
 
         // Keep only the 10 best scores.
         while (fastestTimes[sceneNum].Count > 10)
             fastestTimes[sceneNum].RemoveAt(fastestTimes[sceneNum].Count - 1);
     }
 
 
 
 
     // File management
 
     static public void Create()
     {
         if (m_Instance == null)
         {
             m_Instance = new PlayerProgress();
 
         }
 
         m_Instance.saveFile = Application.persistentDataPath + "/save.bin";
 
         if (File.Exists(m_Instance.saveFile))
         {
             // If we have a save, we read it.
             m_Instance.Read();
         }
         else
         {
             // If not we create one with default data.
             NewSave();
         }
     }
 
     static public void NewSave()
     {
         Debug.Log("This is a new save!");
 
         m_Instance.tokens = 0;
         m_Instance.premium = 0;
 
         m_Instance.totalBonusCount = 0;
         m_Instance.totalScore = 0;
 
         m_Instance.ftueLevel = 0;
         m_Instance.rank = 0;
 
         for (int i = 0; i < GameControllerMain.sceneNames.Length; ++i){
             m_Instance.fastestTimes[i] = new List<FastestTimeEntry>();
         }
 
         m_Instance.Save();
     }
 
     public void Read()
     {
         BinaryReader r = new BinaryReader(new FileStream(saveFile, FileMode.Open));
 
         int ver = r.ReadInt32();
 
         tokens = r.ReadInt32();
 
 
         // Save contains the version they were written with. If data are added bump the version & test for that version before loading that data.
         if(ver >= 1)
         {
             premium = r.ReadInt32();
 
             for (int i = 0; i < GameControllerMain.sceneNames.Length; ++i)
             {
                 fastestTimes[i] = new List<FastestTimeEntry>();
                 fastestTimes[i].Clear();
                 int count = r.ReadInt32();
                 for (int j = 0; j < count; ++j)
                 {
                     Debug.Log("Reading a fastest time entry");
                     FastestTimeEntry entry = new FastestTimeEntry();
 
                     entry.name = r.ReadString();
                     entry.time = r.ReadSingle();
                     entry.score = r.ReadInt32();
                     entry.sceneNum = r.ReadInt32();
                     entry.bonusCount = r.ReadInt32();
 
                     fastestTimes[i].Add(entry);
                 }
 
             }
 
 
             previousName = r.ReadString();
             licenseAccepted = r.ReadBoolean();
             ftueLevel = r.ReadInt32();
             rank = r.ReadInt32();
 
             totalBonusCount = r.ReadInt32();
             totalScore = r.ReadInt32();
         }
 
 
         r.Close();
     }
 
     public void Save()
     {
         BinaryWriter w = new BinaryWriter(new FileStream(saveFile, FileMode.OpenOrCreate));
 
         w.Write(s_Version);
         w.Write(tokens);
         w.Write(premium);
     
         // Write fastestTimes
         for (int i = 0; i < GameControllerMain.sceneNames.Length; ++i){
             w.Write(fastestTimes[i].Count);
             for(int j = 0; j < fastestTimes[i].Count; ++i)
             {
                 //w.Write(fastestTimes[i].name);
                 w.Write(fastestTimes[i][j].time);
                 w.Write(fastestTimes[i][j].sceneNum);
                 w.Write(fastestTimes[i][j].score);
                 w.Write(fastestTimes[i][j].bonusCount);
             }
         }
 
 
         // Write name.
         w.Write(previousName);
 
         w.Write(licenseAccepted);
 
         w.Write(ftueLevel);
         w.Write(rank);
 
         w.Write(totalBonusCount);
         w.Write(totalScore);
 
         w.Close();
     }
 
 
 
 
 }



Comment
Add comment · Show 1
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
avatar image auzzyWiz · Nov 29, 2017 at 12:53 AM 0
Share

After discussing with a buddy he thinks it may have to do with the fact that I am writing empty list items initially which may be tripping up the reader because there is nothing there.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by auzzyWiz · Dec 02, 2017 at 12:31 PM

So, after finding out it was the empty/null list tripping me up above I have decided to go this route which is working:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using UnityEngine;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 
 
 [Serializable]
 public struct FastestTimeEntry : System.IComparable<FastestTimeEntry>
 {
     public int sceneNum;
     public string name;
     public float time;
     public int score;
     public int bonusCount;
 
     public int CompareTo(FastestTimeEntry other)
     {
         // We want to sort from  lowest to highest time.
         return time.CompareTo(other.time);
     }
 }
 
 
 public class PlayerProgress{
 
     static protected PlayerProgress m_Instance;
     static public PlayerProgress instance { get { return m_Instance; } }
     protected string saveFile = "";
     static int s_Version = 1; 
     public int coins;
         public int premium;
     public int unlockedSceneNum;
      public bool licenseAccepted;
 
      public string previousName = "Best Bud";
 
     public List<FastestTimeEntry>[] fastestTimes = new List<FastestTimeEntry>[GameControllerMain.sceneNames.Length];
 
 
     public int ftueLevel = 0;
     public int rank = 0;
 
 
     public int totalBonusCount = 0;
     public int totalScore;
  
     
     // Fastest Time management
 
     public int GetTimePlace(float time, int sceneNum)
     {
         Debug.Log("Getting time...");
         FastestTimeEntry entry = new FastestTimeEntry();
         entry.time = time;
         entry.sceneNum = sceneNum;
 
         int index = fastestTimes[sceneNum-1].BinarySearch(entry);
 
         Debug.Log("GetTimePlace Index:"+ index);
 
         return index < 0 ? (~index) : index;
     }
 
 
 
 
     public void InsertTime(float time, int score, int sceneNum, int bonusCount)
     {
         Debug.Log("Inserting time...");
         FastestTimeEntry entry = new FastestTimeEntry();
         entry.time = time;
         //entry.name = name;
         entry.score = score;
         entry.sceneNum = sceneNum;
         entry.bonusCount = bonusCount;
 
         int timePlace = GetTimePlace(time, sceneNum);
         fastestTimes[sceneNum-1].Insert(timePlace, entry);
 
         // Keep only the 10 best scores.
         while (fastestTimes[sceneNum-1].Count > 10)
             fastestTimes[sceneNum-1].RemoveAt(fastestTimes[sceneNum-1].Count - 1);
     }
 
 
 
     // File management
     static public void Create()
     {
         if (m_Instance == null)
         {
             m_Instance = new PlayerProgress();
 
         }
 
         m_Instance.saveFile = Application.persistentDataPath + "/save.bin";
 
         if (File.Exists(m_Instance.saveFile))
         {
             m_Instance.Read();
         }
         else
         {
             NewSave();
         }
 
     }
 
     static public void NewSave()
     {
         
 
         m_Instance.coins = 0;
         m_Instance.premium = 0;
 
         m_Instance.totalBonusCount = 0;
         m_Instance.totalScore = 0;
 
         m_Instance.ftueLevel = 0;
         m_Instance.rank = 0;
 
         for (int i = 0; i < GameControllerMain.sceneNames.Length; ++i){
             m_Instance.fastestTimes[i] = new List<FastestTimeEntry>();
         }
 
         m_Instance.Save();
     }
 
     public void Read()
     {
         IFormatter formatter = new BinaryFormatter();
             using (Stream stream = new FileStream( saveFile, FileMode.Open, FileAccess.Read, FileShare.Read))
             {
                 m_Instance = (PlayerProgress)formatter.Deserialize(stream);
             }
     }
 
     public void Save()
     {
         IFormatter formatter = new BinaryFormatter();
             using (Stream stream = new FileStream(saveFile, FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 
                 formatter.Serialize(stream, m_Instance);
             }
     }
 
 
 }
Comment
Add comment · Share
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
avatar image
0

Answer by text23d · Nov 30, 2017 at 07:01 PM

what I did for my saving/loading, is to make a class, and store all savable/loadable variables inside it. Then just save this class (serialize) into a binary file. and read a file similarly (deserialize), and restore each variable from file (class)...

Comment
Add comment · Show 1 · Share
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
avatar image auzzyWiz · Nov 30, 2017 at 07:57 PM 0
Share

Thank you for the reply @text23d. That is actually what I am in the process of doing now. :)

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

72 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

Related Questions

How to save an instance of a class(Make a custom component?) from the editor window 0 Answers

Racing Game: Saving and loading replay data? 1 Answer

How to save custom binary files to iCloud? 0 Answers

Cannot save(serialize) a double array of a custom class 1 Answer

Saves on android, could not find file 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