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;
}