- Home /
The given key was not present in the dictionary, i am calling the key immediately after adding it
Here is the dictionaries i am calling from in the function
public Dictionary<Vector2, GameObject> TilePos = new Dictionary<Vector2, GameObject>();
public Dictionary<Vector2, GameObject> UnitPos = new Dictionary<Vector2, GameObject>();
public Dictionary<Vector2, GameObject> BuildingPos = new Dictionary<Vector2, GameObject>();
Here is the function that the error originates from
public void SaveMap(Dictionary<Vector2, GameObject> TP, Dictionary<Vector2, GameObject> UP,Dictionary<Vector2,GameObject> BP, string SaveName)
{
if (!System.IO.File.Exists(Application.dataPath + "/StreamingAssets/Saves/" + SaveName + ".dat"))
{
if (SaveName != "")
{
if (!Regex.IsMatch(SaveName, @"^[a-z][A-Z]+$"))
{
Map save = new Map();
foreach (KeyValuePair<Vector2, GameObject> kvp in TP)
{
foreach (var kvvp in DBC.TerrainDictionary)
{
if (kvp.Value.transform.name == kvvp.Value.Title)
{
save.TerrainPositions.Add(kvp.Key, kvvp.Value);
}
}
}
foreach (KeyValuePair<Vector2, GameObject> ukvp in UP)
{
if (ukvp.Value != null)
{
foreach (var keyvp in DBC.UnitDictionary)
{
if (ukvp.Value.transform.name == keyvp.Value.Title)
{
save.UnitPosition.Add(ukvp.Key, keyvp.Value);
//this errors, cant figure out why
save.UnitPosition[ukvp.Key].Team = ukvp.Value.transform.GetComponent<SpriteController>().Team;
//this errors, cant figure out why
}
}
}
}
foreach (KeyValuePair<Vector2, GameObject> bkvp in BP)
{
if (bkvp.Value != null)
{
foreach (var kvp in DBC.BuildingDictionary)
{
if (bkvp.Value.transform.name == kvp.Value.Title)
{
save.BuildingPositions.Add(bkvp.Key, kvp.Value);
save.BuildingPositions[bkvp.Key].Team = bkvp.Value.transform.GetComponent<SpriteController>().Team;
}
}
}
}
string destination = Application.dataPath + "/StreamingAssets/Saves/" + SaveName + ".dat";
var fs = File.Create(destination);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, save);
fs.Close();
MEMCC.SaveFeedback.text = "File saved as: " + SaveName;
}
else
{
MEMCC.SaveFeedback.text = "Only use letters";
}
}
else
{
MEMCC.SaveFeedback.text = "Add a name.";
}
}
else
{
MEMCC.SaveFeedback.text = "Current file name is already in use, please rename your save or delete old file.";
}
}
Here is the class for Map
[Serializable]
public class Map
{
public Dictionary<SeralizableVector2, Terrain> TerrainPositions = new Dictionary<SeralizableVector2, Terrain>();
public Dictionary<SeralizableVector2, Unit> UnitPosition = new Dictionary<SeralizableVector2, Unit>();
public Dictionary<SeralizableVector2, Building> BuildingPositions = new Dictionary<SeralizableVector2, Building>();
}
This is my unique vector2 (unity vector2 are not serializable)
[Serializable]
public class SeralizableVector2
{
public float x;
public float y;
public SeralizableVector2(float rx,float ry)
{
x = rx;
y = ry;
}
public static implicit operator Vector2 (SeralizableVector2 rValue)
{
Vector2 newVec = new Vector2(rValue.x, rValue.y);
return newVec;
}
public static implicit operator SeralizableVector2(Vector2 Value)
{
return new SeralizableVector2(Value.x, Value.y);
}
}
Let me know if more info is needed. Any help is appreciated.
Answer by DiegoSLTS · Apr 22, 2019 at 03:15 AM
You have to override the Equals and GetHashCode methods in a class if you want to use instances of that class as keys of a Dictionary, or you can pass a IEqualityComparer when you create the Dictionary object.
Again, thank you if you can provide some insight, this is driving me nuts because I cannot see the problem for the life of me.
This is the official documentation of IEqualityComparer: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=netframework-4.8
You must tell the dictionary hoy to compare to instances of the key Class, otherwise the Dictionary won't find a key even if you added it. Equals and GetHashCode (or the IEqualityComparer, which implements the same methods) are used by the dictionary. If you search "IEqualityComparer" on Google you'll find lots of examples.
i bow to you my master, now on to other bugs...... lol