[C#] How to export a "[Flags] enum"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hedi Sellami

In Godot, it is possible to export an int as flag bits like this:

[Export(PropertyHint.Flags, "Fire,Wind,Ice,Earth")] int myElement;

But c# (in its magnificent glory) also supports flags:

[Flags]  enum Element {None = 0x0, Fire = 0x1, Wind = 0x2, Ice = 0x4 }

Is there a way to define a [Flags] enum, and export it in godot? For example, i want to do this:

[Flags]  enum Element {None = 0x0, Fire = 0x1, Wind = 0x2, Ice = 0x4 }
[Export(PropertyHint.Flags, "Fire,Wind,Ice,Earth")] int myElementInt;
Element myElementEnum;

public override void _Ready() {
    myElementEnum = (Element) myElementInt
}

But i don’t have to write all the values again for each export.

Any ideas?

:bust_in_silhouette: Reply From: Flavelius

If anyone else is looking for this, this will do it:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ExportEnumAsFlagsAttribute : ExportAttribute
{
    public ExportEnumAsFlagsAttribute(Type enumType) : base(PropertyHint.Flags, enumType.IsEnum ? string.Join(",", Enum.GetValues(enumType).OfType<Enum>().Where(e=>(int)(object)e != 0)) : "Invalid Type")
    {
    }
}

usage:

[ExportEnumAsFlags(typeof(MyEnumType))] int myFlags;

ExportAttribute is sealed class so no longer viable option
but im searching for same issue, let me know when u solve :slight_smile: