Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MordazaH · Jun 16, 2018 at 04:48 AM · arraysaveloadembed

What is best way to save several arrays into a file?

Hi all, I have been fighting this issue for a couple of days and now I have come to think that maybe Im doing something wrong here because errors seem to always remain no matter what I try, I searched here and in other sites and couldnt find the right answer for me. Im trying to save several hi-score tables into a file and later retrieve them of course.

For file storage I created one class that holds only and array or ints and an array of strings, also created a class to store all the hi-score tables called ClaseGuardado:

     [System.Serializable]
     class Claseguardado
     {
         public puntoNombre []arrptonmbr;
     }
     [System.Serializable]
     class puntoNombre
     {
         public int[] pts;
         public string[] nombres;
     }

So I wrote save and load functions like any other save and load function explained in many sites and threads you know using Binary formatter and serialize or deserialize on the object that holds the data (an instance of ClaseGuardado that has been filled with the data or that we get the data from), then my doubt is, this method is compatible with embedded arrays like these? because I always get errors like the file is empty or the object is not assigned or similar, but I have used this method to save and load simple data before and always worked, and I cant see the reason why its not working aside from the fact that maybe serialize/deserialize cant work with this type of structures. I think it might be as said Im using this when it works only on simple type of data, not something like arrays embedded maybe, anyway here is the code Im using for saving (arrminigames holds the hi scores tables in the game so I have them all in that array), "destino" is the filename destination, I initialize it at the beginning to change only in one place. Hi-scores tables hold a total of 10 names and scores just in case there is any doubt about this:

 public void guardar()
     {
         int i,j;
         BinaryFormatter bf;
         FileStream file;
         Claseguardado cguard;            
 
         if (File.Exists(destino))
         {
             file = File.OpenWrite(destino);
         }
         else
         {
             file = File.Create(destino);
         }    
 
         bf = new BinaryFormatter();            
 
         cguard = new Claseguardado();
         cguard.arrptonmbr = new puntoNombre[cantiJuegos];
         for (i = 0; i < cantiJuegos; i++)
         {
             cguard.arrptonmbr[i].pts = new int[10];
             cguard.arrptonmbr[i].nombres = new string[10];
             for (j=0;j<10;j++)
             {
                 cguard.arrptonmbr[i].pts[j] = arrminigames[i].arrpuntos[j];
                 cguard.arrptonmbr[i].nombres[j] = arrminigames[i].arrnombresHiScores[j];
             }
         }            
         bf.Serialize(file, cguard);
 
         file.Close();
     }

And this is the one to load:

 public void carga()
     {
         int i, j;        
         BinaryFormatter bf;
         FileStream file;
         Claseguardado cguard;
             
         if (File.Exists(destino))
         {    
             bf = new BinaryFormatter();                
             file = File.OpenRead(destino);    
             cguard = (Claseguardado)bf.Deserialize(file);
             file.Close();

             for (i = 0; i < cantiJuegos; i++)
             {
                 for (j = 0; j < 10; j++)
                 {
                     arrminigames[i].arrpuntos[j] = cguard.arrptonmbr[i].pts[j];
                     arrminigames[i].arrnombresHiScores[j]= cguard.arrptonmbr[i].nombres[j];
                 }
             }
         }        
     }


Well as said I appreciate any clue about this issue or if you can direct me the right direction like there is other method to save this type of data or something, I would prefer to use same method of course but only if it can work with this structure i have here.

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 madhav-aspiration · Jun 16, 2018 at 05:49 AM 0
Share

Json use the jSon this is best to save array

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

103 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

Related Questions

Saving/Loading variables 1 Answer

SerializationException: Type UnityEngine.Vector3 is not marked as Serializable. 1 Answer

Save and load arrays into an XML file 1 Answer

Save Multiple GameObject with XML 1 Answer

Why can't I save an array? 1 Answer


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