Home Game Development How to make use of a Custom Class as a Dictionary Key in Unity, the place one of many objects within the class is a List

How to make use of a Custom Class as a Dictionary Key in Unity, the place one of many objects within the class is a List

0
How to make use of a Custom Class as a Dictionary Key in Unity, the place one of many objects within the class is a List

[ad_1]

I’ve a ScriptableObject that holds the info that I’ll put right into a Dictionary:

utilizing System;
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Audio;
utilizing Unity.VisualScripting;

[CreateAssetMenu(fileName = "New PronunciationData", menuName = "Pronunciation Data", order = 51)]

public class PronunciationData : ScriptableObject
{
    [SerializeField]
    public MixKey thisBlendKey;

    [IncludeInSettings(true)]
    [System.Serializable]
    public class MixKey{

        [SerializeField]
        public string soundName;

        [SerializeField]
        public List<NeemData> neemList;
    
    }

    [SerializeField]
    public AudioClip Pronunciation;

}

MixKey would be the key for the Dictionary. It’s a {custom} class which incorporates 2 objects:

  • A string (e.g. the letters used to make a specific sound)
  • An inventory of things in one other {custom} class (e.g. a listing of the sounds themselves).

Pronunciation is simply an Audioclip, and would be the worth for the Dictionary.

The downside is that it doesn’t matter what MixKey I present, the Dictionary returns a worth of Null. In different phrases, it is unable to seek out something utilizing the keys I present – despite the fact that I’ve executed intensive checking to be sure that the bottom line is precisely the identical.

I feel there are 2 potential causes:

  1. I’m utilizing a {custom} class as a Key. Some sources point out that while you achieve this, you “have to override GetHashCode() (and ideally additionally Equals())”.
    https://stackoverflow.com/questions/6999191/use-custom-object-as-dictionary-key

  2. One of the objects in my Custom Class is a List. I’ve encountered issues evaluating Lists earlier than for a match, so think about it might be much more difficult to take action right here… the place you must match not solely 2 lists, but additionally the two different strings (i.e. the soundNames).

I’ve regarded into different potential knowledge constructions, however the problem is that I actually do want each the MixKey and the SoundName to be a novel mixture. So the Dictionary Key actually does want to make use of each of these variables.

Is it potential to edit the Scriptable Object to make use of overrides, such that Unity can efficiently search for values within the dictionary? Please let me know – thanks!

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here