EduardMalkhasyan / Serializable-Dictionary-Unity

Create a serializable dictionary in Unity with all necessary error detections
MIT License
38 stars 3 forks source link

Nested dictionnary #5

Closed nicolas-goyon closed 3 months ago

nicolas-goyon commented 3 months ago

[EDITED]

Here is the nested dictionnary :


public abstract class NestedDictionnary<T, K> : ScriptableObject where T : System.Enum where K : System.Enum
{
    public SerializableDictionary<T, NestedDictionnaryEntry> holder;

    public class NestedDictionnaryEntry{
        /// <summary>
        /// Names of leaderboards for each difficulty for that game mode
        /// </summary>
        public SerializableDictionary<K, string> entry;
    }
}

I have the following implement :

public class LeaderBoard : NestedDictionnary<GameMode, Difficulty> {}

but now i have the following error when adding a new element to the holder :

NullReferenceException: Object reference not set to an instance of an object
ProjectTools.SerializableDictionaryDrawer.IsSingleLine (UnityEditor.SerializedProperty prop) (at Assets/Plugins/SerializableDictionary/SerializableDictionaryDrawer.cs:344)
ProjectTools.SerializableDictionaryDrawer.<DrawListElement>g__DrawRects|3_1 (ProjectTools.SerializableDictionaryDrawer+<>c__DisplayClass3_0& ) (at Assets/Plugins/SerializableDictionary/SerializableDictionaryDrawer.cs:227)
ProjectTools.SerializableDictionaryDrawer.DrawListElement (UnityEngine.Rect rect, System.Int32 index, System.Boolean isActive, System.Boolean isFocused) (at Assets/Plugins/SerializableDictionary/SerializableDictionaryDrawer.cs:316)
UnityEditorInternal.ReorderableList.DoListElements (UnityEngine.Rect listRect, UnityEngine.Rect visibleRect) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
UnityEditorInternal.ReorderableList.DoList (UnityEngine.Rect rect, UnityEngine.Rect visibleRect) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
UnityEditorInternal.ReorderableList.DoList (UnityEngine.Rect rect) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
ProjectTools.SerializableDictionaryDrawer.<OnGUI>g__List|0_1 (ProjectTools.SerializableDictionaryDrawer+<>c__DisplayClass0_0& ) (at Assets/Plugins/SerializableDictionary/SerializableDictionaryDrawer.cs:144)
ProjectTools.SerializableDictionaryDrawer.OnGUI (UnityEngine.Rect rect, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (at Assets/Plugins/SerializableDictionary/SerializableDictionaryDrawer.cs:150)
UnityEditor.PropertyDrawer.OnGUISafe (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
UnityEditor.GenericInspector.OnOptimizedInspectorGUI (UnityEngine.Rect contentRect) (at <5092d70d16a64dd9a555dd50f38aead0>:0)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <963b7b516af945bcb7d30875c64e2e07>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
EduardMalkhasyan commented 3 months ago

@nicolas-goyon yeah the error reason is that class not serialized

[Serializable]
public class NestedDictionnaryEntry<K> where K : System.Enum
{
    /// <summary>
    /// Names of leaderboards for each difficulty for that game mode
    /// </summary>
    public SerializableDictionary<K, string> entry;
}

But it not going to work because nested dict will not be duplicated on creation of new instance so its plugin problem the only think i can provide is use manually nested dict

 public SerializableDictionary<string, SerializableDictionary<string, int>> nestedDict;

This will work

nicolas-goyon commented 3 months ago

Okay thanks

nicolas-goyon commented 3 months ago

Oh well, setting all 3 classes to serializable seems to work image

Not the prettiest but its working

nicolas-goyon commented 3 months ago

Oh, when using the nested class/struct (instead of public SerializableDictionary<string, SerializableDictionary<string, int>> nestedDict;)

if i add a key for the holder, it dupplicate the data and will cause issue with the nested dictionnary that will be the same object and when one is changed the other will be too...

But when using your method i have this display and no errors when dupplicate values :

image

EduardMalkhasyan commented 3 months ago

Yeah if its work for you that's good news!