UdonSharp icon indicating copy to clipboard operation
UdonSharp copied to clipboard

Enum types use enum value rather than enum name when converted to string

Open synergiance opened this issue 2 years ago • 6 comments

Describe the bug in detail: In C#, when you use ToString() on an enum type, it will return the name of the enum value, while in U# it returns the integer converted to a string. This is undesirable behavior most of the time, and unaligned with C#'s behavior.

Provide steps/code to reproduce the bug: Let's take this enum for instance:

public enum MyEnum {
    MyFirstValue,
    MySecondValue
}

If we print it out like this, we should see the problem:

MyEnum myEnumInstance = MyEnum.MyFirstValue;
Debug.Log(myEnumInstance.ToString());

Expected behavior: Log should read "MyFirstValue"

Observed behavior: Log actually reads "0"

synergiance avatar Oct 25 '22 21:10 synergiance

This is known and will have partial handling eventually but can't have full handling due to boxing obscuring the user type.

MerlinVR avatar Oct 25 '22 21:10 MerlinVR

Maybe add it to the "Differences from regular Unity C# to note" list at https://udonsharp.docs.vrchat.com/?

jellejurre avatar Oct 25 '22 21:10 jellejurre

Currently I work around it by creating a string array containing the names of the enum values.

Unfortunately though the workaround splits these into two separate parts of the code, which is undesirable. Is it possible to just internally create this structure and any enum tostring just reference the name array for that enum?

synergiance avatar Oct 25 '22 21:10 synergiance

Yeah that's the plan to generate arrays, you can make a PR for it if you want it before I get around to it. If you do, note that enums with the Flags attribute generate their ToString() value differently from ones without it.

MerlinVR avatar Oct 25 '22 21:10 MerlinVR

Speaking of enums using the flags attribute, it appears that operator | confuses the symbol processor on flags enums.

synergiance avatar Oct 26 '22 18:10 synergiance

Only equality and inequality are handled for enums at the moment

MerlinVR avatar Oct 26 '22 19:10 MerlinVR