KeyNotFoundException: The given key 'Player' was not present in the dictionnary

Hello.
I’ve a strange message when I launch the game at this state of the course.

I understand that the classes aren’t in the dictionnary but why ?
I must signal that I haven’t a look like sam in the Progression scriptable object but it worked so far.
Is it an indentation problem ?
here is my Progression scriptable object “code”:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 0}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 10073e6f9ccbd1d468678ecd4a785653, type: 3}
  m_Name: progression
  m_EditorClassIdentifier: 
  characterClasses:
  - characterClass: 0
    stats:
    - stat: 0
      levels:
      - 20
      - 50
      - 100
      - 200
      - 400
  - characterClass: 4
    stats:
    - stat: 0
      levels:
      - 20
      - 50
      - 100
    - stat: 1
      levels:
      - 15
      - 30
      - 60
  - characterClass: 5
    stats:
    - stat: 0
      levels:
      - 20
      - 50
      - 100
      - 200
      - 400
    - stat: 1
      levels:
      - 15
      - 30
      - 60
      - 120
      - 240
  - characterClass: 6
    stats:
    - stat: 0
      levels:
      - 20
      - 50
      - 100
      - 200
      - 400
    - stat: 1
      levels:
      - 15
      - 30
      - 60
      - 120
      - 240

My Classes are listed in the CharacterClass script :

namespace RPG.Stats
{
    public enum CharacterClass
    {
        Player,
        Primitif,
        peasant,
        Raider,
        Cannibal_Unarmed,
        Cannibal_OneHandWeapon,
        Cannibal_TwoHandsWeapon,
        Merchant,
        Wanderer,
        scientist,
        technoFan
    }
}

My Progression Script:

using System;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Stats
{
    [CreateAssetMenu(fileName = "progression", menuName = "RPG Stats/New Progression", order = 0)]
    public class Progression:ScriptableObject
    {

        [SerializeField] ProgressionCharacterClass[] characterClasses = null;

        Dictionary<CharacterClass,Dictionary<Stat, float[]>> lookupTable = null;

        public float GetStat(Stat stat,CharacterClass characterClass, int level)//Dans cet ordre comme dans le code ci-après :
        {
            BuildLookup();
            float[] levels = lookupTable[characterClass][stat];
            if(levels.Length < level)
            {
                return 0;
            }
            return levels[level-1];
        }

        private void BuildLookup()
        {
            if(lookupTable != null) return;
            lookupTable = new Dictionary<CharacterClass,Dictionary<Stat, float[]>>();
            foreach(ProgressionCharacterClass progressionClass in characterClasses)
            {
                var statLookupTable = new Dictionary<Stat, float[]>();
                foreach(ProgressionStat progressionStat in progressionClass.stats)
                {
                    statLookupTable[progressionStat.stat] = progressionStat.levels;
                }
                lookupTable[progressionClass.characterClass] = statLookupTable;
            }
        }

        [System.Serializable]
        class ProgressionCharacterClass
        {
            public CharacterClass characterClass;
            public ProgressionStat[] stats;         
        }

        [System.Serializable]
        class ProgressionStat
        {
            public Stat stat;
            public float[] levels;//L'array sert pour le niveau du personnage.Ca vie sera différente en fonctiond e son niveau.
        }
    }
}

Any Idea ?
Thanks.
François

The keys definitely should be in the Dictionary, as your code looks exactly right.

Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a look.

Hello Brian.
Thanks for your feedback.
I sent you my zip project file without Library folder as said in the upload page.
I’ve tested it without it and it works, Unity rebuild it.
Thank you.
François

PS: I use Unity 2022.3.14f1

This was odd, as there was a discrepancy in the BuildLookup() script from what was posted here…
This is from the script in the project

        private void BuildLookup()
        {
            lookupTable = new Dictionary<CharacterClass,Dictionary<Stat, float[]>>();

            if(lookupTable != null) return;

            foreach(ProgressionCharacterClass progressionClass in characterClasses)
            {
                var statLookupTable = new Dictionary<Stat, float[]>();

                foreach(ProgressionStat progressionStat in progressionClass.stats)
                {
                    statLookupTable[progressionStat.stat] = progressionStat.levels;
                }

                lookupTable[progressionClass.characterClass] = statLookupTable;
            }
        }

In this case, the first thing that BuildLookup() does is create a new Dictionary, but we really only want it to do that when there isn’t an existing Dictionary in the first place.

Make sure that the first line in BuildLookup() is

if(lookupTable!=null) return;

Then after that, you should create the new Dictionary and populate it.
I’m not sure how this was in the correct order on the script you posted here.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms