- Home /
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;
     }
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.
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.
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....
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.
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
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                