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 Sorade · May 10, 2017 at 05:49 PM · serializationsaveloadsavegamediskspace

Save Game loading Deserialization from disk

Hi all,

I've been pulling my hairs out over the following for a couple of days now. I managed to make it work fine for saving data to disc but can't seem to get the reverse to work.

I want to load back from the disc a file and deserialize it into:

 public KeyValuePairLists<bool> boolKeyValuePairLists = new KeyValuePairLists<bool> ();

My loading method is:

     public void LoadFromDisk(){
             //the errors send back to this line but I can't see any wrong arguments ...
         Serializer.Deserialize (this.name + "Bool.dat", boolKeyValuePairLists);
     }

Followed by the Serializer class which contains:

     public class Serializer{    
         //generic deserialization from disk method
         public static void Deserialize<T>(string filename, ref KeyValuePairLists<T> data)
         {
             if (File.Exists (Application.dataPath + "/GameSave/" + filename)) {
                 BinaryFormatter bf = new BinaryFormatter ();
                 FileStream file = File.Open(Application.dataPath + "/GameSave/" +  filename, FileMode.Open);
                 data = (KeyValuePairLists<T>) bf.Deserialize (file);
                 file.Close();
             }
         }
 
         // deserialization overloads
         private void Deserialize(string filename, ref KeyValuePairLists<bool> data)
         {
             Deserialize (filename, ref data);
         }    
     }
 

So far I've managed to reduce all the errors down to:

 Assets/Scripts/ScriptableObjects/DataPersistence/SaveData.cs(182,14): error CS1502: The best overloaded method match for `SaveData.Serializer.Deserialize<bool>(string, ref SaveData.KeyValuePairLists<bool>)' has some invalid arguments

 Assets/Scripts/ScriptableObjects/DataPersistence/SaveData.cs(182,51): error CS1620: Argument `#2' is missing `ref' modifier


Comment
Add comment · Show 2
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 RobAnthem · May 10, 2017 at 06:34 PM 0
Share

You have to use the ref keyword when calling the method, much like using the out keyword. ' Although to be honest, this seems like a bad way to retrieve data. A simple deserializer that outputs a generic object that can be converted after, and only takes an inco$$anonymous$$g string for path or filename seems way more versatile. This can basically only deserialize one type of data, ever. Whereas the other way, would work for any serializable data at all. I wrote a tutorial on it if you want to take a look. Simple Data Storage It is under Part 3: Serializing Complex Objects.

avatar image Sorade RobAnthem · May 11, 2017 at 11:44 AM 0
Share

Thanks for the comment and link. Yes the system is the one from the Unity's adventure tutorial and can only store on type of data per $$anonymous$$eyValuePairLists. I have a question regarding serializable objects though. Can you serialize absolutely anything as long as you add the [serializable] keyword above the class name ? That sounds way to easy ...

1 Reply

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

Answer by TreyH · May 10, 2017 at 07:01 PM

It's telling you the problem, you need to include your "ref" keyword:

 Serializer.Deserialize (this.name + "Bool.dat", ref boolKeyValuePairLists);
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

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

70 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

Related Questions

How do i change my save "Total Coin" everytime i get more coins. 0 Answers

Bool array to binaryformatter method 1 Answer

Xml Serialization of "sub classes" 0 Answers

Serializing Properties (JSON saving and loading system) 2 Answers

Webplayer deserialization fails 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