vrchat-community / UdonSharp

A compiler for compiling C# to Udon assembly
https://udonsharp.docs.vrchat.com
MIT License
461 stars 50 forks source link

Issue with DataDictionary and using enums as keys, "Cannot retrieve heap variable of type 'Int32' as type 'DataToken" #137

Open MyroG opened 1 year ago

MyroG commented 1 year ago

The piece of code bellow generates the error "Cannot retrieve heap variable of type 'Int32' as type 'DataToken'"

DataDictionary _dict;

// more code here that initializes the dictionary

void SomeFunction(EEnum enumVal)
{
    DataToken key = (int) enumVal;
    if (!_dict.ContainsKey(key)) //crash here
    {
    }
}

To fix it, I need to use Convert.ToInt32() instead of using a regular (int) cast, like this :

DataToken key = Convert.ToInt32(enumVal);
if (!_dict.ContainsKey(key))