Search Unity

Question Is it possible to use Generic Dictionaries in Custom Classes in Visual Scripting?

Discussion in 'Visual Scripting' started by literacy, Oct 1, 2023.

  1. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    I made a ProfileState class to store a guid string, and also a Dictionary with key string and value ProfileState.

    [System.Serializable]
    public class ProfileState
    {
    public string guid;

    public static Dictionary<string, ProfileState> profileStateDictionary = new Dictionary<string, ProfileState>();

    public Profile profileData;
    }

    But when I try to create a generic Dictionary and set a Profile State Dictionary, I get a type mismatch error:

    InvalidConversionException: Cannot convert from 'Unity.VisualScripting.AotDictionary' to 'System.Collections.Generic.Dictionary`2[System.String,ProfileState]'.
    Unity.VisualScripting.ConversionUtility.Convert (System.Object value, System.Type type, Unity.VisualScripting.ConversionUtility+ConversionType conversionType)

    I see from the error that you can't create a Generic Dictionary and set it equal to a Profile State Dictionary.

    I ended up writing a Custom Node that creates a string/ProfileState dictionary for me, and that seemed to resolve the issue:

    upload_2023-10-1_12-29-53.png

    Is this the best approach though? I would have to create a Custom Node every time I wanted to make a dictionary for a different type of State. I also have to make Custom Nodes to Add and Remove Dictionary Items for each State type. Just feels like a bit of kludge.

    I could make a Generic Dictionary and then store that data in a scene variable, but I was hoping to use the Profile State custom class.

    Is there an easy way use Unity Visual Scripting to make a Generic Dictionary, and still store the data inside of a custom class?
     
    Last edited: Oct 1, 2023
  2. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    Try using an interface like ICollection or IDictionary
     
  3. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    39
    i did make a node like this

    2023-10-08 11_44_02-2D Practice - SampleScene - Windows, Mac, Linux - Unity 2022.2.18_ _DX11_.png

    i could give it to you if you want