Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sjoerdwouters · Jul 08, 2020 at 11:15 AM · vector3serializationsavingdatadictionary

Serializing Dictionary with Vector3 keys

hello everyone that finds this question. i need help with saving some data for my game. i need to serialize and store a dictionairy with a vector3 as key. exept unity cant do this for some stupid reason. please help me.

This is the data i want to store:

 [System.Serializable]
 public class WorldData
 {
     public string worldname;
     public int smoothing;
     public int noisetype;
     public float zoom;
     public int octaves;
     public int chunkrenderdistance;
     public int chunksize;
 
     public Dictionary<Vector3, WorldGenerator.TerrainChunk> WorldDataDictionairy = new 
     Dictionary<Vector3, WorldGenerator.TerrainChunk>();
 }

This is the function i nuse to save this data:

     //worldgenerator -> worlddata -> file
     public void SaveWorld()
     {
         var worldgenerator = 
         GameObject.FindObjectOfType<WorldGenerator> 
         ().GetComponent<WorldGenerator>();
 
         WorldData worlddata = new WorldData();
 
         worlddata.worldname = worldgenerator.worldname;
         worlddata.chunksize = worldgenerator.chunksize;
         worlddata.chunkrenderdistance = 
         worldgenerator.chunkrenderdistance;
         worlddata.octaves = worldgenerator.octaves;
         worlddata.zoom = worldgenerator.zoom;
         worlddata.noisetype = worldgenerator.noisetype;
         worlddata.smoothing = worldgenerator.smoothing;
         worlddata.WorldDataDictionairy = 
         worldgenerator.currentWorldChunksGenerated;
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/" + 
         worlddata.worldname + ".world");
         bf.Serialize(file, worlddata);
         file.Close();
     }

this is the function i use to load this data:

     //file -> worlddata -> worldgenerator
     public void LoadWorld()
     {
         var worldgenerator = 
         GameObject.FindObjectOfType<WorldGenerator> 
         ().GetComponent<WorldGenerator>();
 
         WorldData worlddata = new WorldData();
 
         BinaryFormatter bf = new BinaryFormatter();
         if(System.IO.File.Exists(Application.persistentDataPath + "/" + 
         DropDownLabel.text + ".world"))
         {
             FileStream file = File.OpenRead(Application.persistentDataPath + 
             "/" + DropDownLabel.text + ".world");
             if(file != null)
             {
                 worlddata = (WorldData)bf.Deserialize(file);
                 file.Close();
             }
         }
 
 
         worldgenerator.worldname = worlddata.worldname;
         worldgenerator.chunksize = worlddata.chunksize;
         worldgenerator.chunkrenderdistance = 
         worlddata.chunkrenderdistance;
         worldgenerator.octaves = worlddata.octaves;
         worldgenerator.zoom = worlddata.zoom;
         worldgenerator.noisetype = worlddata.noisetype;
         worldgenerator.smoothing = worlddata.smoothing;
         worldgenerator.currentWorldChunksGenerated = 
         worlddata.WorldDataDictionairy;
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ray2yar · Jul 08, 2020 at 11:58 AM

You need to save the information inside the dictionary individually (FALSE). However, your WorldTerrain data might also give you problems. Anyways.... something like this:

     foreach(Vector3 key in MyData.Keys)
     {
         //do stuff with the data... 

     }

Is your world procedural? If it is I think it would be way simpler to use Random.Seed and save JUST the random seed. Then when you load the game you just load t hat key, send it to your generator, and then rebuild the world from that.

Comment
Add comment · Show 4 · 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 sjoerdwouters · Jul 08, 2020 at 12:09 PM 0
Share

i first had a saving system, that saved just the seed. but now i want to make it possible to deform/edit the world, like digging inside the world mesh. but to store this infomation, i have to store the entire chunk.

also, i really cant figure out how to save the information inside the dictionary individually like you said.

avatar image ray2yar sjoerdwouters · Jul 08, 2020 at 12:24 PM 0
Share

I have saved dictionaries before, I'll share some code with you to do it in a $$anonymous$$ute. As far as defor$$anonymous$$g; maybe you could keep a separate dictionary of "deformations" and save those separately. Perhaps you could grab the terrain heightmap and save that? Anyways....

avatar image ray2yar sjoerdwouters · Jul 08, 2020 at 12:55 PM 0
Share

So I reject my previous answer- apparently you can directly save a dictionary since when I looked at my code that's exactly what I did. $$anonymous$$ake sure all your data types are set to serializable.

avatar image sjoerdwouters ray2yar · Jul 08, 2020 at 02:14 PM 0
Share

im already working on a system that saves deformations. alse here is my game, if you want to check it out: https://sjoerdwouters.itch.io/a-call-of-the-void

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

170 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 structure my project so that I can save/load information? 0 Answers

how to serialize unity variables? (Sprite, Image ...) 1 Answer

Saving players progress in ScriptableObject asset 1 Answer

Saving system tutorial / Load through main menu 1 Answer

Collecting MASSIVE Array of Vector3 Points 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