This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+3 votes

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?

in Engine by (20 points)

1 Answer

+6 votes

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;
by (26 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.