Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

key not present error

From my understanding what should be happening here is explained in the comments but for some reason I get this error in the editor

KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.Int32,System.String].get_Item (Int32 key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) UpgradeManager.ScanForAvailableUpgrades () (at Assets/Scripts/UpgradeManager.cs:125 UpgradeManager.Update () (at Assets/Scripts/UpgradeManager.cs:70)

The error happens after the function DisableSameType is called

public string GetUpgradeType(string upgradeName){
        return upgradeTypes [upgradeName];
    }

    public void DisableSameType (string upgradeName){
        string upgradeType = GetUpgradeType (upgradeName);

        string[] keys = KeyByValue (upgradeTypes, upgradeType); //returns all the keys of the same type

        if (keys.Length == 0) { //if there are no keys
            return;             //then return
        } else {
            for (int i = 0; i < keys.Length; i++) { //loop through all the keys
                upgradesOwned [keys [i]] = false;   //Set them to false
            }
        }
    }

And here are other relevant code

public List<string> AvailableUpgrades = new List<string>();

    protected Dictionary<int,string> upgradeNames = new Dictionary<int, string>();

    protected Dictionary<string,bool> upgradesOwned = new Dictionary<string, bool>();
    protected Dictionary<string,bool> upgradesPurchased = new Dictionary<string, bool>();

    protected Dictionary<string,int> upgradeCost = new Dictionary<string, int>(); 

    /* UPGRADE TYPES
     * Title
     * Layout
     * Scoreboard
     * VisualCountdown
     */
    protected Dictionary<string,string> upgradeTypes = new Dictionary<string, string>();

protected bool IsPurchased(string upgradeName){
    return upgradesPurchased [upgradeName];
}

public static string[] KeyByValue(Dictionary<string, string> dict, string val)
    {
        string key = null;
        foreach (KeyValuePair<string, string> pair in dict)
        {
            if (pair.Value == val)
            { 
                key += pair.Key + "|"; 
                break; 
            }
        }
        return key.Split('|');
    }

protected void ScanForAvailableUpgrades(){
        AvailableUpgrades.Clear ();                                             //Clear the list
        int scanRange = GetUnits() + GameManager.Instance.scanRange;            //Get the range by adding the scanRange to current units
        for (int i = 0; i < upgradesOwned.Count; i++) {                     //Loop through all upgrades
            if (IsPurchased (upgradeNames [i])) {
                //return
            } else {
                if (scanRange >= upgradeCost [upgradeNames [i]]) {                      //Check if the cost is within the scanrange
                    if (AvailableUpgrades.Contains (upgradeNames [i])) {                //Check if upgrade is already in the available list
                        //return;                                                   //If it is we just return
                    } else {                                                        //Else
                        if (upgradesOwned [upgradeNames [i]]) {                 //Check if the user already has the upgrade *NOTE* Should probaby check for this first to improve performance
                            //return;                                               //If it is return
                        } else {                                                    //Else
                            AvailableUpgrades.Add (upgradeNames [i]);                   //Upgrade does not exist and is not bought so add it to the available list
                        }
                    }
                }
            }
        }
    }

after further debugging i have found out that Debug.Log(keys[i]); is returning the first string as "basicTitle|" and the string string is blank

EDIT: I can see where my question can seem misleading line 125 is this line if (IsPurchased (upgradeNames [i])) { however Ive already gone through and after commenting out "DisableSameType" the error goes away the method DisableSameType is a new function and the error didnt start happening till it was implemented so I am 100% positive this is the source of the error im just not sure what is causing it

like image 736
ForgedInFlame Avatar asked Nov 28 '25 04:11

ForgedInFlame


1 Answers

You don't have a valid key in upgradesOwned dictionary.
Before setting the key value to false, you should check if the dictionary has the key. If it has the key then you can make it false, otherwise you'll need to Add a key value pair in the dictionary.
So, instead of this line in your code:

upgradesOwned [keys [i]] = false;   //Set them to false

It should be like this:

if(upgradesOwned.ContainsKey(keys[i])){
    upgradesOwned [keys [i]] = false;   //Set them to false
}else{
    upgradesOwned.Add(keys [i],false);   //Add new key and set them to false
}
like image 76
ZayedUpal Avatar answered Nov 30 '25 18:11

ZayedUpal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!